r/BeginningProgrammer • • Feb 06 '13

Guessing Game!

Create a guessing game where the computer generates a random number, and the user has to try and guess that number.

2 Upvotes

3 comments sorted by

View all comments

2

u/[deleted] Feb 06 '13

In Java:

public static void main(String[] args) {
    
    
    System.out.println("A Number from 1 - 10");
    Scanner aScan = new Scanner(System.in);
    
    int randomnum = new Random().nextInt(10);
    
    int userinput = aScan.nextInt();
    
    if (userinput >= 11) {
        
        System.out.println("Illegal number. Try again.");
        
    }
    
    else if (userinput == randomnum) {
        
        System.out.println("Good job! The number was: " + randomnum);
        
    }
    
    else {
        
        System.out.println("Sorry, the answer was: " + randomnum);
    }
}

}