关于java:while循环猜数字小游戏

11次阅读

共计 557 个字符,预计需要花费 2 分钟才能阅读完成。

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("猜到了 棒!");
          }

      }


    }
}

正文完
 0