while循环 无线次数猜1~100数字

明天咱们来聊一下 while循环,
1.1 while 循环 在条件为真的状况下,会反复的执行语句

根底语法

while 循环的语句如下:   while(循环持续条件){   // 循环体   语句(组);}

通过1.1 的小贴士咱们就能够来做一个猜数字的小游戏了

while循环猜数字小游戏

import java.util.Scanner;public class For {    public static void main(String[] args) {//创立了一个键盘输入      Scanner input =  new Scanner(System.in);//猜猜数字      System.out.println("请输入一个 1 ~ 100 的数字 ");//创立1~100随机数      int number1 = (int)(Math.random() * 101);      int guess = -1;// while 循环个性 不对不许走      while(guess !=  number1){          guess = input.nextInt();          if (guess < number1){              System.out.println("小了 ");          }else if(guess > number1){              System.out.println("大了");          }else{              System.out.println("猜到了 棒! ");          }      }    }}