Java 我需要帮助从数组中进行选择
基本上,我需要传递的价格时,他们作出选择,但不知道如何Java 我需要帮助从数组中进行选择,java,Java,基本上,我需要传递的价格时,他们作出选择,但不知道如何 int selection; String[] snacks = { " (1): Chewing Gum", " (2): Kit Kat", " (3): Snickers", " (4): Pop Tarts" }; int[] price = { 40, 75, 80, 90 }; System.out.println("Java Vending Machine"); for (int index = 0; ind
int selection;
String[] snacks = { " (1): Chewing Gum", " (2): Kit Kat", " (3): Snickers", " (4): Pop Tarts" };
int[] price = { 40, 75, 80, 90 };
System.out.println("Java Vending Machine");
for (int index = 0; index < snacks.length; index++) {
System.out.println(snacks[index] + " -- " + price[index] + "¢");
}
Scanner keyboard = new Scanner(System.in);
selection = keyboard.nextInt();
return selection;
int选择;
字符串[]零食={”(1):口香糖“,”(2):Kit-Kat“,”(3):Snickers“,”(4):Pop-Tarts“};
int[]价格={40,75,80,90};
System.out.println(“Java自动售货机”);
for(int index=0;index<0.length;index++){
System.out.println(零食[指数]+“--”+价格[指数]+“);
}
扫描仪键盘=新扫描仪(System.in);
选择=键盘.nextInt();
返回选择;
如果我正确理解您的问题,您所需要的就是
// return selection;
return (selection > 0 && selection <= price.length) ? price[selection-1] : 0;
//返回选择;
return(selection>0&&selection 0&&selection给我们一些示例案例。不清楚您想要什么。
// That ternary could also be written as
if (selection > 0 && selection <= price.length) {
return price[selection-1];
}
return 0;