Java输入无法正常工作(初学者)

时间:2015-03-24 17:02:52

标签: java input system choice

出于某种原因,我的代码不接受最后一行的输入"您想要订购什么:"

有人能告诉我这里的错误是什么吗?它正确编译和一切。我只是一个初学者,所以请用基本的术语告诉我。

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");
                    }

3 个答案:

答案 0 :(得分:4)

您需要在致电in.nextLine()的行后面拨打in.nextInt() 原因是只要求下一个整数不会消耗输入中的整行,因此您需要通过调用in.nextLine()来跳过输入中的下一个换行符

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

每次在调用不占用整行的方法后需要获取新行时,必须完成这项工作。请考虑使用BufferedReader对象!

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

如果输入无法解析为整数,则会抛出与Scanner.nextInt()相同的错误。

关于您对错误的评论,有一个:

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

}

应该如下所示:

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

此外,它并非绝对必要,但我建议您在实例化列表时声明ArrayList的通用类型,以便进一步调用theMenu.get()不需要被转换为String。

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

比较字符串时,请确保使用str.equals("string to compare with")方法,而不是使用相等运算符(==)。因此,例如,choice2 == "yes"应该是choice2.equals("yes")。使用equalsIgnoreCase代替equals会忽略大小写差异,这在这种情况下可能很有用。

答案 1 :(得分:2)

in inneLine();功能你只需尝试其他扫描仪功能,如&#39; in.next()&#39;。只是R&amp; D使用已经赋予JVM本身的方法。 你只需使用正确的逻辑并使用等于&#34; =&#34;的等号()或equlIgnoreCase()方法。操作

答案 2 :(得分:-1)

您的代码缺少三个大括号。 Arraylist必须像这样声明

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

如果你想要一个整数的arraylist

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