Java代码:猜数字游戏

用java编写一个简单的猜数字游戏:

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

public class GuessNumGame {
    public static void main(String[] args) {
        int[] level = {50, 100, 200, 500, 1000, 2000, 5000};

        //
        Random rand = new Random();
        Scanner sc = new Scanner(System.in);
        int le = level[rand.nextInt(level.length)];
        int num = rand.nextInt(1,le + 1);
        int i = 0;
        while (true){
            ++i;
            System.out.println("请输入[1-" + le + "]:");
            int t = sc.nextInt();
            if (t > num){
                System.out.printf("%d、\033[31m太大了\033[0m%n", i);
            } else if (t

运行结果:

Java代码:猜数字游戏_第1张图片

 

你可能感兴趣的:(java,游戏,python)