A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. import java.util.Scanner;
  2. class GuessNumber
  3. {
  4.         public static void main (String [] args)
  5.         {
  6.                 Scanner s = new Scanner(System.in);

  7.                 //定义一个1-100之间的随机数
  8.                 int number = (int)(Math.random()*100)+1;
  9.                 //不确定猜的次数,利用while循环
  10.                 while(true)
  11.                 {               
  12.                         System.out.print("请输入你要猜的数字:");
  13.                         int x = s.nextInt();
  14.                         if(x > number)
  15.                         {
  16.                                 System.out.println("你猜的数字:"+x+" 大了");
  17.                         }
  18.                         else if(x < number)
  19.                         {
  20.                                 System.out.println("你猜的数字:"+x+" 小了");
  21.                         }
  22.                         else
  23.                         {
  24.                                 System.out.println("恭喜你,猜对了!");
  25.                                 break;//猜对时结束循环
  26.                         }
  27.                         System.out.println();
  28.                 }
  29.         }
  30. }
复制代码


3 个回复

倒序浏览
很好玩把
回复 使用道具 举报
好,不错!
回复 使用道具 举报
顶一个~~~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马