Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package numberGuesser;
- import java.util.concurrent.ThreadLocalRandom;
- import java.util.Scanner;
- public class Guesser {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int yes = 1;
- while (true) {
- // creates a random number each time the loop is restarted
- int randomNum = ThreadLocalRandom.current().nextInt(0, 10 + 1);
- //lets the user guess the number
- Scanner input = new Scanner(System.in);
- try {
- System.out.println("Take a Guess to what my number is: ");
- String Guess = input.next();
- //converts the number to an integer
- int Guess_int = Integer.parseInt(Guess);
- }
- catch(NumberFormatException ex) {
- System.out.print("there was an error");
- }
- // checks if the numbers are the same
- if (Guess_int == randomNum) {
- //runs when the answer is correct
- System.out.println("correct");
- System.out.println("do you want to play again, answer 1 if yes or 0 if no: ");
- String answer = input.next();
- int answer_int = Integer.parseInt(answer);
- //checks if the answer user wants to go again
- if(answer_int == yes) {
- // runs when the user answers yes or 1
- System.out.println("ok, lets go again");
- System.out.println("");
- System.out.println("");
- continue;
- }
- else{
- // runs when the user answers anything other than yes or 1
- System.out.println("ok, goodbye");
- input.close();
- break;
- }
- }
- else{
- // runs when the answer was wrong
- System.out.println("wrong, my number was: " + randomNum );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement