Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class guessingGame {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int game = 1;
- while (game==1) {
- int turns = 2;
- int num = (int)((Math.random()*10)+1);
- System.out.println("Guess a number 1-10: ");
- int guess = sc.nextInt();
- while(num!=guess&&turns>0) {
- System.out.println("You guessed wrong. Please guess again: ");
- guess = sc.nextInt();
- turns--;
- }
- if(num==guess) {
- System.out.println("You guessed correctly");
- }else {
- System.out.println("You didn't get the number in 3 guesses. You lose.");
- }
- System.out.println("Play again? (1 for yes, 2 for no) ");
- game = sc.nextInt();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement