为什么这条线打印两次?

时间:2014-11-03 18:52:32

标签: java

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

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;

        }

    }
    }
}

}

这是程序运行时的输出:

output

3 个答案:

答案 0 :(得分:3)

问题在于阅读线和整数的混合:

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

首先,您使用barnextLine()开始阅读。用户将输入mars\r\n\r\n是因点击返回而导致的换行符),扫描程序将显示mars\r\n

然后您正在从nextInt()阅读bar(!)。用户输入2\r\n,但nextInt()只会在2扫描仪上显示\r\n,光标只是bar之前。

您的逻辑进入第二个循环,重新打印消息,但当\r\n扫描程序再次点击bar时,它将继续并阅读nextLine() - 此开关失败,您的逻辑进入第三个循环(第二次打印消息

现在,\r\n再次为空,因此bar将再次等待用户输入。

要解决此问题,请确保在读取整数后跳过当前行,因此当扫描程序在提示类型时再次点击bar.readLine()时,它不会仅消耗换行符。

nextLine()

答案 1 :(得分:0)

我认为问题在于4个不同的扫描程序,都是从System.in中读取的,在switch-clause中添加一个默认语句并输出chocolate2.toLowerCase()。我可以想象chocolate2也保持输入“是”,并且switch语句无法识别这一点:)

一个扫描仪应该这样做

答案 2 :(得分:0)

在main方法开头用1初始化的计数器,它将计数器设置为1,因此它将被打印两次。尝试将计数器初始化为0,然后运行代码。