1

In this code I have an array with 5 elements and each element contains a value. Below in the while loop, I'm trying to give the user 3 attempts to guess what number is contained in the array (the user enters their guesses). My problem is that I don't know how to make match the user's guess (stored in a variable choose) with the array values caja[i] to print whether the user won or lost depending on a correct or incorrect guess respectively.

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--;
        }
    } 
}
8
  • So the user has to guess all 5 values or 1 value with the array containing it's digits? Commented Mar 21, 2016 at 14:31
  • What is the purpose of the variable 'o'? Commented Mar 21, 2016 at 14:33
  • @ Lagomorpph the user have 3 times to guess any of the array values Commented Mar 21, 2016 at 14:37
  • I'm not sure about what you want. Your game is ok if you want to get only the first value of the array everytime. If it is not what you want, explain it better. Commented Mar 21, 2016 at 14:38
  • 1
    Alex's solution should work perfectly for you then. Commented Mar 21, 2016 at 14:50

3 Answers 3

3

Your problem is that you break out of your loop in every case:

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

Instead of doing this (and printing the result after only the first comparison), you should have a Boolean indicating whether the number was found in the array:

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

This way, you can check the variable and tell the user whether he won or lost.

Sign up to request clarification or add additional context in comments.

Comments

1

Here are some additional suggestions. The answer by Alex (https://stackoverflow.com/a/36133864/6077352) is good. Please note the comments in the code.

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

Comments

0

So if the user can attempt to get any of the values of the array you might to change your while loop to this one:

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

1 Comment

if you want to finish the application after the first correct attempt just put a return after Ganaste message, of course inside braces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.