Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this is Michelle's random number program
- import java.util.*;
- public class randomNumber
- {
- public static void main (String [] args)
- {
- Random rand = new Random(); //generating random number
- int x = rand.nextInt(10)+1;
- Scanner prompt = new Scanner(System.in); //scanner prompting user to guess a number
- System.out.print("Guess a number from 1-10. ");
- int y = prompt.nextInt();
- if (y>10 || y<1) //error message if guess is not between 1-10
- {
- System.out.print("Error! Not 1-10.");
- }
- else
- {
- if (y==x) //congratulations if guess=random number
- {
- System.out.print("Congratulations! You guessed the number!");
- }
- else
- {
- System.out.print("Sorry, the number was " + x + ".");
- if(y>x+3 || y<x-3) //if guess is 3 away from random number
- {
- System.out.print("You missed by a mile!");
- }
- else if (y!=x && y>10 && y<1) //if guess does not equal random number
- {
- System.out.print("You were close!");
- }
- System.out.print("Better luck next time :(");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment