Android Development 02 Challenge 2

发布于 2023-09-23  69 次阅读


参考视频:Android Development for Beginners - Full Course

  一个简单的猜数游戏:

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

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

    /* Username */
        System.out.print("Input Username: ");

        String username = new Scanner(System.in).next();

        System.out.println("Welcome " + username + ", we'll generate a random integer in [0, 100].");

    /* Generate Random Number */
        Random rnd = new Random();
        int target = rnd.nextInt(100);

    /* Loop */
        boolean loop_flag = true;
        while (loop_flag)
        {
            System.out.println("input number: ");
            int input = new Scanner(System.in).nextInt();

            if (input == target)
            {
                System.out.println("The number you give is equal to target: " + target);
                loop_flag = false;
            }
            else if (input < target)
                System.out.println("The number you give is less than target");
            else
                System.out.println("The number you give is great than target");

        }
    }
}