简体   繁体   English

Java 输入不工作(初学者)

[英]Java Input not working (Beginner)

For some reason, my code will not accept input on the last line "What would you like to order: "出于某种原因,我的代码将不接受最后一行“您想要订购什么:”的输入

Could anyone tell me what my error is here?谁能告诉我我的错误是什么? It is compiling correctly and everything.它正在正确编译和一切。 I am only a beginner so please tell me in basic terms.我只是一个初学者,所以请用基本术语告诉我。

import java.util.Scanner;
import java.util.*;

class RestaurantMain {
    public static void main(String[] args)
    {

        //Create an array list
        ArrayList menu = new ArrayList();

        //Variables//
        int choice;
        int customerChoice;
        boolean trueFalse;
        int restart = 0;
        String choice2;
        String addItems = "";
        int menuCount = 0;
        int indexCount = 0;
        String item = "";

        //Import input device
        Scanner in = new Scanner(System.in);

        ArrayList theMenu = new ArrayList();

        System.out.println("Welcome to the Cooper's restaurant system!");
        System.out.println("How can I help?");
        System.out.println("");
        System.out.println("1. Customer System");
        System.out.println("2. Management System");
        System.out.println("");
        System.out.println("");
        System.out.print("Which option do you choose: ");
        choice = in.nextInt();

            if (choice == 1) {
                System.out.println("Our menu's are as follows:");
                System.out.println("");
                System.out.println("1. Drinks");
                System.out.println("2. Starters");
                System.out.println("3. Mains");
                System.out.println("4. Desserts");
                System.out.println("");
                System.out.println("Please note - You MUST order 5 items.");
                System.out.println("");
                System.out.print("What menu would you like to follow? ");
                customerChoice = in.nextInt();

                    if (customerChoice == 1) {
                        System.out.println("Drinks Menu");
                            System.out.println("Would you like to order? ");
                            choice2 = in.nextLine();
                                if (choice2 == "yes") {
                                    System.out.println("Please enter the amount of items you want to order: ");
                                    while (indexCount <= menuCount);
                                        System.out.println("Please enter your item: ");
                                        item = in.nextLine(); {
                                        theMenu.add(item);
                                    }
                                    }

                    }
                    if (customerChoice == 2) {
                        System.out.println("Starters Menu");
                    }
                    if (customerChoice == 3) {
                        System.out.println("Mains menu");
                    }
                    if (customerChoice == 4) {
                        System.out.println("Desserts Menu");
                    }

You need to call in.nextLine() right after the line where you call in.nextInt() The reason is that just asking for the next integer doesn't consume the entire line from the input, and so you need skip ahead to the next new-line character in the input by calling in.nextLine() 您需要在调用in.nextLine()的行之后立即调用in.nextInt() ,原因是仅要求下一个整数不会占用输入中的整个行,因此您需要跳至通过调用in.nextLine()输入中的下一个in.nextLine()

customerChoice = in.nextInt();
in.nextLine();

This pretty much has to be done each time you need to get a new line after calling a method that doesn't consume the entire line. 每次您需要在调用不消耗整行的方法后获取新行时,都必须执行此操作。 Consider using a BufferedReader object instead! 考虑改用BufferedReader对象!

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int integer = Integer.parseInt(reader.readLine());

This will throw the same errors that Scanner.nextInt() does if the input can't be parsed as an integer. 如果不能将输入解析为整数,则将引发与Scanner.nextInt()相同的错误。

Regarding your comment about errors, there is one: 关于您对错误的评论,有以下一种:

while (indexCount <= menuCount);
System.out.println("Please enter your item: ");
item = in.nextLine(); {
 theMenu.add(item);
}

} }

Should instead be like the following: 相反,应如下所示:

while(indexCount <= menuCount){
    System.out.println("Please enter your item: ");
    item = in.nextLine();
    theMenu.add(item);
}

Also, it isn't strictly necessary, but I suggest that you do declare the ArrayList's generic type when instantiating the list, so that further calls to theMenu.get() don't need to be casted to a String. 另外,这也不是绝对必要的,但我建议您在实例化列表时确实声明ArrayList的泛型类型,这样就不必将对Menu.get()的进一步调用强制转换为String了。

ArrayList<String> theMenu = new ArrayList<String>();

When comparing strings, ensure that you use the str.equals("string to compare with") method, instead of the equality operator ( == ). 比较字符串时,请确保使用str.equals("string to compare with")方法,而不是等于运算符( == )。 Therefore for example, choice2 == "yes" should instead be choice2.equals("yes") . 因此,例如, choice2 == "yes"应该改为choice2.equals("yes") Using equalsIgnoreCase instead of equals would ignore case differences, which may be useful in this situation. 使用equalsIgnoreCase代替equals将忽略大小写差异,这在这种情况下可能很有用。

insted of in.nextLine(); 插入in.nextLine(); function you just try another scanner functions like 'in.next()'. 您只需尝试使用其他扫描仪功能(例如“ in.next()”)即可。 just R&D with the methods that already give the JVM itself. 只需使用已经提供JVM本身的方法进行研发。 you just use correct the logic and use equal() or equlIgnoreCase() methods insted of "=" operator. 您只需要使用正确的逻辑并使用由“ =”运算符插入的equal()或equlIgnoreCase()方法即可。

Your code has three braces missing. 您的代码缺少三个花括号。 Arraylist have to declared like this Arraylist必须这样声明

 ArrayList<class> list = new ArrayList<class>();

If you want an arraylist of integers 如果要整数数组列表

ArrayList<Integers> in = new ArrayList<Integers>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM