下面是我的代码:
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美元。
是否要输入其他员工?(是或否)
我如何做一个循环,如果回答是,程序将重新启动,如果否,程序将退出
发布于 2021-09-18 05:31:00
要做到这一点,最简单的方法是将代码抛入循环,然后在用户输入no时中断。如下例所示:
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;
}
}
}https://stackoverflow.com/questions/69231822
复制相似问题