java 无法输入,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");

}

解决方案

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

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

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

}

}

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值