gpt4 book ai didi

java - 我怎样才能让这个开关只选择 3 个选项之一?

转载 作者:行者123 更新时间:2023-11-30 07:43:56 26 4
gpt4 key购买 nike

System.out.println("Please make a selection between 1 and 3");
int choice = s.nextInt();

switch (choice)
{
case 3: System.out.println("Can a brother get some fries with that?");
case 2: System.out.println("Cheeseburger it is.... fatso");
case 1: System.out.println("Good choice, you could use a salad");
break;
default: System.out.println("Not a valid selection bruh");
}

最佳答案

switch (choice) {
case 3: System.out.println("Can a brother get some fries with that?"); break;
case 2: System.out.println("Cheeseburger it is.... fatso"); break;
case 1: System.out.println("Good choice, you could use a salad"); break;
default: System.out.println("Not a valid selection bruh"); break;
}

我实际上只是在每个case之后添加了break,因此如果遇到1,2,它将退出您的switch ,3 或没有。

关于java - 我怎样才能让这个开关只选择 3 个选项之一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34211085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com