Java输入无法正常工作(初学者) [英] Java Input not working (Beginner)

查看:181
本文介绍了Java输入无法正常工作(初学者)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

您需要致电 in.nextLine()就在您调用 in.nextInt()的行之后
原因是要求下一个整数不会消耗输入中的整行,因此您需要通过调用 in.nextLine()

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()

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

每次调用一个方法后需要获取新行时,这几乎必须完成不消耗整条线。考虑使用 BufferedReader 对象!

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 reader = new BufferedReader(new InputStreamReader(System.in));
int integer = Integer.parseInt(reader.readLine());

这将抛出与 Scanner.nextInt()如果输入无法解析为整数。

This will throw the same errors that Scanner.nextInt() does if the input can't be parsed as an integer.

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

Regarding your comment about errors, there is one:

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。

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<String> theMenu = new ArrayList<String>();

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

When comparing strings, ensure that you use the str.equals("string to compare with") method, instead of the equality operator (==). Therefore for example, choice2 == "yes" should instead be choice2.equals("yes"). Using equalsIgnoreCase instead of equals would ignore case differences, which may be useful in this situation.

这篇关于Java输入无法正常工作(初学者)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆