如何解决您可以在这段代码中找到无限循环吗?
我是编程新手。从hyperskills.org学习。 在扫描fill()方法时,我认为程序会执行无限循环。我无法理解Intellij IDEA edu的调试器模块。
package machine;
import java.util.Scanner;
public class CoffeeMachine {
public static Scanner scanner = new Scanner(system.in);
//Initial Amounts
private static int wateramount = 400;
private static int milkAmount = 540;
private static int beansAmount = 120;
private static int disposableCupAmount = 9;
private static int moneyAmount = 550;
//Details of Coffee Types: 0 - espresso,1 - latte,2 - cappuccino
private static int coffeeTypeTemp;
final private static int[][] cofFeedetails = {
{0,250,16,4},{1,350,75,20,7},{2,200,100,12,6}
};
public static void main(String[] args) {
System.out.println("Write action (buy,fill,take,remaining,exit):");
String action = scanner.nextLine();
while (!action.equals("exit")) {
switch (action) {
case "buy":
buy();
System.out.println("Write action (buy,exit):");
action = scanner.nextLine();
break;
case "fill":
fill();
System.out.println("Write action (buy,exit):");
action = scanner.nextLine();
break;
case "take":
take();
System.out.println("Write action (buy,exit):");
action = scanner.nextLine();
break;
case "remaining":
amountOfGoods();
System.out.println("Write action (buy,exit):");
action = scanner.nextLine();
break;
default:
break;
}
}
}
public static void buy() {
System.out.println("What do you want to buy? 1 - espresso,2 - latte,3 - cappuccino,back - to main menu:");
String coffeeType = scanner.nextLine();
// Initial amount changes according to coffee type
switch (coffeeType) {
case "1":
coffeeTypeTemp = 0;
isEnoughTomakeCoffee(coffeeTypeTemp);
break;
case "2":
coffeeTypeTemp = 1;
isEnoughTomakeCoffee(coffeeTypeTemp);
break;
case "3":
coffeeTypeTemp = 2;
isEnoughTomakeCoffee(coffeeTypeTemp);
break;
default:
break;
}
}
public static void fill() {
System.out.println("Write how many ml of water do you want to add:");
int waterFill = scanner.nextInt();
wateramount += waterFill;
System.out.println("Write how many ml of milk do you want to add:");
int milkFill = scanner.nextInt();
milkAmount += milkFill;
System.out.println("Write how many grams of coffee beans do you want to add:");
int beansFill = scanner.nextInt();
beansAmount += beansFill;
System.out.println("Write how many disposable cups of coffee do you want to add:");
int disposableCupFill = scanner.nextInt();
disposableCupAmount += disposableCupFill;
}
public static void take() {
System.out.println("I gave you $" + moneyAmount);
moneyAmount = 0;
}
public static void amountOfGoods() {
System.out.println("The coffee machine has:");
System.out.println(wateramount + " of water");
System.out.println(milkAmount + " of milk");
System.out.println(beansAmount + " of coffee beans");
System.out.println(disposableCupAmount + " of disposable cups");
System.out.println(moneyAmount + " of money");
}
public static void isEnoughTomakeCoffee(int coffeeTypeTemp) {
if ((wateramount - cofFeedetails[coffeeTypeTemp][1]) < 0) {
System.out.println("Sorry,not enough water!");
} else if((milkAmount - cofFeedetails[coffeeTypeTemp][2]) < 0) {
System.out.println("Sorry,not enough milk!");
} else if((beansAmount - cofFeedetails[coffeeTypeTemp][3]) < 0) {
System.out.println("Sorry,not enough coffee beans!");
} else if((disposableCupAmount) < 0) {
System.out.println("Sorry,not enough disposable cups!");
} else {
System.out.println("I have enough resources,making you a coffee!");
remaindersOfGoods(coffeeTypeTemp);
}
}
public static void remaindersOfGoods(int coffeeTypeTemp) {
wateramount -= cofFeedetails[coffeeTypeTemp][1];
milkAmount -= cofFeedetails[coffeeTypeTemp][2];
beansAmount -= cofFeedetails[coffeeTypeTemp][3];
disposableCupAmount--;
moneyAmount += cofFeedetails[coffeeTypeTemp][4];
}
}
解决方法
问题是,您的fill()
函数仅读取整数。
因此,假设您输入以下内容:
"fill\n1\n1\n1\n1\nexit"
其中\n
是换行符。
首先,您调用nextLine,它被正确解析为“ fill”,并且已读取\ n字符,但是它不是输出的一部分。然后将调用fill()
,在此您将nextInt()
称为4x。该功能会读取字符,直到找到任何数字字符为止,并一直读取直到遇到任何空格字符为止。
因此,起初它将读取1
,因为它命中\n
。
然后它将读取\n1
,并再次命中\n
。 -这样就得到整数1。
然后,它将再次读取\n1
,并再次命中\n
。这将再次发生,因此1读取4倍。
然后,您的函数返回到while循环,在其中调用nextLine()
。
但是,此函数将在击中\n
之前读取所有字符。这意味着它将仅读取\n
-解析的输入为空,在换行符前没有字符,在case
中此输入没有switch
,并且也不是等于退出,因此while循环实际上永远不会结束。
要解决此问题,请在scanner.nextLine()
函数的末尾附加fill()
,以便在扫描前读取换行符。
注意:换行符取决于平台,它不一定是\n
。
当fill()方法结束时,您将必须调用scan.nextLine(),否则它将读取空白或在fill()方法中输入的任何此类字符。 同样,您的默认情况只是休息一下,因此,如果操作变量具有您期望的值以外的任何其他值,它将进入无限循环。 因此,也将这些行设置为默认情况:
System.out.println("Write action (buy,fill,take,remaining,exit):");
action = scanner.nextLine();
,
我认为您在使用填充功能后遇到了问题。这是由于未使用nextInt()进行换行而引起的。 您可以在Scanner is skipping nextLine() after using next() or nextFoo()?
了解更多信息以此替换您的填充功能。
public static void fill() {
System.out.println("Write how many ml of water do you want to add:");
int waterFill = Integer.parseInt(scanner.nextLine());
waterAmount += waterFill;
System.out.println("Write how many ml of milk do you want to add:");
int milkFill = Integer.parseInt(scanner.nextLine());
milkAmount += milkFill;
System.out.println("Write how many grams of coffee beans do you want to add:");
int beansFill = Integer.parseInt(scanner.nextLine());
beansAmount += beansFill;
System.out.println("Write how many disposable cups of coffee do you want to add:");
int disposableCupFill = Integer.parseInt(scanner.nextLine());
disposableCupAmount += disposableCupFill;
}
另一种解决方案是在函数末尾简单地添加一个scaner.nextLine()。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。