首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是重复程序/否退出

是重复程序/否退出
EN

Stack Overflow用户
提问于 2021-09-18 05:22:06
回答 1查看 26关注 0票数 0

下面是我的代码:

代码语言:javascript
复制
import java.util.Scanner;
import java.io.Console;

        public class Activity2
       {
        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);
        
        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();
        
        
        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 weeks is "+nsalary+".");
        }
        
        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 Network Engineer is 800 per hour.");
            System.out.println("Your gross salary for this week is "+gsalary+".");
            System.out.println("Your net salary for this weeks is "+nsalary+".");
        }
        
        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 Network Engineer is 600 per hour.");
            System.out.println("Your gross salary for this week is "+gsalary+".");
            System.out.println("Your net salary for this weeks is "+nsalary+".");
        }
        
        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 Network Engineer is 500 per hour.");
            System.out.println("Your gross salary for this week is "+gsalary+".");
            System.out.println("Your net salary for this weeks is "+nsalary+".");
        }
    }
}

以下是示例输出:

输入你的名字: Matsuno Chifuyu

菜单

1网络工程师

2软件工程师

3完整堆栈开发人员

4技术支持

输入您的工作(从菜单中选择):3输入no。工作时间的百分比: 36

你好,松野千夫宇!作为一个全栈开发人员,你的工作薪水是每小时600美元。你这周的毛薪是21600美元。你这周的净薪是19008美元。

是否要输入其他员工?(是或否)

我如何做一个循环,如果回答是,程序将重新启动,如果否,程序将退出

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-18 05:31:00

要做到这一点,最简单的方法是将代码抛入循环,然后在用户输入no时中断。如下例所示:

代码语言:javascript
复制
public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    while (true) {
        // put your app logic here
    
        System.out.println("Do you want to input another employee? [Y]es/[N]o");
        if (!in.next().equalsIgnoreCase("Y") {
            break;
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69231822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档