Reputation: 3
I am currently completing my first java course and assignment based on a random number game. I am having some issues with some if greater and less statements.
I would really appreciate some general comments.
The reason I am looking to input the 'random' number is for testing purposes
I also need to incorporate get()
and set()
methods as the assignment entails object oriented programming so I need to use multiple classes
Any advice would be highly appreciated
The problem I'm having is that the last if statement which checks random number is in range is always getting the output that it is out of range regardless
package randomnumbergame;
/*
* @author Matthew O
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
int numGuesses = 3;
int rangeMin = 1;
int rangeMax = 10;
int randomNum = 0;
Scanner scan = new Scanner (System.in);
System.out.println("Welcome to the random number game!");
System.out.println("The object of this game is to correctly "
+"guess a number between a set range within a certain ammount of guesses.");
System.out.println("\nThe default range is " + rangeMin + " - " + rangeMax);
System.out.println("Would you like to change this?");
System.out.println("Please enter y for yes or n for no and press enter.");
String range = scan.nextLine();
char changeRange = range.charAt(0);
if (! (changeRange == 'Y' || changeRange == 'y'|| changeRange == 'n' || changeRange == 'N'))
{
System.out.println("Error: invalid input entered for the interstate question");
System.exit(0);
}
if (changeRange == 'Y' || changeRange == 'y')
{
System.out.println ("\nPlease enter the new numerical minimum value for the range");
int newRangeMin = scan.nextInt();
rangeMin = newRangeMin;
System.out.println ("\nPlease enter the new maximum numerical value for the range");
int newRangeMax = scan.nextInt();
rangeMax = newRangeMax;
}
{
System.out.println("\nThe default number of guesses is " + numGuesses);
System.out.println("Would you like to change this?");
System.out.println("\nPlease enter y for yes or n for no and press enter.");
String guesses = scan.next();
char changeGuesses = guesses.charAt(0);
if (! (changeGuesses == 'Y' || changeGuesses == 'y' || changeGuesses == 'n' || changeGuesses
== 'N'))
{
System.out.println("Error: invalid input entered for the interstate question");
System.exit(0);
}
if (changeGuesses == 'Y' || changeGuesses == 'y')
{
System.out.println("\nPlease enter the new number of permitted guesses");
int newGuesses = scan.nextInt();
numGuesses = newGuesses;
}
{
System.out.println ("\n\n\n\n\n\n\n\n\n");
System.out.println("Welcome to the random number game!\n");
System.out.println("You have chosen a range of " + rangeMin + " to " + rangeMax);
System.out.println("You have chosen to have " + numGuesses + " Guesses");
}
}
{
System.out.println("\nPlease press y to input the random number or n" +
" to let the number be automatically generated");
String random = scan.next();
char changeRandom = random.charAt(0);
if (! (changeRandom == 'Y' || changeRandom == 'y' || changeRandom == 'n' ||
changeRandom == 'N'))
{
System.out.println("Error: invalid input entered for the interstate question");
System.exit(0);
}
if (changeRandom == 'Y' || changeRandom == 'y')
{
System.out.println("\nPlease enter the new 'random' number between "
+ rangeMin + " and " + rangeMax);
int inputRandom = scan.nextInt();
randomNum = inputRandom;
System.out.println("\nThe 'random' number chosen for the game is " + randomNum);
}
if (randomNum < rangeMin);
{
System.out.println("Random number is out of range!");
}
}
}
}
Upvotes: 0
Views: 718
Reputation: 38424
if (randomNum < rangeMin);
{
System.out.println("Random number is out of range!");
}
The problem is in the ;
(semicolon) on the first line. Java thinks it is the end of that if
condition and prints your error message every time. The statement then says:
"If (randomNum < rangeMin), do nothing. Then print out 'Random number is out of range!'."
Upvotes: 3
Reputation: 10789
Just remove ;
symbol after if.
if (randomNum < rangeMin);
{
System.out.println("Random number is out of range!");
}
Upvotes: 1
Reputation: 15052
You have an unnecessary ;
at the end of the if
statement in question, so even if it is evaluated, the result is nothing.
if (randomNum < rangeMin);
{
System.out.println("Random number is out of range!");
}
No matter how the if
is evaluated, the message will always be printed.
Change the above to:
if (randomNum < rangeMin)
{
System.out.println("Random number is out of range!");
}
Upvotes: 4