Variablen Aktualisieren von Instanzvariablen

okay, muss es besser erklären,
also das ist das "Ende" meines Programmes:

Java:
 while (antwort== "j")
            {
                System.exit(0);
                
            }
             while (antwort == "n"){
                 
             }
               
             while (antwort != "j"){
                 System.out.println("Falsche Eingabe!");
                 
             }
             while (antwort != "n"){
                 System.out.println("Falsche Eingabe!");
             }
                 
             }

und bei while antwort == n möchte ich eine Methode automatisch wiederholen.
 
Java:
while (antwort != "n"){
                 System.out.println("Falsche Eingabe!");
             }

und

Java:
while (antwort == "n"){
                 
             }


widerspricht sich.

besser:

Java:
while (antwort != "n" && antwort !="j"){
                 
             }

dafür den 3. fall rauswerfen.

instanzvariablen setzt du mit zb.:
#
Java:
this.antwort="wert";
 
also, muss ich das sozusagen alles am anfang this setzen?

ich schick dir mal das komplette Programm.. weiß nicht so recht,
welches "Wort" ich genau wiederholen sollte,
damit sich die Methode automatisch wiederholt...

Java:
public class MPGTester2 {
    
    private double tanfangsbestand = 0.0;
    private double tachometerstand;
    private double anzgallone;
    private double meilen;
    private String antwort;
    
    public MPGTester2(double tanfangsbestand, double tachometerstand, double anzgallone, double meilen, String antwort){
        
        
    }
    public static void main (String[] args){
        
        double tanfangsbestand = 0.0;
        double tachometerstand;
        double anzgallone;
        
        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Neues Auto - Tachometer-Anfangsstand: " + tanfangsbestand);
        
        System.out.println("Aktuellen Tachostand eingeben:");
        tachometerstand = scan.nextDouble();
        
        System.out.println("Anzahl der Gallonen für die Tankfüllung:");
        anzgallone = scan.nextDouble();
        
       MPGTester2 t = new MPGTester2();
        
       t.auftanken(tachometerstand, anzgallone);
       
        
   }
       void auftanken(double tachometerstand, double anzgallone){
            
            double m = tachometerstand / anzgallone;
            
            System.out.println("Meilen pro Gallone: " + m);
            
            if (m < 15.0){
                System.out.println("Autotyp: Sparauto!");
            }
                else if (m >30.0){
                    System.out.println("Autotyp: Benzinschwein!");
            }
 
Strings vergleicht man mit [c]equals()[/c], nicht mit [c]==[/c]!
@achillesat: mit nur einem [c]=[/c], noch mehr Kacke 😉

also sozusagen so

Java:
      while (antwort.equals("j"))
            {
                System.exit(0);
                
            }
             while (antwort.equals("n")){
                 
             }
               
             while (antwort != "j" && antwort != "n"){
                 System.out.println("Falsche Eingabe!");
                 
             }

und wie mach ich das bei dem dritten, wenns ungleich ist??
also mal auf das equal bezogen
 
hallo Julschgi,

dafür gibt es den Nicht-Operator
Code:
!
der ändert einen Wahrheitswert.

also z.B.:
Java:
if(!antwort.equals("j") && !antwort.equals("n")) {
    System.out.println("Falsche Eingabe!");
}
 
Java:
  while (!antwort.equals("j") && !antwort.equals("n") ){
                 System.out.println("Falsche Eingabe!");  
             }

Edit: Zu langsam...
 
Oh wow..das was du da baust wird so nicht funktionieren... 🙂

Ich vermute, du willst sowas haben:

Code:
String input = null;

while(!"n".equals((input = readInput())){
  if("y".equals(input)){
     // mach was mit y
  } else if("abc".equals(input)){
    // tu was anderes
  }
}
 
so, also es funktioniert oÔ

Java:
public class MPGTester2 {

    private double tanfangsbestand = 0.0;
    private double tachometerstand;
    private double anzgallone;
    private double meilen;
    private String antwort;

    public MPGTester2(double tanfangsbestand, double tachometerstand, double anzgallone, double meilen, String antwort) {
    }

    public static void main(String[] args) {

        double tanfangsbestand = 0.0;
        double tachometerstand;
        double anzgallone;
        String antwort = "";


        while (!antwort.equals("j")) {


            Scanner scan = new Scanner(System.in);

            System.out.println("Neues Auto - Tachometer-Anfangsstand: " + tanfangsbestand);

            System.out.println("Aktuellen Tachostand eingeben:");
            tachometerstand = scan.nextDouble();

            System.out.println("Anzahl der Gallonen für die Tankfüllung:");
            anzgallone = scan.nextDouble();




            double m = tachometerstand / anzgallone;

            System.out.println("Meilen pro Gallone: " + m);

            if (m < 15.0) {
                System.out.println("Autotyp: Benzinschwein!");
            } else if (m > 30.0) {
                System.out.println("Autotyp: Sparauto!");
            }



            System.out.println("Programm beenden? [j] [n]");
            antwort = scan.next();

            while (!antwort.equals("j") && !antwort.equals("n")) {
                System.out.println("Falsche Eingabe! Geben Sie es nochmal ein!");
                antwort = scan.next();


            }


        }

    }
}

hihihi :toll:
 

Neue Themen


Zurück
Oben