如何修复此循环验证?

时间:2015-07-10 01:47:30

标签: java validation while-loop

我对Java非常陌生,我正在尝试从华氏温度到摄氏温度的单位转换程序,我对验证循环感到震惊。这就是我得到的。

BUILD SUCCESS

您可以看到我要验证的是用户没有输入字符串。但是当我运行该程序时,它会陷入某种循环而且会说

    // Validation
    do {
        isNumber = true;

        System.out.print("What is the temperature in Fahrenheit?: ");

        // If alphabetical characters are entered
        while (!input.hasNextDouble()) {
            System.out.println("Oops! Try entering only numerical characters.");
            System.out.println();

            isNumber = false;
            input.next();
        }
        fahrenheit = input.nextDouble();

    } while (!isNumber);

就是这样。它没有回到输入或任何东西,它只是停留在那里,直到我输入一个数字,然后它回到

    What is the temperature in Fahrenheit?: something <-- what I input
    Oops! Try entering only numerical characters.

为了澄清,我的问题仅在于验证循环,因为当我输入一个数字时,它的工作正常。只有在输入字符串时才会出现此问题。

3 个答案:

答案 0 :(得分:0)

示例代码:

import java.util.Scanner;

public class QuickTester {

    public static void main(String[] args) {

        double fahrenheit;
        Scanner input = new Scanner(System.in);

        // Validation
        while(true) {

            System.out.print("What is the temperature in Fahrenheit?: ");

            // If alphabetical characters are entered
            if (!input.hasNextDouble()) {
                System.out.println("Oops! " +
                        "Try entering only numerical characters.\n");

                // Clear away erroneous input
                input.nextLine();
            }
            else {

                fahrenheit = input.nextDouble();
                break; // Get out of while loop
            }
        }

        input.close();
        System.out.println("Temperature in Fahrenheit: " + fahrenheit);
    }
}

<强>输入/输出:

What is the temperature in Fahrenheit?: abc
Oops! Try entering only numerical characters.

What is the temperature in Fahrenheit?: banana
Oops! Try entering only numerical characters.

What is the temperature in Fahrenheit?: 36.5
Temperature in Fahrenheit: 36.5

注意:

  • 修改了代码。在do-while循环中不需要while循环。
  • 检查输入是否为double,如果确实是double值,则跳出while循环。

答案 1 :(得分:0)

这样的事情会做

    Scanner input = new Scanner(System.in);
    double fahrenheit;

    do  {
        System.out.print("What is the temperature in Fahrenheit?: ");
        try  {
            fahrenheit = input.nextDouble();
            //Do your work
            break;
        } catch (InputMismatchException ex) {
            input.nextLine();
            System.out.println("Oops! Try entering only numerical characters.");
            System.out.println();
        }
    } while (true); 

答案 2 :(得分:0)

这样就可以了。

public class Main
{

    public static void main( String[] args )
    {

        Scanner input = new Scanner( System.in );

        System.out.print( "What is the temperature in Fahrenheit?: " );

        // If alphabetical characters are entered
        while ( !input.hasNextDouble() )
        {
            System.out.println( "Oops! Try entering only numerical characters." );
            System.out.println();

            input.next();
        }
        //Do Fahrenheit to Celsius calculation

    }

}