0

我很难弄清楚为什么 while 循环实际上不会循环。它运行一次并停止。

import java.util.*;

public class mileskm {
    public static void main(String[] args) {

        Scanner inp = new Scanner(System.in);
        boolean continue1 = true;

        while (continue1 == true) {

            System.out.println("Would you like to convert m-km or km-m(km for m-km and m for km-m)");

            String convert = inp.nextLine();
            if (convert.equalsIgnoreCase("km")) {
                System.out.println("Enter the mileage to be converted.");
                double num = inp.nextDouble();
                double con = num * 1.60934;
                System.out.println(con);
                continue1 = true;
            } else if (convert.equalsIgnoreCase("m")) {
                System.out.println("Enter the km to be converted.");
                double num = inp.nextDouble();
                double con = num * 0.621371;
                System.out.println(con);
                continue1 = true;
            } else {
                continue1 = false;
            }

        }
    }
}

我正在尝试使其循环,以便用户能够多次转换单位。欢迎任何和所有帮助!

4

4 回答 4

1

The problem is that when you call nextDouble(), it consumes the number but not the newline that comes after the number. To fix this, simply put a line of code inp.nextLine(); after calling nextDouble().


Example and full explanation:

Suppose your input "km", press enter, "123", press enter. Then from the program's point of view, the input stream is "km\n123\n".

The code String convert = inp.nextLine(); gets the value "km" and also advances the input past the first "\n".

The code double num = inp.nextDouble(); gets the string "123" and returns the value (double)123.0 . It stops parsing when it sees the '\n', but it does not consume the character - it remains in the input buffer.

In the next iteration of the loop, inp.nextLine(); sees "\n" immediately, so the String convert = "";. This triggers the else case in your loop, so it exits the loop.

于 2013-08-10T15:52:34.333 回答
1

If either of the two conditions:

(convert.equalsIgnoreCase("km"))

or

(convert.equalsIgnoreCase("m"))

... is not true, you set your boolean continue1 to false, which will break your loop at the next iteration.

Please also note that since it's a boolean, you can (and should) write your while statement as such:

while (continue1) {
  // etc.
}

Note also that you should try/catch for Exceptions when you scan for specific types, e.g. Scanner.nextDouble, as this might very well break your code with a stack trace if the input is of an unexpected type.

于 2013-08-10T15:54:04.103 回答
1

您的代码中仍有一个问题在您的 while 循环中初始化您的扫描仪对象,它将解决您打印该行 2 次的问题。用这个

import java.util.*;

public class mileskm {
public static void main(String[] args) {

    Scanner inp;
    boolean continue1 = true;

    while (continue1) {

        System.out.println("Would you like to convert m-km or km-m(km for m-km and m for km-m)");
       inp = new Scanner(System.in);

        String convert = inp.nextLine();
        if (convert.equalsIgnoreCase("km")) {
            System.out.println("Enter the mileage to be converted.");
            double num = inp.nextDouble();
            double con = num * 1.60934;
            System.out.println(con);
            continue1 = true;
        } else if (convert.equalsIgnoreCase("m")) {
            System.out.println("Enter the km to be converted.");
            double num = inp.nextDouble();
            double con = num * 0.621371;
            System.out.println(con);
            continue1 = true;
        } else {
            break;
        }

    }
}
}

请检查这是您在评论中写的问题的答案

于 2013-08-10T16:26:47.990 回答
0

利用

String convert = inp.next();

代替

String convert = inp.nextLine();

由于 System.out.println(),nextLine() 正在读取空行。

于 2013-08-10T16:02:19.100 回答