2014-11-03 161 views
3

我已经创建了一个程序,作为我的计算工作的一部分,用于检查库存,并且工作正常,但是,一旦用户检查完一个物品的库存后,他们会询问他们是否想检查另一个物料的库存这是同一行打印两次,我不明白为什么?这是代码:为什么这行打印两次?

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

+5

良好类型以一个简短的,可编辑的例子发布这篇文章。 – christopher 2014-11-03 18:57:13

+1

我认为问题出在4个不同的扫描仪上,都是从System.in中读取,在switch-clause中添加一个默认语句,并放出chocolate2.toLowerCase()。我可以想象,巧克力2也拥有输入“是”,并且这不会被交换子句 – Michael 2014-11-03 19:02:23

+0

确认,所以我可以使用一台扫描仪进行所有用户输入吗? – haroldj97 2014-11-03 19:03:05

回答

0

我认为这个问题是有4台不同的扫描仪,从System.in所有阅读,添加一个到你的switch-clause的默认语句,并把chocolate2.toLowerCase()。我可以想象,chocolate2保持输入“是”为好,这不是由开关条款:)认可

一台扫描仪应该这样做

+0

刚刚尝试过使用一台扫描仪,但现在该行不打印两次,但用户无法选择是否要检查任何更多的https://imageshack.com/i/eyaBJA30p – haroldj97 2014-11-03 19:09:02

+0

你确定?请使用一台扫描仪重新发布您的代码。应该工作然后 – Michael 2014-11-03 19:13:58

0

你在你的主要的开始初始化为1计数器方法,该方法始终将计数器设置为1,因此它会打印两次。尝试初始化您的计数器为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(); 

首先,你是从bar使用nextLine()阅读。用户将进入mars\r\n\r\n是由打回断行)和扫描器读取mars\r\n

然后你从bar阅读nextInt()(!)。用户输入2\r\n,但nextInt()只会读取2,在bar扫描仪上保留\r\n,光标只是超过\r\n

你的逻辑进入第二循环,重印消息,但是当你的bar扫描仪再次击中nextLine(),它只会继续和阅读\r\n - 这个交换机出现故障,你的逻辑进入第三圈( 第二次打印的信息

现在,bar再次为空,所以bar.readLine()将再次等待用户输入。

要解决此问题,请确保您跳过当前行,看完后的整数,所以,当您的扫描仪再次命中nextLine()同时提示,将不只是消耗断行:

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

谢谢!!!这工作完美。使用一台扫描仪而不是像我一样使用4台扫描仪会更好吗? – haroldj97 2014-11-03 19:27:04

+0

@ haroldj97因为你把它们全部连接到'system.in',是的,一台扫描仪就足够了。 – dognose 2014-11-03 19:29:46