Tutorials › Java Projects › Java Number Guessing Game
Project Overview
Create a number guessing game using Random, loops, conditions, and user input.
This beginner Java project helps you practice real coding skills with a small program you can run, edit, and improve.
Java Code
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Random random = new Random();
Scanner input = new Scanner(System.in);
int secret = random.nextInt(10) + 1;
int guess = 0;
while (guess != secret) {
System.out.print("Guess 1 to 10: ");
guess = input.nextInt();
if (guess < secret) System.out.println("Too low");
else if (guess > secret) System.out.println("Too high");
}
System.out.println("Correct!");
input.close();
}
}How to Run This Project
- Install the Java JDK.
- Create a new file. Use the public class name as the file name.
- Paste the code into the file.
- Compile it using javac FileName.java.
- Run it using java FileName.
What You Practice
- Java syntax
- Variables and data types
- Conditions and loops
- Methods, classes, or objects depending on the project
- Problem solving
Project Improvements
- Add input validation.
- Add better menus and messages.
- Use classes and objects for better structure.
- Save data to a file.
- Convert the project into a GUI app later.
Related Java Lessons
Java Tutorial | Methods | Classes | Objects | Collections