2016-03-21 10 views
1

このコードでは、5つの要素を持つ配列があり、各要素には値が含まれています。下のwhileループでは、配列に何が含まれているのかを推測する3回の試みをユーザーに与えようとしています(ユーザーは推測を入力します)。私の問題は、配列の値がcaja[i]のユーザーの推測(変数chooseに格納されている)と一致させる方法がわからないということです。配列値と変数の一致

public static void main(String[] args) 
{ 
    int[] caja = new int [5]; 

    caja[0] = 1; 
    caja[1] = 3; 
    caja[2] = 5; 
    caja[3] = 7; 
    caja[4] = 9; 

    System.out.println("Mostrando todos los numeros del arreglo"); 
    for (int i = 0; i < caja.length; i++) 
    { 
     System.out.println(caja[i]); 
    } 
    System.out.println(); 

    int electionGame = 3; 
    int o = 0; 
    while(electionGame > 0) 
    {    
     Scanner sc = new Scanner(System.in); 
     int choose = sc.nextInt(); 
     for (int i = 0; i < caja.length; i++) 
     { 
      o = o + 1; 
      if(choose == caja[i]) 
      { 
       System.out.println("Ganastes"); 
       break; 
      } 
      else 
      { 
       System.out.println("Perdiestes"); 
       break; 
      } 
     } 
     electionGame--; 
     } 
    } 
} 
+0

ので、ユーザは、すべての5つの値または1を推測していますその数字を含む配列の値? – ZbyszekKr

+0

変数 'o'の目的は何ですか? –

+0

@ Lagomorpphユーザーは3回アレイの値を推測することができます –

答えて

3

あなたの問題はあなたがすべてのケースでは、あなたのループから抜け出すことです:

if(choose == caja[i]) 
{ 
    System.out.println("Ganastes"); 
    break; 
} 
else 
{ 
    System.out.println("Perdiestes"); 
    break; 
} 

代わりの(最初の比較の後、結果を印刷する)これを行うには、示すブールを持っている必要があります

int electionGame = 3; 
boolean found = false; //indicates whether user has found right number 
while (electionGame > 0 && !found) { 
    Scanner sc = new Scanner(System.in); 
    int choose = sc.nextInt(); 
    for (int i = 0; i < caja.length && !found; i++) { 
     if (choose == caja[i]) { 
      found = true; 
     } 
    } 
    electionGame--; 
    if (found) { 
     System.out.println("you won"); 
    } else { 
     System.out.println("nope"); 
    } 
} 

このようにして、変数をチェックして、勝ちか迷子かをユーザーに伝えることができます。

1

ここにいくつかの追加の提案があります。 Alex(https://stackoverflow.com/a/36133864/6077352)の答えは良いです。コード内のコメントにご注意ください。

import java.util.Scanner; 

/** https://stackoverflow.com/q/36133524/6077352 */ 
public class GuessNumber { 
    public static void main(String[] args) { 

     int[] caja = new int[5]; 
     caja[0] = 1; 
     caja[1] = 3; 
     caja[2] = 5; 
     caja[3] = 7; 
     caja[4] = 9; 

     System.out.println("Mostrando todos los numeros del arreglo"); 
     for (int i = 0; i < caja.length; i++) { 
      System.out.println(caja[i]); 
     } 
     System.out.println(); 

     int tries = 3; 
     Scanner sc = new Scanner(System.in);// no need to create scanners in loop 
     while (tries-- > 0) {// count down tries 
      System.out.print("Please guess a number... "); 
      int guess = sc.nextInt(); 
      boolean win = false;// flag to determine if won 
      for (int i : caja) {// iterate through array and check if guess is inside 
       win |= (i == guess);// when inside: win=true 
      } 
      System.out.println("Answer right? " + win); 
      System.out.println(); 
     } 
    } 
} 
0

したがって、ユーザーはこの1つのあなたのwhileループを変更するためにあなたがかもしれない配列の値のいずれかを取得しようとすることができた場合:

while (electionGame > 0) { 
     Scanner sc = new Scanner(System.in); 
     int choose = sc.nextInt(); 
     boolean ganaste = false; 
     for (int i = 0; i < caja.length; i++) { 
      o = o + 1; 
      if (choose == caja[i]) { 
       ganaste = true; 
       break; 
      } 
     } 

     if (ganaste) 
      System.out.println("Ganastes"); 
     else 
      System.out.println("Perdiestes"); 

     electionGame--; 
    } 
+0

最初の正しい試行の後にアプリケーションを終了したい場合は、 'Ganaste'メッセージの後に' return'を入れてください。もちろん、中括弧の中に入れてください。 –

関連する問題