【发布时间】:2020-05-05 20:25:55
【问题描述】:
我正在尝试制作一个程序来计算某人的步数(如果数字大于或等于 10000,该程序应该停止),但我似乎找不到输入“回家”的方法",然后输入回家所需的步数。
Scanner scan = new Scanner(System.in);
int totalSteps = 0;
while(true)
{
int steps = scan.nextInt();
totalSteps = totalSteps + steps;
if(totalSteps >= 10000) {
System.out.println("Goal reached! Good job!");
break;
}
else if(steps < 10000)
{
String home = scan.nextLine();
if(home.equals("Going home"))
{
int extraSteps = scan.nextInt();
totalSteps = totalSteps + steps + extraSteps;
System.out.println(10000 - totalSteps + " more to reach goal.");
}
}
}
【问题讨论】: