玩法:输入1-100以内的随机数,系统会判断偏大或者偏小,然后重新输入直至输入与系统给出的随机数一致时,程序结束运行,程序会统计你一共用了多少次猜对结果。

Tips:猜数方法正确的话7次就能猜对。

import java.util.Scanner;

public class MathGame {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    int number = (int)(Math.random()*100+1);
    int a,count = 0;
    do
    {
        a =in.nextInt();
        count = count + 1;
        if(a>number)
        {
            System.out.println("偏大");
        }
        else if(a<number)
        {
            System.out.println("偏小");
        }
    }while( a !=number );
    System.out.println("恭喜你猜对了,一共猜了"+count+"次");
}

}
最后修改:2019 年 12 月 31 日 11 : 05 PM
如果觉得我的文章对你有用,请随意赞赏