以下代码包含一个开关盒,它执行五次询问顾客的五个冰淇淋选项,但条件是每次客户必须选择不同的冰淇淋,如果他再次选择相同的冰淇淋,它应该显示错误有人可以帮助我解决上述逻辑,或者建议一个比切换案例更好的方法。
class Icecreams {
public static void main( String[] args ) throws Exception {
for( int i = 1; i <= 5; i++ ) {
System.out.println("Select an Icecream:\n1.Strawberry\n2.Vanilla\n3.Chocolate\n4.Butterscotch\n5.Black current\n6.Exit");
DataInputStream in = new DataInputStream(System.in);
int n = Integer.parseInt(in.readLine());
switch( n ) {
case 1:
System.out.println("STRAWBERRY-Rs.50");
break;
case 2:
System.out.println("VANILLA-Rs.40");
break;
case 3:
System.out.println("CHOCOLATE-Rs.60");
break;
case 4:
System.out.println("BUTTERSCOTCH-Rs.70");
break;
case 5:
System.out.println("BLACKCURRENT-Rs.40");
break;
case 6:
System.out.println("Exit");
System.exit(0);
break;
default:
System.out.println("Select from the available choice");
break;
}
}
}
}
答案 0 :(得分:2)
你可以这样做:把可能的冰淇淋放在List
中,即ArrayList
,每次选择冰淇淋时,你都要从{{1}中取出相应的冰淇淋。 }。
您也可以使用相同的ArrayList
向用户显示选项菜单。
使用ArrayList
的好处是,如果将来冰淇淋的选择发生变化,您无需修改程序。根据经验:在代码中放置抽象,在数据中加入详细信息。(Dave Thomas,Andrew Hunt http://pragmatictips.com/38)
之后,您甚至可以修改程序以从文件中读取冰淇淋,因此您只需在选择冰淇淋时更改文本文件。
答案 1 :(得分:1)
就像我在评论中所说,你需要保存以前的选择。所以它可能看起来像这样:
public static void main( String[] args ) throws Exception {
int previousChoice = 0;
for( int i = 1; i <= 5; i++ ) {
System.out.println("Select an Icecream:\n1.Strawberry\n2.Vanilla\n3.Chocolate\n4.Butterscotch\n5.Black current\n6.Exit");
DataInputStream in = new DataInputStream(System.in);
int n = Integer.parseInt(in.readLine());
while (previousChoice == n) {
System.out.println("Can't select the same ice-cream twice in a row, try again.");
n = Integer.parseInt(in.readLine());
}
previousChoice = n; // save previous choice
switch( n ) {
免责声明:这只适用于您需要避免连续两次选择相同的冰淇淋。如果您需要确保选择总是唯一,那么解决方案会有所不同,但最好在您的问题中澄清它。
答案 2 :(得分:0)
您可以将此代码扩展到您的问题。此解决方案简单易懂。您应该很容易理解改变的位置。
import java.util.ArrayList;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int n = 0, selection;
ArrayList<Integer> selectionsSoFar = new ArrayList<Integer>();
while (n < 5) {
System.out.println("Enter Option:\n");
selection = keyboard.nextInt();
if (selectionsSoFar.contains(selection) || selection <= 0 || selection > 2) {
System.out.println("Error");
} else {
switch (selection) {
case 1:
System.out.println("1");
selectionsSoFar.add(selection);
n++;
break;
case 2:
System.out.println("2");
selectionsSoFar.add(selection);
n++;
break;
}
}
}
keyboard = null;
}
}
答案 3 :(得分:0)
import java.util.Scanner;
public class practice4 {
public static void main(String[] args) {
String I = "Icecream Rs. 10.";
String S = "Sandwitch Rs.5";
String C = "Cake Rs.15";
String Ch = "Chocolate Rs.5";
String L = "Lemon water Rs.12";
int limit = 6;
System.out.println(
"Select your choice from below options of 1 - 5 \n" +
"1 :" + I + "\n2 :" + S + "\n3 :" + C + "\n4 :" + Ch + "\n5 :" + L);
for (int i = 1; i <= limit; i++) {
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("You have chosen a item " + I);
break;
case 2:
System.out.println("You have chosen a item " + S);
break;
case 3:
System.out.println("You have chosen a item " + C);
break;
case 4:
System.out.println("You have chosen a item " + Ch);
break;
case 5:
System.out.println("You have chosen a item " + L);
break;
default:
System.out.println("Select only from the available choice of 1 - 5");
break;
}
}
}
}