美文网首页
GuessNumber.java

GuessNumber.java

作者: Seki_Etsu | 来源:发表于2022-03-17 09:08 被阅读0次

    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("猜小了,重新输入你的猜测:");
            }
        }
    }
    

    }

    相关文章

      网友评论

          本文标题:GuessNumber.java

          本文链接:https://www.haomeiwen.com/subject/xofvdrtx.html