如何使用switch语句和scan实用程序模拟商店队列



我必须使用扫描、切换和案例制作一个Java程序,在这些案例中,我可以用命令添加一个客户"添加";并用命令"删除"删除一个顾客;移除";。

队列中的默认客户数为5。如果顾客的数量大于8;这个队列太大了"如果少于1个顾客,则打印出"1";队伍里没有人">

我试着做了一些代码,但我不知道下一步该做什么。

import java.util.Scanner;
public class fronta {
public static void main(String[] args) {

System.out.println ("This queue has 5 people in it at the moment.");    
Scanner scan = new Scanner(System.in);
boolean x = true;
String b = "ADD";
int a = 5;
b = scan.nextLine();
while(x){
switch (b) {
case "ADD":

System.out.println ("This queue has " + a + " people in it at the moment.");
b = scan.nextLine();
System.out.println ("This queue is too big");
break;

default:
case "EXIT":
System.out.println("End of simulation.");
x = false;
break;  
}
}
}
}

我认为您需要以下内容:

public static void main(String[] args) {
boolean isExitRequested = false;
int queueSize = 5;
System.out.println ("This queue has "+queueSize+" people in it at the moment.");
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()){
String input = scan.nextLine();
switch (input){
case "ADD":
System.out.println ("This queue has " + queueSize++ + " people in it at the moment.");
if (queueSize > 8) {
System.out.println("This queue is too big");
}
break;
case "REMOVE":
if (queueSize == 0){
System.out.println("There's nobody in the queue.");
} else {
queueSize--;
}
break;
case "EXIT":
isExitRequested = true;
break;
default:
System.out.println("Unknown input: "+input);
}
if(isExitRequested)
break;
}
}

最新更新