- import java.util.Scanner;
- import java.util.Random;
-
- public class Elev
- {
- static Scanner input = new Scanner(System.in);
- static Random random = new Random();
- static boolean play = true;
-
- public static void main(String args[])
- {
-
- while(play)
- {
- runGame();
- }
-
- }
-
- public static void runGame()
- {
- int randomNumber = randomNumber();
- System.out.print("Guess numbers 1 - 10 : ");
- int userInput = input.nextInt();
-
- if(userInput==randomNumber)
- {
- System.out.println("\nYou are correct!\nI was thinking of " + randomNumber+"\n");
- playAgain();
- }
- else
- {
- System.out.println("Wrong! I was thinking of " + randomNumber);
- playAgain();
- }
- }
-
- public static int randomNumber()
- {
- int a = random.nextInt(10);
- return a;
- }
-
- public static void playAgain()
- {
- System.out.print("Play again? (Y/N): ");
- input = new Scanner(System.in);
- String playAgainInput = input.nextLine();
-
- if(playAgainInput.equalsIgnoreCase("n"))
- {
- System.out.println("Thanks for playing!");
- play = false;
- }
- else if(playAgainInput.equalsIgnoreCase("y"))
- {
- play = true;
- }
- }
- }
Jeg ville mene, at denne kode burde virke, som du gerne vil have den til