Reputation: 3
So I am trying to replicate this game I've played before. In this game you are shown a number and there is a hidden number. You need to guess wether this hidden number is smaller, bigger, or the same as the number that is show. I am having issues getting the input to work. I can't seeem to get the switch statement to work. I am also having issues with the scanner. While it is on the outside of the while loop it works, but inside it doesn't.
import java.util.Scanner;
import java.util.Random;
public class Jamey {
/**
* @param args
*/
public static void main(String[] args) {
//This will give us the first random shown number
Random yourRandom = new Random();
int y = 1+yourRandom.nextInt(10);
//Here is the introduction text
System.out.println("Welcome to the guessing game!");
System.out.println("The objective of this game is simple.");
System.out.println("You will be shown one of two numbers which range between one and ten.");
System.out.println("You have to gues if the number shown is larger, smaller, or equal to the hidden number.");
System.out.println("If you believe the number you see is larger enter 1.");
System.out.println("If you believe the number you see is smaller enter the 3.");
System.out.println("If you believe the number you see is the same enter the 2.");
System.out.println("Good luck, your number is "+y+".");
boolean isDone = false;
while(isDone=false){
//This allows the user to guess
Scanner guess = new Scanner(System.in);
int g = guess.nextInt();
//This will help us to keep score later.
int score = 0;
//This will give us the new random number
Random newRandom = new Random();
int n = 1+newRandom.nextInt(10);
//This will give us the new hidden number
Random hiddenRandom = new Random();
int r = 1+hiddenRandom.nextInt(10);
//This is to allow multiple different inputs
switch(score){
case 1 :
score +=1;
if(y>r){
System.out.println("Correct");
System.out.println("Your new number is "+n+".");
}
else{
score +=1;
System.out.println("Inccorect, your overall score was "+score+".");
isDone = true;
}
break;
case 2 :
score +=1;
if(y==r){
System.out.println("Correct");
System.out.println("Your new number is "+n+".");
}
else{
System.out.println("Inccorect, your overall score was "+score+".");
isDone = true;
}
break;
case 3 :
score +=1;
if(y<r){
System.out.println("Correct");
System.out.println("Your new number is "+n+".");
}
else{
System.out.println("Inccorect, your overall score was "+score+".");
isDone = true;
}
break;
default:
System.out.println("Invalid input.");
isDone = true;
break;
}
}
}
}
Upvotes: 0
Views: 90
Reputation: 3442
Your while statement is using assignment
while(isDone=false) // Incorrect
while (isDone == false) OR while(!isDone) // Better
Notice the ==
instead of just =
. You weren't doing comparison, only setting isDone
to false every iteration.
Your switch statement is failing because you are checking your score
variable instead of the guess one.
switch(score) // Incorrect
switch(g) // Better
Also, you are creating lots of Random
objects when you only need to create one single instance, then name each variable accordingly. For example
Random rand = new Random();
int yourRandom = 1 + rand.nextInt(10);
int newRandom = 1 + rand.nextInt(10);
int hiddenRandom = 1 + rand.nextInt(10);
Upvotes: 1
Reputation: 66
while(isDone=false){
should be
while(isDone==false){
or even better
while(!isDone){
Upvotes: 0