1 Star 0 Fork 48

刘欣媛 / 22级07班JAVA基础仓库

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2022.12.5.md 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
刘欣媛 提交于 2022-12-26 16:28 . 2022.12.26

while语句

格式

初始化语句;

while(条件判断语句){

循环语句体;

条件控制语句;

}

import java.util.Scanner;

public class D6_1 {
    public static void main(String[] args) {
        // 玩猜数字游戏,每局有三次机会(不能用for) ,提示:结束循环可以用break
// 三次机会用完,询问用户是否继续.输入"Y"或"y"就让其继续猜3次,如果回答"N"或者"n",就结束游戏;
        Scanner sc = new Scanner(System.in);
        int number = 34;

        int count = 0;

        while (count<=3){
            System.out.println("数字是:");
            int s = sc.nextInt();
            if (s==number){
                System.out.println("恭喜你猜对了!");
                break;
            }else if(s<number){
                System.out.println("猜小了");

            }else if( s>number){
                System.out.println("猜大了");

            }
            count++;
            if(count==3){
                System.out.println("是(y)否(n)继续?");
                String choose = sc.next();
                if ("y".equals(choose)){
                    System.out.println("继续游戏");
                    count=0;
                } else {
                    System.out.println("结束游戏");
                    count=4;


                }
            }
        }
    }
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liu-xinyuan123/java-base.git
git@gitee.com:liu-xinyuan123/java-base.git
liu-xinyuan123
java-base
22级07班JAVA基础仓库
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891