How can I make this statement (within a while-loop) not print out twice to the console?

StackOverflow https://stackoverflow.com/questions/21979008

  •  15-10-2022
  •  | 
  •  

質問

I am very new to Java, (and to programming altogether). I am sure that the solution to my problem is very simple, but I just can't figure it out. The code below is a small chunk of my program. But it is still compile-able, 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 within the while-loop is:

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 continues as expected.

The problem is after the program reaches the end of the while-loop, and returns to the top of the while loop. it will print:

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

I just can't figure out why it prints it out twice.

Here is an example of the exact output I received when running the program (The italic is the output to the console and the bold is my input):

Start{

Hello, I am the Physics Calculator!!
What would you like to calculate?: speed

What is the distance in meters?: 50
What is the time is seconds?: 50
The Speed is 1.0 m/s

What would you like to calculate?:
What would you like to calculate?:
}End

At the end, I can still type 'speed' and continue on with the program. I just want to get rid of the second 'What would you like to calculate'.

Any input regarding the problem that I am having will be highly appreciated!!

Thank you very much for your time!!

役に立ちましたか?

解決

This happens because nextDouble doesn't consume the newline that follows the number. You need to do that separately. Currently, that newline (after the number) gets consumed the next time you are prompted for a command. Since empty string is not "speed", the loop goes through one extra time.

Add Input.nextLine(); after t = Input.nextDouble(); and this will work just fine.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top