- import java.util.Scanner;
- class GuessNumber
- {
- public static void main (String [] args)
- {
- Scanner s = new Scanner(System.in);
- //定义一个1-100之间的随机数
- int number = (int)(Math.random()*100)+1;
- //不确定猜的次数,利用while循环
- while(true)
- {
- System.out.print("请输入你要猜的数字:");
- int x = s.nextInt();
- if(x > number)
- {
- System.out.println("你猜的数字:"+x+" 大了");
- }
- else if(x < number)
- {
- System.out.println("你猜的数字:"+x+" 小了");
- }
- else
- {
- System.out.println("恭喜你,猜对了!");
- break;//猜对时结束循环
- }
- System.out.println();
- }
- }
- }
复制代码
|
|