Guten Abend,
wollte mal aus Spaß ein Programm ändern was mit if und else geschrieben ist. Anstatt if und else wollte ich dann switch und case anwenden.
Ich dachte das wird schon gehen aber nach mehreren Überlegungen komme ich einfach nicht drauf ?
Ich hoffe um gute Vorschläge.
Zu dem habe ich eine frage, müsste ich das Programm großartig ändern ?
Das Programm und dann meine Idee (das programm rechnet von Ps in kW um und umgekehrt):
[JAVA=42]import javax.swing.*;
class Ps_kW {
public static double pink(double zahl)
{
double kw = zahl*0.735499;
return kw;
}
public static double kinp(double zahl)
{
double ps = zahl/0.735499;
return ps;
}
public static void ausgabe1(double kw, double zahl)
{
JOptionPane.showMessageDialog(null, zahl+" Ps enspricht "+kw+" kW!", "Ps in kW", JOptionPane.PLAIN_MESSAGE);
}
public static void ausgabe2(double ps, double zahl)
{
JOptionPane.showMessageDialog(null, zahl+" kW enspricht "+ps+" Ps!", "kW in Ps", JOptionPane.PLAIN_MESSAGE);
}
public static void main(String args[]) {
int i=0;
String str[] = {"Ps -> kW", "kW -> Ps"};
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 Ps 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 kw=pink(zahl);
ausgabe1(kw,zahl);
}
}
if(i==1)
{
String eingabe2 = JOptionPane.showInputDialog("Geben Sie den Wert in kW 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 ps=kinp(zahl);
ausgabe2(ps,zahl);
}
}
System.exit(0);
}
}
[/code]
Und meien Idee (nur die erste if und else bedingunggeändert) zu dem weiß ich das ich bei case folgendes nicht angeben kann "zah<0" da habe ich auch ??????? hingeschrieben. Ich bitte darum mir zu helfen wie ich das sonst schreiben könnte.
[JAVA=47]
switch (i) {
case 1: i=0 ;
String eingabe1 = JOptionPane.showInputDialog("Geben Sie den Wert in Ps an!");
double zahl = Double.parseDouble(eingabe1);
break;
case 2: zahl<0 ; (???????)
JOptionPane.showMessageDialog(null, "Bitte geben Sie nur positive Zahlen ein!", "Warning", JOptionPane.WARNING_MESSAGE);
break;
default : double kw=pink(zahl);
ausgabe1(kw,zahl);
}[/code]
lg Simon
wollte mal aus Spaß ein Programm ändern was mit if und else geschrieben ist. Anstatt if und else wollte ich dann switch und case anwenden.
Ich dachte das wird schon gehen aber nach mehreren Überlegungen komme ich einfach nicht drauf ?
Ich hoffe um gute Vorschläge.
Zu dem habe ich eine frage, müsste ich das Programm großartig ändern ?
Das Programm und dann meine Idee (das programm rechnet von Ps in kW um und umgekehrt):
[JAVA=42]import javax.swing.*;
class Ps_kW {
public static double pink(double zahl)
{
double kw = zahl*0.735499;
return kw;
}
public static double kinp(double zahl)
{
double ps = zahl/0.735499;
return ps;
}
public static void ausgabe1(double kw, double zahl)
{
JOptionPane.showMessageDialog(null, zahl+" Ps enspricht "+kw+" kW!", "Ps in kW", JOptionPane.PLAIN_MESSAGE);
}
public static void ausgabe2(double ps, double zahl)
{
JOptionPane.showMessageDialog(null, zahl+" kW enspricht "+ps+" Ps!", "kW in Ps", JOptionPane.PLAIN_MESSAGE);
}
public static void main(String args[]) {
int i=0;
String str[] = {"Ps -> kW", "kW -> Ps"};
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 Ps 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 kw=pink(zahl);
ausgabe1(kw,zahl);
}
}
if(i==1)
{
String eingabe2 = JOptionPane.showInputDialog("Geben Sie den Wert in kW 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 ps=kinp(zahl);
ausgabe2(ps,zahl);
}
}
System.exit(0);
}
}
[/code]
Und meien Idee (nur die erste if und else bedingunggeändert) zu dem weiß ich das ich bei case folgendes nicht angeben kann "zah<0" da habe ich auch ??????? hingeschrieben. Ich bitte darum mir zu helfen wie ich das sonst schreiben könnte.
[JAVA=47]
switch (i) {
case 1: i=0 ;
String eingabe1 = JOptionPane.showInputDialog("Geben Sie den Wert in Ps an!");
double zahl = Double.parseDouble(eingabe1);
break;
case 2: zahl<0 ; (???????)
JOptionPane.showMessageDialog(null, "Bitte geben Sie nur positive Zahlen ein!", "Warning", JOptionPane.WARNING_MESSAGE);
break;
default : double kw=pink(zahl);
ausgabe1(kw,zahl);
}[/code]
lg Simon