我不知道';我不知道为什么循环不';别停下来!JAVA
这是我在这里的第一个疑问,我真的不知道这一点,我的英语不是最好的,所以首先对不起,如果有一个傻瓜的错误。 问题是我想做一个for循环来保存来自控制台的每个用户输入,并将其添加到名为“Order”的列表中。如果用户正确键入订单,则必须执行此操作,并且必须检查该操作,以了解菜单上是否存在该操作。如果没有,我们必须让用户知道情况,并询问他/她是否想要菜单上的东西。此外,在列表上保存每个输入后,我们希望询问用户是否想要订购更多的产品,因此,如果是,则必须初始化循环,如果不是,则必须退出循环。 我的代码的问题是,无论我在控制台中键入什么,循环都是从头到尾完成的。 错误在哪里? 谢谢我不知道';我不知道为什么循环不';别停下来!JAVA,java,loops,for-loop,Java,Loops,For Loop,这是我在这里的第一个疑问,我真的不知道这一点,我的英语不是最好的,所以首先对不起,如果有一个傻瓜的错误。 问题是我想做一个for循环来保存来自控制台的每个用户输入,并将其添加到名为“Order”的列表中。如果用户正确键入订单,则必须执行此操作,并且必须检查该操作,以了解菜单上是否存在该操作。如果没有,我们必须让用户知道情况,并询问他/她是否想要菜单上的东西。此外,在列表上保存每个输入后,我们希望询问用户是否想要订购更多的产品,因此,如果是,则必须初始化循环,如果不是,则必须退出循环。 我的代码的
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Fase2_final {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] menu = { "chicken", "steak", "hamburger", "spaghettis", "pizza" };
int[] price = { 10, 15, 20, 5, 12 };
boolean order = true;
for (int i = 0; i < menu.length; i++) {
System.out.println("We have " + menu[i] + " for " + price[i] + "€");
}
int yes = 1;
int no = 0;
Scanner yesOrNo = new Scanner(System.in);
System.out.println("What do you want to eat?");
List<String> Order = new ArrayList<String>();
do {
Scanner inputOrder = new Scanner(System.in);
String userOrder = inputOrder.next().toLowerCase();
for (int j = 0; j < menu.length; j++) {
if (userOrder.equals(menu[j])) {
Order.add(userOrder);
System.out.println("You ordered" + Order + "\nSomething more?" + "\nSay YES with 1 and NO with 0");
} else if (userOrder.equals(menu[j]) == false) {
System.out.println("We don't have this on menu!"
+ "\nDo you want to order another thing that we do have in menu?" + "\nSay YES with 1 and NO with 0");
} else if (yesOrNo.nextInt() == no) {
System.out.println("Order finished!");
}
}
} while (yesOrNo.nextInt() == yes);
}
}```
导入java.util.ArrayList;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Scanner;
公开课Fase2_期末考试{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
String[]菜单={“鸡肉”、“牛排”、“汉堡包”、“意大利面”、“比萨饼”};
int[]价格={10,15,20,5,12};
布尔顺序=真;
对于(int i=0;i
nextInt()方法返回当前输入一次,如果调用两次,则需要第二次输入。它应该是这样的:
do{
扫描仪输入顺序=新扫描仪(System.in);
字符串userOrder=inputOrder.next().toLowerCase();
int-answer=0;
对于(int j=0;j
我希望我回答了你的问题。我已经重构了你的代码。尝试在比较工具中查看
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Fase2_final {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] menu = { "chicken", "steak", "hamburger", "spaghettis", "pizza" };
int[] price = { 10, 15, 20, 5, 12 };
boolean order = true;
for (int i = 0; i < menu.length; i++) {
System.out.println("We have " + menu[i] + " for " + price[i] + "€");
}
int decision = 0;
int yes = 1;
int no = 0;
Scanner yesOrNo = new Scanner(System.in);
System.out.println("What do you want to eat?");
List<String> Order = new ArrayList<String>();
do {
Scanner inputOrder = new Scanner(System.in);
String userOrder = inputOrder.next().toLowerCase();
for (int j = 0; j < menu.length; j++) {
if (userOrder.equals(menu[j])) {
Order.add(userOrder);
System.out.println("You ordered" + Order + "\nSomething more?" + "\nSay YES with 1 and NO with 0");
} else if (userOrder.equals(menu[j]) == false) {
System.out.println("We don't have this on menu!"
+ "\nDo you want to order another thing that we do have in menu?" + "\nSay YES with 1 and NO with 0");
}
}
decision = yesOrNo.nextInt();
if (decision == no) {
System.out.println("Order finished!");
}
} while (decision == yes);
}
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
公开课Fase2_期末考试{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
String[]菜单={“鸡肉”、“牛排”、“汉堡包”、“意大利面”、“比萨饼”};
int[]价格={10,15,20,5,12};
布尔顺序=真;
对于(int i=0;i
完成后,请始终关闭扫描仪。无需每次迭代都重新声明扫描仪对象,也无需拥有多个将从同一源读取的扫描仪对象。此外,没有必要在每次比较中都打印“我没有找到它!” 考虑以下代码块:
.
.
.
// there is no need to re-declare the Scanner object every iteration, or two scanners
Scanner keyboard = new Scanner(System.in);
do {
String userOrder = keyboard.next().toLowerCase();
// we haven't found the item
boolean found = false;
// integer option (yes or no)
int option;
// do the search
for (int j = 0; j < menu.length; j++) {
// do this if found
if (userOrder.equals(menu[j])) {
// set flag to indicate we found the item
found = true;
// add the order
Order.add(userOrder);
// print out, then read the user's option
System.out.println("You ordered" + userOrder + "\nSomething more?" + "\nSay YES with 1 and NO with 0");
option = keyboard.nextInt();
// consume the NL leftover from the nextInt() call
keyboard.nextLine();
// break out of the for loop since you already found what you're looking for
break;
}
}
// check if you didn't find the item
if (!found) {
// print out
System.out.println("We don't have this on menu!"
+ "\nDo you want to order another thing that we do have in menu?" + "\nSay YES with 1 and NO with 0");
// read if user wants to continue
option = keyboard.nextInt();
// consume the NL leftover from the nextInt() call
keyboard.nextLine();
}
} while (option == yes);
System.out.println("Order finished!");
.
.
.
// close the scanner when you're done
keyboard.close();
.
.
.
。
.
.
//不需要每次迭代或两次扫描都重新声明Scanner对象
扫描仪键盘=新扫描仪(System.in);
做{
字符串userOrder=keyboard.next().toLowerCase();
//我们还没有找到那件物品
布尔值=false;
//整数选项(是或否)
int选项;
//搜索
对于(int j=0;j