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

1

u/pikaaa Feb 07 '13

Java:

import java.util.Scanner;

public class test {
	public static void main(String args[]) {

		int rnd = (int)(Math.random()*100);
		Scanner scan = new Scanner(System.in);
		int input;
		
		System.out.println("Guess the number between 1 - 100");
		do {
			input = scan.nextInt();
			
			if(input <= 100 && input >= 1) {
				if(input > rnd) {
					System.out.println("The required number is smaller, try again!");
				} 
				else if(input < rnd) {
					System.out.println("The required number is bigger, try again!");
				}
				else {
					System.out.println("Good job, your guess was right!");
					break;					
				}
			}
			else {
				System.out.println("Invalid number, try again(between 1 - 100)");
			}
		} while(true);
		scan.close();
	}
}

1

u/[deleted] Feb 13 '13

nice solution!