You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java程序如何通过输入Yes/No重复执行?do while及代码问题排查

功能实现说明
  • 语法选型结论:do-while和switch-case不冲突,二者可以搭配使用
    • do-while是循环控制结构,正好匹配你需要的「程序至少运行一次,用户输入Yes就重复执行、输入No就退出」的需求,你之前运行失效大概率是代码结构或者输入处理的问题,不是do-while本身不适用
    • switch-case可以用来替换你现在4个并列if的分支判断逻辑,代码会更简洁易读
  • 原代码的已知问题:
    • 所有if分支里的职位输出都写死成了Network Engineer,和实际选择的职位不符
    • 缺少循环包裹逻辑,也没有用户Yes/No输入的判断代码
    • 如果之前写do-while时没处理Scanner的输入残留,会出现读取空值导致逻辑失效的问题

修复后的完整代码

import java.util.Scanner;
import java.io.Console;

public class Main
{
    public static void main(String[] args) {
        String name;
        int choice;
        int time;
        Console cons = System.console();
        if (cons==null)
        {
            System.out.println("控制台获取失败,程序退出");
            return;
        }

        name = cons.readLine("Enter your Name: ");
        Scanner scn = new Scanner(System.in);
        String continueFlag;

        // 用do-while包裹所有要重复执行的逻辑
        do {
            System.out.println("\nMenu");
            System.out.println("\n[1] Network Engineer\n[2] Software Engineer\n[3] Full Stack Developer\n[4] Technical Support");
            
            System.out.print("\nEnter your job(Choose a number from the menu): ");
            choice = scn.nextInt();
            System.out.print("Enter no. of hours present at work: " );
            time = scn.nextInt();
            // 处理nextInt残留的换行符,避免后续nextLine读空
            scn.nextLine();

            int hourlySalary = 0;
            String jobName = "Unknown Job";
            switch (choice) {
                case 1:
                    hourlySalary = 1200;
                    jobName = "Network Engineer";
                    break;
                case 2:
                    hourlySalary = 800;
                    jobName = "Software Engineer";
                    break;
                case 3:
                    hourlySalary = 600;
                    jobName = "Full Stack Developer";
                    break;
                case 4:
                    hourlySalary = 500;
                    jobName = "Technical Support";
                    break;
            }

            if (hourlySalary > 0) {
                int gsalary = hourlySalary * time;
                int nsalary = gsalary - (gsalary * 12 / 100);
                
                cons.printf("\nHello %s!\n", name);
                System.out.printf("Your job salary as a %s is %d per hour.%n", jobName, hourlySalary);
                System.out.println("Your gross salary for this week is "+gsalary+".");
                System.out.println("Your net salary for this week is "+nsalary+".");
            } else {
                System.out.println("输入的职位编号无效");
            }

            // 询问用户是否继续
            System.out.print("\n是否继续运行程序?输入Yes继续,输入No退出:");
            continueFlag = scn.nextLine().trim();
        // 循环条件:用户输入Yes就继续执行,不区分大小写
        } while (continueFlag.equalsIgnoreCase("Yes"));

        scn.close();
        System.out.println("程序退出");
    }
}

内容的提问来源于stack exchange,提问作者El jinx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 18:06:02