Practice Day: Build a number guessing game or a task list

Practice Java basics by building a number guessing game or a simple task list manager. Day 13 Java tutorial for beginners.
Practice Day: Build a number guessing game or a task list

On this practice day, you will apply variables, loops, conditionals, arrays, and methods by building small projects. Choose one challenge or try both for better practice.

1. Number Guessing Game

This program generates a random number and asks the user to guess it. It uses loops, conditionals, and user input.


import java.util.Scanner;
import java.util.Random;

public class NumberGuessingGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        int numberToGuess = random.nextInt(100) + 1; // random number 1-100
        int guess;
        int attempts = 0;

        System.out.println("Guess a number between 1 and 100:");

        do {
            guess = scanner.nextInt();
            attempts++;

            if (guess > numberToGuess) {
                System.out.println("Too high! Try again.");
            } else if (guess < numberToGuess) {
                System.out.println("Too low! Try again.");
            } else {
                System.out.println("Correct! You guessed it in " + attempts + " attempts.");
            }
        } while (guess != numberToGuess);
    }
}

2. Task List Manager

This simple program lets the user add tasks and view them. It uses arrays, methods, and loops.


import java.util.Scanner;

public class TaskList {
    static String[] tasks = new String[10];
    static int count = 0;

    static void addTask(String task) {
        if (count < tasks.length) {
            tasks[count] = task;
            count++;
            System.out.println("Task added.");
        } else {
            System.out.println("Task list full!");
        }
    }

    static void showTasks() {
        System.out.println("Your tasks:");
        for (int i = 0; i < count; i++) {
            System.out.println((i + 1) + ". " + tasks[i]);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            System.out.println("\n1. Add Task\n2. Show Tasks\n3. Exit");
            choice = scanner.nextInt();
            scanner.nextLine(); // clear buffer

            switch (choice) {
                case 1:
                    System.out.print("Enter task: ");
                    String task = scanner.nextLine();
                    addTask(task);
                    break;
                case 2:
                    showTasks();
                    break;
                case 3:
                    System.out.println("Goodbye!");
                    break;
                default:
                    System.out.println("Invalid choice.");
            }
        } while (choice != 3);
    }
}

Summary

Both exercises use multiple concepts: loops, arrays, conditionals, methods, and user input. The guessing game builds logic skills, while the task list improves array and method practice.

Post a Comment