标签:bre guess void 跳出循环 ber 一个 情况下 util 思路
问题:在键盘里输入数字,判断是否等于随机数
思路:
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 }
标签:bre guess void 跳出循环 ber 一个 情况下 util 思路
原文地址:https://www.cnblogs.com/ONE-PIECE-ZXZ/p/9749989.html