Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *This code generates a random number and gives the user 3 chances to guess the number
- *
- * @Molly Limaye
- * @09/05/19
- */
- import java.util.*;
- public class GuessingGame {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- //play again loop
- int play = 1;
- while (play == 1){
- //generate random number
- int num = (int)(Math.random() * ((10-1)+1))+1;
- //user guessing
- System.out.println("Guess a number between 1 and 10.");
- for(int i=0; i<3; i++) {
- int guess = sc.nextInt();
- //make sure guess is between 1 and 10
- if((guess>10)||(guess<1)){
- System.out.println("Please guess a number between 1 and 10.");
- i=0;
- guess = sc.nextInt();
- }
- //results of guess
- if (guess==num){
- System.out.println("Winner!");
- i=4;
- }
- else{
- System.out.println("Sorry, wrong.");
- if(i<2){
- System.out.println("Try again");
- }
- }
- }
- System.out.println("The answer was " + num + ".");
- //play again feature
- System.out.println("Would you like to play again? (1=yes/2=no)");
- int answer = sc.nextInt();
- if (answer==1){
- play = 1;
- }
- else{
- play = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement