Home > Enterprise >  Why are my variables not totaling correctly in this Java program?
Why are my variables not totaling correctly in this Java program?

Time:04-01

I'm working on an assignment that requires me to write a program that allows the user to guess a color, then generates a random color to test their prediction. At the end of the program, the user must be notified how many correct/incorrect guesses they had. Problem is, my accumulators for the correct and incorrect amounts are not adding up correctly. The totals seem to be random, but they're obviously counting something - I'm just not sure what. I think it's an issue with the switch statement, but I'm not sure what I should do to fix it. Thank you for your time!

import java.util.Scanner;
import java.util.Random;

public class ESP 

{
    public static void main(String[] args)
    {
        String colorChoice;
        int correct = 0, incorrect = 0;

        Scanner keyboard = new Scanner(System.in);

        for(int i = 1; i <= 10; i  )
        {    
            System.out.print("Choose either red, green, blue, orange or yellow! ");
            colorChoice = keyboard.nextLine();

            System.out.println("You chose: "   colorChoice);
            System.out.println("The computer chose: "   computerColorChoice());

            if(colorChoice.equalsIgnoreCase(computerColorChoice()))
                {
                    correct  ;
                }

            if(!colorChoice.equalsIgnoreCase(computerColorChoice()))
                {
                    incorrect  ;
                }
        
        }

        System.out.println("Amount of times you guessed correctly: "   correct);
        System.out.println("Amount of times you guess incorrectly: "   incorrect);
    }
   
  
    public static String computerColorChoice()
    {
        String randomColor;
        int randomNumber;

        Random random = new Random();
        randomNumber = random.nextInt(4);

        switch (randomNumber)
        {
            case 0: 
                randomColor = "Red";
                break;
            case 1:
                randomColor = "Green";
                break;
            case 2:
                randomColor = "Blue";
                break;
            case 3:
                randomColor = "Orange";
                break;
            case 4:
                randomColor = "Yellow";
                break;
            default:
                randomColor = " ";      
        }
        return randomColor;
    }
    
}

CodePudding user response:

You are calling computerColorChoice() three times in each iteration and every time it could result in a different value.
Instead you should only call it once and save the value in a variable i.e.

for(int i = 1; i <= 10; i  ){    
    colorChoice = keyboard.nextLine();
    String compChoice = computerColorChoice();
    if(colorChoice.equalsIgnoreCase(compChoice)){
        correct  ;
    }
    else{
        incorrect  ;
    }     
}

CodePudding user response:

Each time you call computerColorChoice() the function may return a different color, so you are not really checking what the computer chose but a random color everytime.

        System.out.println("You chose: "   colorChoice);
        System.out.println("The computer chose: "   computerColorChoice());
        if(colorChoice.equalsIgnoreCase(computerColorChoice()))
            {
                correct  ;
            }

        if(!colorChoice.equalsIgnoreCase(computerColorChoice()))
            {
                incorrect  ;
            }

So you should store the computer's choice and use the stored one to check

        System.out.println("You chose: "   colorChoice);
        String computerChoice= computerColorChoice();
        System.out.println("The computer chose: "   computerChoice);

        if(colorChoice.equalsIgnoreCase(computerChoice)) {
                correct  ;
        } else {
                incorrect  ;
        }

CodePudding user response:

You could help us a bit more here by explaining how they are random, or if not, at least include an output of the elements of the program relevant to the problem.

You only expect ONE color to be generated by the computer, right? However, you called the computerColorChoice() method 3 times during each iteration (each round of your game), resulting in a different value every time you call it, and as a result, your checks between the user input and the intended answer of the round is changed during the round every time.

You can fix this by making sure to have only 1 color generated by the computer in every round, and you can use a variable for that. The fixed code can be:

// For loop index counters should always start at 0
for (int i = 0; i < 10; i  ) {
    String userChoice = keyboard.nextLine();
    String computerChoice = computerColorChoice();
    if (userChoice.equalsIgnoreCase(computerChoice)) {
        correct  ;
    } else {
        incorrect  ;
    }
}

An extra tip would be to instead of creating an extra variable to hold the incorrect answers, simply don't and calculate it. Since you know there will be 10 rounds, and a round can only be correct or wrong, you can simply do:

int incorrect = 10 - correct;

And by the way, nextInt(4) will generate a random integer BELOW 4. You might notice that the case 4: is never being reached. Change the argument from 4 to 5 and you should be Gucci.

  • Related