2017-05-07 15 views
-1

私は私だけのJava端末上でいくつかの年齢を読み取るためにループしばらく使用したいクレイジー滞在Javaの単純な問題

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!"); 
} 

これで、それはそのメッセージを印刷しているため、エラーになり私のコード、!

出力

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

それはそのメッセージを印刷しているのはなぜ?、コードはwhileループに戻って取得する必要があり、私の中には、通常の中です!

+2

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

+0

だから申し訳ありませんが、私はその比較であると思っていませんでした! –

答えて

2

私はあなたがJavaの世界への初心者だと思います。 Javaでの文字列の比較は、equals(Object o)またはequalsIgnoreCase(String another)のメソッドを使用して行われ、==では行われません。あなたがしようとしていることを達成するための複数のより簡単で良い方法があります。 注:java.util.Scannerを使用してユーザーの入力を受け入れます。

ワン:あなたは値を格納するためのint配列を使用している場合 -

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(); 

2:あなたが年齢を保存するために、ArrayListようCollectionを使用している場合はを -

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(); 

私はこれがあなたの学習プロセスにおいてあなたを助けることを願っています。

+0

うん、それを試してみました!必要な修正を加えました。 –

+0

@DawoodibnKareemええ、私は最初にそれをチェックしていない!私は答えのセクションに直接書きました。それは私を打った!しかし、指摘してくれてありがとう!乾杯!! –

+0

あなたはまだ質問に答えていません - OPが実際に間違っていたもの。はい、あなたのコードは動作しますが、今のところ説明はありませんが、OPは本当にあまりそれから学ぶつもりはありません。 –

関連する問題