JOptionPane

Fürsti

Neues Mitglied
Guten Tag,
Wir haben von unseren Professor eine Aufgabe bekommen, in der wir auswählen sollen, ob von Celsius in Fahrenheit oder von Celsius in Kelvin umgerechnet werden soll. Wir sollen es mit JOptionPane machen. Bis zu einem gewissen Punkt habe ich es geschafft, aber jetzt weiß ich nicht mehr weiter, wie ich es mache das die Message box mit Kelvin angezeigt wird.
Ich würde mich sehr über rasche hilfe freuen.
MFG,
Fürsti



double celsius = Double.parseDouble(
JOptionPane.showInputDialog("Geben Sie eine Temperatur ein: "));

String [] einheit = {"Fahrenheit","Kelvin"};

int wahl = JOptionPane.showOptionDialog(null, "In welcher Einheit soll es um gerechnet werden", "Einheit",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, einheit, einheit[0]);


JOptionPane.showMessageDialog(null, (celsius *9/5) + 32 + " Fahrenheit", "Ergebniss" , JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
JOptionPane.showMessageDialog(null, (celsius + 273.15) + " Kelvin", "Ergebniss" , JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
 

Robat

Top Contributor
Frag doch einfach mit einer einfachen if-else ab, welcher der beiden letzten OptionPanes angezeigt werden soll.
 
X

Xyz1

Gast
Gucke mal:
Java:
import java.util.HashSet;
import java.util.Scanner;
import java.util.StringJoiner;

public class Umrechnen {
    static Object[][] tabelle = {
        {"Bit",         "Byte",             8.0},
        {"Kilobyte",    "Byte",          1024.0},
        {"kW",          "PS",               1.3596216},
        {"m",           "Inch",            39.3700787},
        {"m",           "Fuss",             3.2808398},
        {"m",           "Yard",             1.0936132},
        {"m",           "Meilen",           0.0006213},
        {"m",           "Seemeilen",        0.0005399},
        {"qm",          "a",                0.01},
        {"qm",          "ha",               0.0001},
        {"l",           "Registertonne",    0.0003531},
        {"Kilogramm",   "Zentner",          0.02},
        {"Kilogramm",   "Karat",         5000.0},
        {"km/h",        "mph",              0.6213711},
        {"Celsius",     "Kelvin",         274.15},
        {"Celsius",     "Fahrenheit",      33.8},
    };

    public static void main(String[] args) {
        HashSet<String> s = new HashSet<>();
        StringJoiner j = new StringJoiner(", ", "(", ")");
        for (Object[] o : tabelle) {
            if (!s.contains((String) o[0])) {
                s.add((String) o[0]);
                j.add((String) o[0]);
            }
            if (!s.contains((String) o[1])) {
                s.add((String) o[1]);
                j.add((String) o[1]);
            }
        }

        System.out.println("Von:");
        System.out.println(j.toString() + ":");
        String von = new Scanner(System.in).nextLine();
        System.out.println("Wert:");
        double w = new Scanner(System.in).nextDouble();

        StringJoiner k = new StringJoiner(", ", "(", ")");
        for (Object[] o : tabelle) {
            if (o[0].equals(von)) {
                k.add((String) o[1]);
            }
            if (o[1].equals(von)) {
                k.add((String) o[0]);
            }
        }
        System.out.println("Nach:");
        System.out.println(k.toString() + ":");
        String nach = new Scanner(System.in).nextLine();

        for (Object[] o : tabelle) {
            if (o[0].equals(von) && o[1].equals(nach)) {
                System.out.println(w * (Double) o[2]);
            }
            if (o[1].equals(von) && o[0].equals(nach)) {
                System.out.println(1.0 / (Double) o[2] * w);
            }
        }

        System.out.println("fertig");
    }
}

Code:
Von:
(Bit, Byte, Kilobyte, kW, PS, m, Inch, Fuss, Yard, Meilen, Seemeilen, qm, a, ha, l, Registertonne, Kilogramm, Zentner, Karat, km/h, mph, Celsius, Kelvin, Fahrenheit):
mph
Wert:
186,41
Nach:
(km/h):
km/h
299.99785957216227
fertig

Wenn nicht bei tabelle vertan dann sollte helfen es Dir.
 

Neue Themen


Oben