2017-05-07 6 views
-1

ich bin bleiben verrückt Ich möchte nur die Schleife während verwenden, um einige Alters auf Java TerminalJava Einfache Ausgabe

public static void main(String[] args){ 

    Scanner keyboard = new Scanner(System.in); 

    int lenghtMax = 100; 

    int[] age = new int[lenghtMax]; 
    int status = 1; 
    String next; 
    int i = 0; 

    while(status == 1){ 

     if (i >= 100){ 

      System.out.println("You cant register more ages!\n"); 

     } else {     

      System.out.println("What the age?"); 
      age[i] = keyboard.nextInt(); 
      i++; 

      System.out.println("Do you want register other age? y/n"); 
      next = keyboard.next(); 

      if (next == "y"){ 

       status = 1; 

      } else { 

       status = 0; 
       keyboard.close(); 

      } 

     } 

    } 

    System.out.println("Passing Here!"); 
} 

Dies ist mein Code zu lesen, wo der Fehler ist, weil es die Nachricht drucken!

Ausgabe

What the age? 
45 
Do you want register other age? y/n 
y 
Passing Here! 

Warum ist es, dass die Nachricht Druck ?, sollte der Code wieder auf der while-Schleife erhalten, ist mein, während ein normaler Weile!

+2

'next.equals (" y ")' – Newbie

+0

Sorry, ich hatte keine Ahnung, das war die Vergleichbarkeit! –

Antwort

2

Ich schätze, Sie sind ein Neuling in Java-Welt. String Vergleich in Java sollte mit equals(Object o) oder equalsIgnoreCase(String another) Methoden durchgeführt werden und nicht von ==. Es gibt mehrere einfachere und bessere Möglichkeiten, um das zu erreichen, was Sie zu tun versuchen. Hinweis: Verwenden Sie java.util.Scanner, um Benutzereingaben zu akzeptieren.

One: Wenn Sie int Array verwenden Werte zu speichern -

int lengthMax = 100; 
int[] ages = new int[lengthMax]; 
Scanner sc = new Scanner(System.in); 
     /* 
     * Just iterate through the length of the array 
     */ 
     for(int i = 0; i < lengthMax; i++) { 
      System.out.println("What is the age?"); 
      /* 
      * Read the input and add the same to array 
      * at position 'i'. 
      */ 
      ages[i] = Integer.parseInt(sc.nextLine()); 

      /* 
      * Read the choice form the user 
      */ 
      System.out.println("Do you want to register more age? y/n"); 
      String choice = sc.nextLine(); 
      /* 
      * If choice is a YES - y 
      * simply continue with the loop 
      */ 
      if(choice.equals("y")){ 
       continue; 
      } else { 
      // If choice is anything other than 'y', break out 
       break; 
      } 
     } 
//close the Scanner 
sc.close(); 

Zwei: Wenn Sie ein Collection wie ArrayList, verwenden Sie das Alter speichern -

int lengthMax = 100; 
List<Integer> agesList = new ArrayList<>(); 
Scanner sc = new Scanner(System.in); 
/* 
* Create an infinite loop 
*/ 
while(true) { 
      /* 
      * At each run, check if the size of the ArrayList is 
      * equal to the max length of ages defined. 
      * 
      * If yes, simply print your message and break out. 
      */ 
      if(agesList.size() == lengthMax) { 
       System.out.println("You can't register more ages!!"); 
       break; 
      } 

      /* 
      * Start accepting the ages, and add each to the list. 
      */ 
      System.out.println("What is the age?"); 
      agesList.add(Integer.parseInt(sc.nextLine())); 

      /* 
      * Ask if user is still interested to register ages. 
      */ 
      System.out.println("Do you want to register more age? y/n"); 
      String choice = sc.nextLine(); 

      // If yes, continue with the loop, else simply break out. 
      if(choice.equals("y")){ 
       continue; 
      } else { 
       break; 
      } 
     } 
sc.close(); 

Ich hoffe, das hilft dir in deinem Lernprozess.

+0

Yup, versuchte es !! Hat die notwendigen Korrekturen vorgenommen. –

+0

@DawoodibnKareem Ja, ich weiß, ich hatte es zum ersten Mal nicht überprüft !! Ich habe es direkt in die Antwort geschrieben. Dann ist es mir aufgefallen !! Danke für das Aufzeigen !! Prost!! –

+0

Sie haben die Frage immer noch nicht beantwortet - was das OP eigentlich falsch gemacht hat. Ja, Ihr Code funktioniert jetzt, aber ohne irgendeine Erklärung wird OP nicht wirklich zu viel davon lernen. –