Guten Tag ich habe heute einen Temperaturenumrechner programmiert jedoch habe ich folgendes Problem: Es wird jede if Anweisung ausgeführt obwohl sie eigentlich nicht ausführt werden sollten. Wo liegt der Fehler?
Java:
import javax.swing.*;
public class PhysikTemperaturumrechner {
public static void main(String[] args) {
String eingabe = JOptionPane.showInputDialog("Gebe die Temperatur ein die du umrechnen möchtest");
int zahl = Integer.parseInt(eingabe);
Object[] options = {"Fahrenheit", "Celsius","Kelvin"};
int selected = JOptionPane.showOptionDialog(null, "Deine Temperaturskala!", "Auswahl", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
int baum = JOptionPane.showOptionDialog(null, "Umrechnen in..?", "Auswahl", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
if ( selected == 0 && baum == 0 )
javax.swing.JOptionPane.showMessageDialog(null,"Error" + " du kannst deine Temperatur nich in Fahrenheit umwandeln!");
if ( selected == 1 && baum == 1 )
javax.swing.JOptionPane.showMessageDialog(null,"Error" + " du kannst deine Temperatur nich in Celcius umwandeln!");
if ( selected == 2 && baum == 2 )
javax.swing.JOptionPane.showMessageDialog(null,"Error" + " du kannst deine Temperatur nich in Kelvin umwandeln!");
double y = 0;
// Fahrenheit zu Celcius
if ( selected == 0 || baum == 1 )
y = (zahl-32) *5/9;
javax.swing.JOptionPane.showMessageDialog(null, "Ihre Umrechnung1," + y + "°C");
// Fahrenheit in Kelvin
if ( selected == 0 && baum == 2 )
y = (zahl-32) *5/9 + 273.15;
javax.swing.JOptionPane.showMessageDialog(null, "Ihre Umrechnung2," + y + "K");
// Celcius in Fahrenheit
if ( selected == 1 && baum == 0 );
y = zahl * 9 / 5 + 32;
javax.swing.JOptionPane.showMessageDialog(null, "Ihre Umrechnung3," + y + "°F");
// Celcius in Kelvin
if ( selected == 1 && baum == 2 );
y = zahl + 273.15;
javax.swing.JOptionPane.showMessageDialog(null, "Ihre Umrechnung4," + y + "K");
// Kelvin in Celcius
if ( selected == 2 && baum == 1 );
y = zahl - 273.15;
javax.swing.JOptionPane.showMessageDialog(null, "Ihre Umrechnung5," + y + "°C");
// Kelvin in Fahrenheit
if ( selected == 2 && baum == 0 );
y = (zahl - 273.15) *9/5+32;
javax.swing.JOptionPane.showMessageDialog(null, "Ihre Umrechnung6," + y + "°F");
}
}
Zuletzt bearbeitet von einem Moderator: