首页 > 其他 > 详细

猜数字游戏

时间:2018-10-07 14:12:47      阅读:125      评论:0      收藏:0      [点我收藏+]

问题:在键盘里输入数字,判断是否等于随机数

思路:

1.首先生成一个随机数

1 int number  = (int)(Math.random()*101);

2.代码实现

 1 package Demo2;
 2 import java.util.Scanner;
 3 public class Demo3 {
 4 
 5     public static void main(String[] args) {
 6         
 7         int number  = (int)(Math.random()*101);
 8         Scanner input = new Scanner(System.in);
 9         
10         
11         int guess = -1;
12         
13         
14         while(guess!=number) {//        while 在无法确定判断条件的情况下,优先选用while
15             System.out.println("请输入你猜的数字:");//循环输入
16             guess = input.nextInt();
17             
18             if(guess==number) {
19                 System.out.print("yes,the number is "+number);
20                 break;// 如果猜对了,就跳出循环,break是跳出整个循环。
21             }
22             
23             else if(guess>number) {
24                 System.out.print("your guess is too high ");
25             }
26             
27             else 
28                 System.out.print("your guess is too low ");
29         }
30         
31         
32     }
33 }

 

 

猜数字游戏

原文:https://www.cnblogs.com/ONE-PIECE-ZXZ/p/9749989.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!