为什么我无法扫描用户的输入?

时间:2015-03-15 02:29:05

标签: java

不会让用户输入字符串。它运行非粗体的部分,但是当我声明String变量“operation”时它停止运行。我以为它会让用户输入“p”或“c”。但它不会让用户输入任何东西。它只是说“建立成功”。

public static void main(String[] args) {
    String[] items = {"notepad","pen","pencil","eraser","marker"};
    int[] quantity = new int[5];
    Scanner input = new Scanner(System.in);
    System.out.println("Please input the quantity for notepad, pen, pencil, eraser, and marker. Enter them in the correct order.");
    quantity[0] = input.nextInt();
    quantity[1] = input.nextInt();
    quantity[2] = input.nextInt();
    quantity[3] = input.nextInt();
    quantity[4] = input.nextInt();
    while (quantity[0]<0||quantity[1]<0||quantity[2]<0||quantity[3]<0||quantity[4]<0){//loop if user enters invalid number
            System.out.println("Invalid input, enter numbers greater than or equal to 0.");
            quantity[0] = input.nextInt();
            quantity[1] = input.nextInt();
            quantity[2] = input.nextInt();
            quantity[3] = input.nextInt();
            quantity[4] = input.nextInt();
            if (quantity[0]>=0&&quantity[1]>=0&&quantity[2]>=0&&quantity[3]>=0&&quantity[4]>=0)
                break;//breaks out the loop            
}
    double[] price = {0,0,0,0,0};
    System.out.println("Please enter the price for the notepad, pen, pencil, eraser,and marker. Enter them in the correct order.");
    price[0] = input.nextDouble();
    price[1] = input.nextDouble();
    price[2] = input.nextDouble();
    price[3] = input.nextDouble();
    price[4] = input.nextDouble();
     while (price[0]<0||price[1]<0||price[2]<0||price[3]<0||price[4]<0){//loop if user enters invalid number
            System.out.println("Invalid input, enter numbers greater than or equal to 0.");
            price[0] = input.nextDouble();
            price[1] = input.nextDouble();
            price[2] = input.nextDouble();
            price[3] = input.nextDouble();
            price[4] = input.nextDouble();
     }
            if (price[0]>=0&&price[1]>=0&&price[2]>=0&&price[3]>=0&&price[4]>=0){

    System.out.println("After you input the quantity and the price, there are a few options for you to choose to operate from:");
    System.out.println("Enter p to print inventory, c to check for low inventory, h to display highest and lowest inventory value items, v to display total inventory value, and e to exit the program.");
            }
    **String operation; 
    operation = input.nextLine();
        switch (operation){
            case "p":
                System.out.println();
                System.out.println();
                System.out.println();
                System.out.println();
                System.out.println();
            case "c":**

        }
    }

}

1 个答案:

答案 0 :(得分:0)

如果您将扫描仪用于不同的数据类型,则需要在扫描仪的使用之间刷新缓冲区

input.nextLine();

这应解决不接受用户输入问题