gpt4 book ai didi

java - 为什么这条线打印两次?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:23 24 4
gpt4 key购买 nike

我已经创建了一个程序,作为我检查库存的计算工作的一部分,它运行良好,但是,在用户完成该程序后,一旦检查了一件商品的库存,就会询问他们是否要检查另一件商品的库存同一行被打印两次的项目,我不知道为什么?这是代码:

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

最佳答案

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

    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

那么您正在从 bar 读取 nextInt() (!)。用户输入2\r\n,但是nextInt()只会读取2,留下\r\nbar 扫描器上,光标刚好\r\n 前面。

您的逻辑进入第二个 循环,重新打印消息,但是当您的bar 扫描器命中nextLine() ,它将继续并读取 \r\n - 开关在此失败并且您的逻辑进入第三个循环(第二次打印消息)

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

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

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

关于java - 为什么这条线打印两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26720992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com