将数组值与变量匹配

时间:2016-03-21 14:20:39

标签: java

在这段代码中,我有一个包含5个元素的数组,每个元素都包含一个值。在while循环中,我试图让用户3尝试猜测数组中包含的数字(用户输入他们的猜测)。我的问题是我不知道如何匹配用户的猜测(存储在变量choose中)与数组值caja[i]来打印用户输赢是否正确或不正确分别猜测。

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--;
        }
    } 
}

3 个答案:

答案 0 :(得分: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 :(得分:1)

以下是一些其他建议。亚历克斯(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();
        }
    }
}

答案 2 :(得分:0)

因此,如果用户可以尝试获取数组的任何值,您可以将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--;
    }