Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class RandomAndLoops {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- Random random = new Random();
- int guess, randNum;
- char yn;
- while (true) { // main game loop
- randNum = random.nextInt(41);
- System.out.println("I'm thinking of a number from 0-40. What is it? ");
- while (true) { // guess loop
- guess = Integer.parseInt(input.nextLine());
- if (guess < randNum)
- System.out.println("Too low, try again: ");
- else if (guess > randNum)
- System.out.println("Too high, try again: ");
- else {
- System.out.println("You have guessed the correct number!");
- break; // exit guess loop
- }
- }
- System.out.println("Play again? (y/n) ");
- yn = input.nextLine().toLowerCase().charAt(0);
- if (yn != 'y') break; // exit main game loop
- }
- input.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement