variable istpositiv might not have been initialized

ruubi

Mitglied
Code:
Java:
import javax.swing.JOptionPane;

public class Zahlentest {
    public static void main(String[] args) {
         String eingabe;
         double c;
         boolean istpositiv;
         eingabe = JOptionPane.showInputDialog("Geben sie die Zahl ein: ");
         c = Double.valueOf(eingabe);
         if (c > 0) {
             istpositiv = true;
               } else {
                    if (c < 0) {
                    istpositiv = false; }
               }
         if (istpositiv == true) {
             JOptionPane.showMessageDialog(null, c + "ist positiv!"); }
         else {
             JOptionPane.showMessageDialog(null, c + "ist negativ!"); }
    }
}
 
Zuletzt bearbeitet von einem Moderator:
mit einem boolean initialiesieren
Stimmt!
Außerdem kann man das Ganze viel einfacher formulieren.
Java:
double c = 0;
        try {
            String eingabe = JOptionPane.showInputDialog("Geben sie die Zahl ein: ");
            c = Double.valueOf(eingabe);
        } catch (NumberFormatException e) {
        }
        String tmp = "positiv";
        if (c < 0)
            tmp = "negativ";
        JOptionPane.showMessageDialog(null, c + " ist " + tmp + "!");
 

Zurück
Oben