GuessNumber.java

2022-03-17  本文已影响0人  Seki_Etsu

import java.util.Scanner;

public class GuessNumber {

public static int generateRandomInteger(int n1 , int n2) {
    int min = n1<n2?n1:n2; 
    int max = n1>n2?n1:n2;
    int randomInteger = (int) (System.currentTimeMillis()%(max-min)+min);
    return randomInteger;
}

public static void main(String[] args) {
    
    final int key = generateRandomInteger(1 , 100);
    /// 下面这行调试用
    //System.out.println(key);
    
    System.out.println("给你一个1-100的整数,请猜测这个数");
    System.out.print("输入你猜的数:");

    for(int value = 0;;) {
        Scanner scanner = new Scanner(System.in);
        value = scanner.nextInt();
        if(value == key) {
            System.out.println("猜对了");
            break;
        }else if (value > key) {
            System.out.println("猜大了,重新输入你的猜测:");
        }else {
            System.out.println("猜小了,重新输入你的猜测:");
        }
    }
}

}

上一篇下一篇

猜你喜欢

热点阅读