This is my code , first i want to add something to check if the user input is an integer at the beginning if the input is not an integer, the program must prompt for another input. Second is to make a yes/no option in the end to decided whether to rerun the "game" (the program itself especially the while loop). I am really into basic basic level programming so any tips would be appreciated!
import java.util.Random;
import java.util.Scanner;
public class Numberguessinggame2
public static void main(String[] args) {
//random number generator
Random rand = new Random();
int round = 1;
System.out.println("Let's play a number guessing game.");
System.out.println("Ill pick a number between 1 and 100,");
System.out.println("and you try to guess it in 7 or fewer tries.");
System.out.println(" ");
System.out.println("Round " + round);
System.out.println("-----");
//acquire input from user
Scanner scanner = new Scanner(System.in);
//generate a number between 1 and 99
int number = rand.nextInt(99) + 1;
int guess = -1;
int counter = 0;
//prompt user for first guess
System.out.print("What is your first guess? ");
//loop until user guesses the right number
while (guess != number) {
guess = scanner.nextInt();
counter++;
//loop until user gives 7 guesses
if (counter>=7){
System.out.println("You didn't get the number in 7 guesses.");
System.out.println("I win! My number was : " + number);
break; }
if (guess<number) {
//guess is too low
System.out.print("That's too low.Try again: ");
if (guess<number && counter == 6) {
//guess is too low and has reacher the Last chance
System.out.print("... Last chance!");
}
}if (guess>number) {
//guess is too high
System.out.print("That's too high.Try again: ");
if (guess>number && counter == 6) {
//guess is too high and has reached its Last chance
System.out.print("... Last chance!");
}
if (guess==number) {
//if the guess is correct
System.out.println("You got it in " + counter + " guesses.");
System.out.println("You win! My number was : " + number);
round++;
}
}
}
}
}