0

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++;

        }


        }
    }
}

}

Gen Miao
  • 1
  • 2

1 Answers1

0

You can use netx() instead nextInt(), next() will return a String object, you can parse that String to int using Integer.valueOf(stringObject), this method can return a NumberFormatException when this stringObject is not an int value.

For example:

Scanner s = new Scanner(System.in);
    String stringValue = s.next();
    int value = 0;
    try {
        value = Integer.valueOf(stringValue);
    } catch (NumberFormatException e) {
        //Code to manage this case.
    }

Edit: I updated the code, I forgot to include value within the try block

Manuel Perez
  • 37
  • 1
  • 4
  • 5
  • So where should i put it in the code? after the while loop or before it? – Gen Miao Apr 24 '17 at 22:12
  • Before the loop, becuase you need to parse and check the String before to make any validation. – Manuel Perez Apr 24 '17 at 22:25
  • Can you put it in my code? i tried but it turns out that after prompting for an input, the code won't the input given. – Gen Miao Apr 24 '17 at 22:37
  • Just use String stringValue = s.next(); int guess = 0; try { guess = Integer.valueOf(stringValue); } catch (NumberFormatException e) { //Code to manage this case. } instead of: guess = scanner.nextInt(); – Manuel Perez Apr 24 '17 at 22:56
  • It tried, now what happend is that if the input is not an integer ex :hello, the code will print : please enter an integer. If an integer is entered afterwards, it runs without any issues. But the problem now is that if the first input is an integer, the program doesn't do anything despite pressing enter multiple times. – Gen Miao Apr 24 '17 at 23:03
  • You need to create a code to handle this kind of cases and let know to the user that was a mistake, try to use the OOP advantage. – Manuel Perez Apr 25 '17 at 11:22