Guten Abend,
habe morgen ein Praktikum und habe dafür ein Programm geschrieben was von Joul mach Kalorien umrechnet.
Ich habe das Programm mit if und else geschrieben weil es für mich so am einfachsten ist, nun habe ich heute gehört das ich switch und case benutzen soll.
Habe damit sehr große Probleme.
Hier ist das Programm mit if und else. Darunter befindet sich meine Idee mit switch und case.
[JAVA=1]
import javax.swing.*;
class Joule_Kalorien {
public static double jink(double zahl)
{
double kalorie = zahl*0.238663;
return kalorie;
}
public static double kinj(double zahl)
{
double joule = zahl/0.238663;
return joule;
}
public static void ausgabe1(double kalorie, double zahl)
{
JOptionPane.showMessageDialog(null, zahl+" Joule enspricht "+kalorie+" cal!", "Joule in Kalorien", JOptionPane.PLAIN_MESSAGE);
}
public static void ausgabe2(double joule, double zahl)
{
JOptionPane.showMessageDialog(null, zahl+" cal enspricht "+joule+" Joule!", "Kalorien in Joule", JOptionPane.PLAIN_MESSAGE);
}
public static void main(String args[]) {
int i=0;
String str[] = {"J -> cal", "cal -> J"};
i = JOptionPane.showOptionDialog(null,
"Umrechnen von ... nach ...",
"Umrechnen",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
str,
JOptionPane.YES_OPTION);
if(i==0)
{
String eingabe1 = JOptionPane.showInputDialog("Geben Sie den Wert in Joule an!");
double zahl = Double.parseDouble(eingabe1);
if(zahl<0)
{
JOptionPane.showMessageDialog(null, "Bitte geben Sie nur positive Zahlen ein!", "Warning", JOptionPane.WARNING_MESSAGE);
}
else
{
double kalorie=jink(zahl);
ausgabe1(kalorie,zahl);
}
}
if(i==1)
{
String eingabe2 = JOptionPane.showInputDialog("Geben Sie den Wert in Kalorien an!");
double zahl = Double.parseDouble(eingabe2);
if(zahl<0)
{
JOptionPane.showMessageDialog(null, "Bitte geben Sie nur positive Zahlen ein!" , "Warning", JOptionPane.WARNING_MESSAGE);
}
else
{
double joule=kinj(zahl);
ausgabe2(joule,zahl);
}
}
System.exit(0);
}
}
[/code]
Zu dem muss ich noch sagen das J nach cal (entsricht Yes.Option) und von cal nach J (entsricht NO.Option.
Das Programm funktoniert so nicht wie ich es geschrieben habe und ich muss das auch so machen das wenn eine negative zahl in das eingabefeld eingegeben wird das der eine warnung rausgibt wie das obere Programm. Da hatte ich aber gar keine idee wie ich das machen soll.
Ich hoffe ihr könnt mir helfen.
habe morgen ein Praktikum und habe dafür ein Programm geschrieben was von Joul mach Kalorien umrechnet.
Ich habe das Programm mit if und else geschrieben weil es für mich so am einfachsten ist, nun habe ich heute gehört das ich switch und case benutzen soll.
Habe damit sehr große Probleme.
Hier ist das Programm mit if und else. Darunter befindet sich meine Idee mit switch und case.
[JAVA=1]
import javax.swing.*;
class Joule_Kalorien {
public static double jink(double zahl)
{
double kalorie = zahl*0.238663;
return kalorie;
}
public static double kinj(double zahl)
{
double joule = zahl/0.238663;
return joule;
}
public static void ausgabe1(double kalorie, double zahl)
{
JOptionPane.showMessageDialog(null, zahl+" Joule enspricht "+kalorie+" cal!", "Joule in Kalorien", JOptionPane.PLAIN_MESSAGE);
}
public static void ausgabe2(double joule, double zahl)
{
JOptionPane.showMessageDialog(null, zahl+" cal enspricht "+joule+" Joule!", "Kalorien in Joule", JOptionPane.PLAIN_MESSAGE);
}
public static void main(String args[]) {
int i=0;
String str[] = {"J -> cal", "cal -> J"};
i = JOptionPane.showOptionDialog(null,
"Umrechnen von ... nach ...",
"Umrechnen",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
str,
JOptionPane.YES_OPTION);
if(i==0)
{
String eingabe1 = JOptionPane.showInputDialog("Geben Sie den Wert in Joule an!");
double zahl = Double.parseDouble(eingabe1);
if(zahl<0)
{
JOptionPane.showMessageDialog(null, "Bitte geben Sie nur positive Zahlen ein!", "Warning", JOptionPane.WARNING_MESSAGE);
}
else
{
double kalorie=jink(zahl);
ausgabe1(kalorie,zahl);
}
}
if(i==1)
{
String eingabe2 = JOptionPane.showInputDialog("Geben Sie den Wert in Kalorien an!");
double zahl = Double.parseDouble(eingabe2);
if(zahl<0)
{
JOptionPane.showMessageDialog(null, "Bitte geben Sie nur positive Zahlen ein!" , "Warning", JOptionPane.WARNING_MESSAGE);
}
else
{
double joule=kinj(zahl);
ausgabe2(joule,zahl);
}
}
System.exit(0);
}
}
[/code]
Zu dem muss ich noch sagen das J nach cal (entsricht Yes.Option) und von cal nach J (entsricht NO.Option.
Das Programm funktoniert so nicht wie ich es geschrieben habe und ich muss das auch so machen das wenn eine negative zahl in das eingabefeld eingegeben wird das der eine warnung rausgibt wie das obere Programm. Da hatte ich aber gar keine idee wie ich das machen soll.
Ich hoffe ihr könnt mir helfen.
Java:
switch (i)
{
case JOptionPane.YES_OPTION : String eingabe1 = JOptionPane.showInputDialog("Geben Sie den Wert in Joule an!");
double zahl = Double.parseDouble(eingabe1);
double kalorie=jink(zahl);
ausgabe1(kalorie,zahl);
break;
case JOptionPane.NO_OPTION : String eingabe2 = JOptionPane.showInputDialog("Geben Sie den Wert in Kalorien an!");
double zahl = Double.parseDouble(eingabe2);
double joule=kinj(zahl);
ausgabe2(joule,zahl);
}