Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class guessingGame {
  3.  
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int game = 1;
  7. while (game==1) {
  8. int turns = 2;
  9. int num = (int)((Math.random()*10)+1);
  10. System.out.println("Guess a number 1-10: ");
  11. int guess = sc.nextInt();
  12. while(num!=guess&&turns>0) {
  13. System.out.println("You guessed wrong. Please guess again: ");
  14. guess = sc.nextInt();
  15. turns--;
  16. }
  17. if(num==guess) {
  18. System.out.println("You guessed correctly");
  19. }else {
  20. System.out.println("You didn't get the number in 3 guesses. You lose.");
  21. }
  22. System.out.println("Play again? (1 for yes, 2 for no) ");
  23. game = sc.nextInt();
  24. }
  25.  
  26. }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement