-10

So for my java program, I am required to write a while loop with an if else statement that allows one to enter in a guess for the season, which is randomly generated by assigning a season to a number generated from 1 to 4. When I compile I get no errors, but when I execute the program, I get zero for all guesses. Is there something I'm missing in the number generation? Or is it in the string comparison?

EDIT: The code is working as intended now. I understood what it was that I did wrong. Thank you to those who helped.

Here is the code and the variables needed:

public class SeasonsGenerator {
     public static void main(String[] args){

        //Variables for seasons counter 
        int summerSeasonCounter=0;
        int fallSeasonCounter=0;
        int springSeasonCounter=0;
        int winterSeasonCounter=0;
        int number;
        int limit = 5;
        Scanner keyboard = new Scanner(System.in);

        int count = 0;       // Counter for total number of iterations or loops
        String season = "";//Variable for season    
        final int NUMBER = 15;  // Number of while loop iterations
        String userGuess;//save user guess here


       // A random number generator
       Random generator = new Random();

       while(count < NUMBER)
       {
         number = generator.nextInt(limit);
         if(number == 1)
         {
          season = "summer";
         }
        else if(number == 2)
         {
          season = "spring";
         }
         else if(number == 3)
        {
          season = "fall";
        }
         else if(number == 4)
        {
         season = "winter";
        }

        System.out.print("Guess the season generated by the random number generator:");
        userGuess = keyboard.nextLine();
        userGuess.equalsIgnoreCase(season);

        if(season == "summer")
         {
        summerSeasonCounter +=1;
        }
        else if(season == "spring")
        {
          springSeasonCounter += 1;
        }
        else if(season == "fall")
        {
          fallSeasonCounter += 1;
        }
        else if(season == "winter")
        {
          winterSeasonCounter += 1;
        }


    // Display the results
       System.out.println ("You guessed Summer season correctly     "+summerSeasonCounter+" number of times.");
       System.out.println ("You guessed Fall season correctly "+fallSeasonCounter+" number of times.");
       System.out.println ("You guessed Spring season correctly "+springSeasonCounter+" number of times.");
       System.out.println ("You guessed Winter season correctly "+winterSeasonCounter+" number of times.");

    }
}
  • http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range this will show you how to create a random number in a range – RAZ_Muh_Taz Oct 20 '16 at 20:18
  • Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you still have issues afterwards, please feel free to come back with more details. – Joe C Oct 20 '16 at 20:32

1 Answers1

2

You have a number of problems, and I suggest you run you code through your debugger to help you find bugs. Some of the obvious errors are

  • nextInt(4) returns 0 to 3.
  • season.equals("summer"); compares the season to "summer" and return false. It doesn't assign anything.
  • you test if (userGuess.equalsIgnoreCase(season)) four times. If it is false the first time, it will be false all four times.

Your immediate problem is that to assigned a variable you need to use =

season = "summer";
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130