Java 为什么这行要打印两次?

Java 为什么这行要打印两次?,java,Java,我已经创建了一个程序,作为我的计算工作的一部分,用于检查库存,它工作正常。但是,当用户检查一个项目的库存后,他们会被询问是否要检查另一个项目的库存,这是同一行打印两次的地方,我不知道为什么?。代码如下: import java.util.*; public class stock { public static void main(String[] args) { //initialising the scanners Scanner stock = new Scanne

我已经创建了一个程序,作为我的计算工作的一部分,用于检查库存,它工作正常。但是,当用户检查一个项目的库存后,他们会被询问是否要检查另一个项目的库存,这是同一行打印两次的地方,我不知道为什么?。代码如下:

import java.util.*;

public class stock {

public static void main(String[] args) {

    //initialising the scanners

    Scanner stock = new Scanner(System.in);
    Scanner levels = new Scanner(System.in);
    Scanner bar = new Scanner(System.in);
    Scanner choice = new Scanner(System.in);

    //initialising the string variables

    String chocolate;
    String chocolate2;
    String change;
    String choiceb;

    //initialising the integer variables

    int mars = 200;
    int twix = 200;
    int bounty = 200;
    int doubled = 200;
    int galaxy = 200;        
    int change2;        
    int counter = 1;
    int a = 1; 


    //asking the user what chocolate bar they want to check stock of

    System.out.println("Enter the chocolate bar to check stock of: Mars, Twix, Bounty, Double and Galaxy");
    System.out.println("***********************************");
    chocolate = stock.nextLine();
    System.out.println("***********************************");

    //depending on the users choice, this switch statement outputs the appropriate stock level of the bar entered

    switch (chocolate.toLowerCase()) {
        case ("mars"):
            System.out.println("There is currenty " + mars + " in stock");
            break;
        case ("twix"):
            System.out.println("There is currenty " + twix + " in stock");
            break;
        case ("bounty"):
            System.out.println("There is currenty " + bounty + " in stock");
            break;
        case ("double"):
            System.out.println("There is currenty " + doubled + " in stock");
            break;
        case ("galaxy"):
            System.out.println("There is currenty " + galaxy + " in stock");
            break;
        default:
            System.out.println("Your an idiot, try again");
            chocolate = stock.nextLine();
    }

    //the user is then asked if they want to change stock level of any of the chocolate bars

    System.out.println("Do you want to change stock levels?");
    System.out.println("***********************************");
    change = levels.nextLine();
    System.out.println("***********************************");

    //if the answer is yes it carries on with the program and ignores this if statement. if the answer is no, the program closes        

     if (change.equals("no")) {
        System.exit(0);
    }

     //this while loop and switch statement is used to check what chocolate bar stock level the user wants to change. 1 is  subtracted from the counter
     // on the users first input so that the message of checking if the user wants to change any more appears. this 

    while (a == 1){

        if (counter == 0) {
            System.out.println("Do you want to change the stock of any more");
            choiceb = choice.nextLine();
            counter = counter + 1;

        }else{
        System.out.println("Which chocolate do you want to change stock levels of?");
        System.out.println("***********************************");
        chocolate2 = bar.nextLine();
        System.out.println("***********************************");

        switch (chocolate2.toLowerCase()) {
            case ("mars"):
                System.out.println("Enter the amount of Mars Bars currently in stock");
                mars = bar.nextInt();
                System.out.println("There is now " + mars + " in stock");
                counter = counter - 1;

                break;
            case ("twix"):
                System.out.println("Enter the amount of Twix currently in stock");
                twix = bar.nextInt();
                System.out.println("There is now " + twix + " in stock");
                counter = counter - 1;
                break;
            case ("bounty"):
                System.out.println("Enter the amount of Bounty Bars currently in stock");
                bounty = bar.nextInt();
                System.out.println("There is now " + bounty + " in stock");
                counter = counter - 1;
                break;
            case ("double"):
                System.out.println("Enter the amount of Double Bars currently in stock");
                doubled = bar.nextInt();
                System.out.println("There is now " + doubled + " in stock");
                counter = counter - 1;
                break;
            case ("galaxy"):
                System.out.println("Enter the amount of Galaxy currently in stock");
                galaxy = bar.nextInt();
                System.out.println("There is now " + galaxy + " in stock");
                counter = counter - 1;
                break;

        }

    }
    }
}

}
这是程序运行时的输出:


我认为问题在于4个不同的扫描仪,都是从系统读取的。在中,在switch子句中添加一个默认语句,并输出chocolate 2.toLowerCase()。我可以想象chocolate2也包含输入“yes”,而switch子句无法识别这一点:)


一个扫描器应该在主方法开始时使用1初始化的计数器执行该操作,该计数器将始终设置为1,因此将打印两次。尝试将计数器初始化为0,然后运行代码

问题在于读取行和整数的混合:

    System.out.println("Which chocolate do you want to change stock levels of?");
    System.out.println("***********************************");
    chocolate2 = bar.nextLine();
    System.out.println("***********************************");

    switch (chocolate2.toLowerCase()) {
        case ("mars"):
            System.out.println("Enter the amount of Mars Bars currently in stock");
            mars = bar.nextInt();
首先,您正在使用
nextLine()
bar
读取。用户将输入
mars\r\n
\r\n
是点击回车键造成的换行符),扫描仪读取
mars\r\n

然后您正在从
读取
nextInt()
(!)。用户输入
2\r\n
,但是
nextInt()
将只读取
2
,将
\r\n
留在
扫描仪上,光标正好位于
\r\n
前面

您的逻辑进入第二个循环,重新打印消息,但是当您的
扫描仪再次点击
下一行()
时,它将继续并读取
\r\n
-开关在此循环上失败,您的逻辑进入第三个循环(第二次打印的消息

现在,
bar
再次为空,因此
bar.readLine()
将再次等待用户输入

要解决此问题,请确保在读取整数后跳过当前行,这样当扫描仪在提示输入类型时再次点击
nextLine()
,它将不会仅使用换行符:

mars = bar.nextInt();
bar.nextLine();

通过一个简短的、可编译的示例发布这篇文章做得很好。我认为问题在于4个不同的扫描仪,都是从System.in读取的。在switch子句中添加一个默认语句,并输出chocolate2.toLowerCase()。我可以想象chocolate2也会保存输入“yes”,而开关子句OK无法识别这一点,因此我可以使用一台扫描仪进行所有用户输入吗?当然,只要所有用户都从同一资源读取(在您的情况下是
系统中。在
),您就可以使用一台扫描仪而不是x扫描仪。Michael,您应该回答这个问题。1扫描器解决了问题只使用了一个扫描器,该行现在不会打印两次,但是,用户无法选择是否要检查更多的库存水平,你确定吗?请用一个扫描器再次发布您的代码。那你应该工作了谢谢!!!。这很有效。使用一台扫描仪而不是像我一样使用4台扫描仪更好吗?@haroldj97,因为您正在将它们全部连接到
系统。在
中,是的,一台扫描仪就足够了。