My project for my class is to create a Java-based game where the user must enter a number between 1-20 and a number between 250 and 300. The computer randomly chooses a number between those 2 numbers. Then the user has 10 guesses to correctly guess what number the computer is "thinking of."
There is a catch, though. We cannot use while
loops, only if
and else-if
statements. I have started this code and was wondering if I'm on the right track. Please point out anything that might help me!
package guessthenumber;
import java.util.Scanner;
import java.util.Random;
public class GuessTheNumber {
public static void main(String[] args)
{
int lowBoundary, highBoundary, secretNumber, boundaryDifference,
g1, g2, g3, g4, g5, g6, g7, g8,
g9, g10;
//g1, g2,... = guess 1, guess2,...
Scanner keyboard = new Scanner(System.in);
System.out.println("Can you guess the number I'm thinking of?\nLet's "
+ "see if you can guess the right number within 10 guesses.\n\n"
+ "First please enter a number between 1 and 20.");
lowBoundary = keyboard.nextInt();
System.out.println("Excellent! Next enter a number between 250 and "
+ "350.");
highBoundary = keyboard.nextInt();
boundaryDifference = highBoundary - lowBoundary;
Random randomNumber = new Random();
secretNumber = randomNumber.nextInt(boundaryDifference) + lowBoundary;
System.out.println("The secret number has been chosen. Now you must "
+ "guess\nthe secret number within 10 guesses or else you lose."
+ "\n(Hint: The secret number is between " + lowBoundary +
" and " + highBoundary + ".)");
g1 = keyboard.nextInt();
if (g1 == secretNumber) {
System.out.println("CONGRATULATIONS! YOU'VE CORRECTLY GUESSED\nTHE"
+ "SECRET NUMBER ON YOUR FIRST GUESS!!");
}
else if (g1 < secretNumber) {
System.out.println("Higher than " + g1 + ". Guess again!");
g2 = keyboard.nextInt();
{
if (g2 == secretNumber) {
System.out.println("Awesome! You've correctly guessed\nthe"
+ "secret number in 2 guesses!");
}
else if (g2 < secretNumber) {
System.out.println("Higher than " + g2 + ". Guess again!");
g3 = keyboard.nextInt();
if (g3 == secretNumber) {
System.out.println("Great job! You've correctly guessed\n"
+ "the secert number in 3 guesses!");
}
else if (g3 < secretNumber) {
System.out.println("Higher than " + g3 + ". Guess again!");
g4 = keyboard.nextInt();
if (g4 == secretNumber) {
System.out.println("Good job! You've correctly guessed\nthe"
+ " secret number in 4 guesses!");
}
else if (g4 < secretNumber) {
System.out.println("Higher than " + g4 + ". Guess again!");
g5 = keyboard.nextInt();
if (g5 == secretNumber) {
System.out.println("Not bad! You've correctly guessed\nthe"
+ " secret number in 5 guesses!");
}
else if (g4 > secretNumber) {
System.out.println("Lower than " + g4 + ". Guess again!");
}
}
}
else if (g3 > secretNumber) {
System.out.println("Lower than " + g3 + ". Guess again!");
}
}
else if (g2 > secretNumber) {
System.out.println("Lower than " + g2 + ". Guess again!"
);
g3 = keyboard.nextInt();
}
}
}
else if (g1 > secretNumber) {
System.out.println("Lower than" + g1 + ". Guess again!");
{
}
}
}
}