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

Java薪资计算程序添加Yes重启No退出循环功能咨询

你可以通过do-while循环包裹原有业务逻辑实现需求,同时修正了原代码中所有分支岗位名称统一显示为Network Engineer的错误,以及处理了Scanner读取整数后残留换行符导致后续输入异常的问题,完整修改后代码如下:

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

public class Activity2 {
    public static void main(String[] args) {
        String name;
        int choice;
        int time;
        String continueInput;
        Console cons = System.console();
        Scanner scn = new Scanner(System.in);
        
        if (cons == null) {
            System.out.println("控制台初始化失败");
            return;
        }
        
        // 外层循环控制是否重新录入
        do {
            name = cons.readLine("Enter your Name: ");
            
            System.out.println("\n菜单");
            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();
            
            if (choice == 1) {
                int gsalary = 1200 * time;
                int nsalary = gsalary - (gsalary * 12 / 100);
                cons.printf("\nHello %s!\n", name);
                System.out.println("Your job salary as a Network Engineer is 1200 per hour.");
                System.out.println("Your gross salary for this week is " + gsalary + ".");
                System.out.println("Your net salary for this week is " + nsalary + ".");
            } else if (choice == 2) {
                int gsalary = 800 * time;
                int nsalary = gsalary - (gsalary * 12 / 100);
                cons.printf("\nHello %s!\n", name);
                System.out.println("Your job salary as a Software Engineer is 800 per hour.");
                System.out.println("Your gross salary for this week is " + gsalary + ".");
                System.out.println("Your net salary for this week is " + nsalary + ".");
            } else if (choice == 3) {
                int gsalary = 600 * time;
                int nsalary = gsalary - (gsalary * 12 / 100);
                cons.printf("\nHello %s!\n", name);
                System.out.println("Your job salary as a Full Stack Developer is 600 per hour.");
                System.out.println("Your gross salary for this week is " + gsalary + ".");
                System.out.println("Your net salary for this week is " + nsalary + ".");
            } else if (choice == 4) {
                int gsalary = 500 * time;
                int nsalary = gsalary - (gsalary * 12 / 100);
                cons.printf("\nHello %s!\n", name);
                System.out.println("Your job salary as a Technical Support is 500 per hour.");
                System.out.println("Your gross salary for this week is " + gsalary + ".");
                System.out.println("Your net salary for this week is " + nsalary + ".");
            }
            
            // 询问是否继续录入
            System.out.print("\n您是否需要录入另一位员工信息?(Yes/No):");
            // 消费nextInt残留的换行符,避免后续读取到空字符串
            scn.nextLine();
            continueInput = scn.nextLine().trim();
            
        } while (continueInput.equalsIgnoreCase("Yes"));
        
        scn.close();
        System.out.println("程序已退出");
    }
}

关键改动说明

  • 外层新增do-while循环:优先执行一次员工信息录入、薪资计算的完整流程,执行完成后判断是否需要重新执行
  • 修复输入异常问题:调用scn.nextLine()消费nextInt()读取后残留的换行符,避免后续读取用户是否继续的输入时读到空值
  • 修正岗位名称显示错误:原代码所有分支的岗位名称都错误写为Network Engineer,已经对应修改为实际选择的岗位
  • 新增循环控制逻辑:读取用户输入的Yes/No,输入不区分大小写的Yes则重新执行录入流程,其他输入直接退出程序
  • 新增资源释放逻辑:程序退出前关闭Scanner释放IO资源

内容的提问来源于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.04 04:42:03