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?
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 = 4;
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.equals("summer");
}
else if(number == 2)
{
season.equals("spring");
}
else if(number == 3)
{
season.equals("fall");
}
else if(number == 4)
{
season.equals("winter");
}
System.out.print("Guess the season generated by the random number generator:");
userGuess = keyboard.nextLine();
if(userGuess.equalsIgnoreCase(season))
{
summerSeasonCounter += 1;
}
else if(userGuess.equalsIgnoreCase(season))
{
springSeasonCounter += 1;
}
else if(userGuess.equalsIgnoreCase(season))
{
fallSeasonCounter += 1;
}
else if(userGuess.equalsIgnoreCase(season))
{
winterSeasonCounter += 1;
}
count +=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.");
}
}
Aucun commentaire:
Enregistrer un commentaire