How can I make this statement (inside the while loop) not print twice to the console?

I am very new to Java (and generally programming). I am sure that the solution to my problem is very simple, but I just can not figure it out. The code below is a small part of my program. But it still compiles and still has the same problem.

Here is the code ::

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
    }
}

The first line of code inside the while loop:

System.out.println("What would you like to calculate?: ");

So far so good. When I run the program, it prints: * What would you like to calculate ?: * And then the program will continue as expected.

The problem is that the program reaches the end of the while loop and returns to the beginning of the while loop. he will print:

What would you like to calculate ?: What do you want to calculate ?:

I just can't understand why he prints it twice.

, ( - , - ):

{

, !!
?:

?: 50
?: 50
1,0 /

?: ?: } End

"" . " ".

, , !

!

+3
1

, nextDouble , . . ( ) . "speed", .

Input.nextLine(); t = Input.nextDouble();, .

+4

All Articles