如何修复此循环验证?
How do I fix this loop validation?
我是 Java 的新手,我正在尝试执行一个从华氏度到摄氏度的单位转换程序,我对验证循环感到震惊。这就是我得到的。
// 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.
就是这样。它不会回到输入或任何东西,它只是停留在那里直到我输入一个数字然后它回到
What is the temperature in Fahrenheit?:
澄清一下,我的问题仅出在验证循环上,因为当我输入数字时它工作正常。只有当我输入字符串时才会出现问题。
示例代码:
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);
}
}
Input/Output:
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 循环。
这样就可以了
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);
这样就可以了。
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
}
}
我是 Java 的新手,我正在尝试执行一个从华氏度到摄氏度的单位转换程序,我对验证循环感到震惊。这就是我得到的。
// 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.
就是这样。它不会回到输入或任何东西,它只是停留在那里直到我输入一个数字然后它回到
What is the temperature in Fahrenheit?:
澄清一下,我的问题仅出在验证循环上,因为当我输入数字时它工作正常。只有当我输入字符串时才会出现问题。
示例代码:
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);
}
}
Input/Output:
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 循环。
这样就可以了
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);
这样就可以了。
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
}
}