Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使此语句(在while循环内)不打印两次到控制台?_Java_While Loop_System.out - Fatal编程技术网

Java 如何使此语句(在while循环内)不打印两次到控制台?

Java 如何使此语句(在while循环内)不打印两次到控制台?,java,while-loop,system.out,Java,While Loop,System.out,我对Java(以及编程)非常陌生。我确信解决我问题的方法很简单,但我就是想不出来。下面的代码是我程序的一小部分。但它仍然是可编译的,并且仍然存在同样的问题 代码如下: import java.util.Scanner; public class Practice{ public static void main(String args[]){ Scanner input = new Scanner(System.in); String command

我对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米/秒

您想计算什么?:
您想计算什么?:
}结束

最后,我仍然可以键入“速度”,继续程序。我只想去掉第二个“你想计算什么”

我们将非常感谢您对我遇到的问题的任何意见


非常感谢您抽出时间

发生这种情况是因为
nextDouble
不使用数字后面的换行符。你需要分开做。当前,该换行符(在数字之后)将在下次提示您输入命令时使用。由于空字符串不是
“speed”
,因此循环会额外执行一次


添加
Input.nextLine()
after
t=Input.nextDouble()这将正常工作。

请考虑java的编码约定。对于命名,您应该使用
input
而不是
input
command
而不是
command
。谢谢!!我对Java的编码约定做了一些研究,你是对的!我没有遵循惯例,所以我编辑了我的帖子(和我的程序),并改正了我的错误。再次感谢!谢谢你,大卫!!当我按照你的建议做的时候,它解决了问题!!尽管我不得不承认,我不明白为什么要添加
Input.nextLine()修复了该问题。但我对编程非常陌生(才两周),我相信随着我学到更多,我会弄明白的。再次感谢@JavaSuffer当您按“回车”提交时,它会在System.in中键入一个新行字符。当您调用nextDouble时,扫描器会读取一些字符,这些字符是它在输入中找到的下一个数字字符串,留下任何剩余的字符。调用nextLine从流中读取下一行,在本例中,它只是nextDouble未读取的最后一个条目中的新行字符。这只是扫描仪和系统之间的一种奇怪的交互。是的,@Radiodef,这是一种很好的表达方式。关于这个完全相同的问题,这个网站上有很多很多问题——使用
nextLine
nextInt
nextDouble
在同一个
扫描仪上的程序。不幸的是,如果你不知道这就是问题所在,就很难知道要搜索什么。我建议您快速搜索“Scanner nextLine”之类的内容,并阅读其他问题的一些答案。