2015-07-10 117 views
1

我是非常新的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?: 

爲了澄清,我的問題是隻有在確認循環,因爲當我輸入一個數字,它工作得很好。只有在輸入字符串時纔會出現問題。

+0

'input.nextLineDouble();'?這對我不起作用。 – lokilindo

回答

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循環。
+0

Gosu,我仍然有同樣的問題。 – lokilindo

+0

如果我做'fahrenheit = input.nextDouble(); isNumber = true; //將此設置爲true!''isNumber'永遠不會是'false','while(!isNumber)'''變得過時 – lokilindo

+0

我已經在'do {'開始時編寫了'isNumber = true;''循環 – lokilindo

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); 
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 

    } 

}