我对Java(以及一门编程)非常陌生.我敢肯定,解决我的问题的方法很简单,但我只是想不通.下面的代码是我程序的一小部分.但是它仍然是可编译的,并且仍然存在相同的问题.
这是代码:
import java.util.Scanner;
public class Practice{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String command = "";
double d; // Distance
double t; // Time
double s; // Speed
System.out.println("Hello, I am the Physics Calculator!!");
while (!command.equals("end")){
System.out.print("What would you like to calculate?: "); // after the first loop, this statement is printed twice to the screen! :(
command = input.nextLine();
System.out.println();
if (command.equals("speed")){
System.out.print("What is the distance in meters?: ");
d = input.nextDouble();
System.out.print("What is the time is seconds?: ");
t = input.nextDouble();
s = d / t;
System.out.println("The Speed is "+ s +" m/s");
System.out.println();
}
} //End of the while-loop
}
}
while循环中的第一行代码是:
System.out.println("What would you like to calculate?: ");
到现在为止还挺好.当我运行该程序时,它将打印:*您要计算什么?:*
然后程序按预期继续.
问题出在程序到达while循环的末尾并返回while循环的顶部之后.它会打印:
您要计算什么?:
?您要计算什么?:
我只是不知道为什么它打印两次.
这是我在运行程序时收到的确切输出的示例(斜体是控制台的输出,粗体是我的输入):
开始{
您好,我是物理计算器!!
您想计算什么?:速度
米的距离是多少?:50
什么时间是秒?:50
速度为1.0 m / s
您要计算什么?:
您要计算什么?:
}结束
最后,我仍然可以输入“ speed”并继续执行程序.我只想摆脱第二个“您想计算什么”.
任何有关我遇到的问题的意见将不胜感激!!
非常感谢您的宝贵时间!!