2013-09-24 74 views
0

在我的距離原點計算器程序中創建錯誤消息時出現問題。掃描儀無法讀取輸入的循環

double x;         // x coordinate 
double y;         // y coordinate 
String quadrant;       //new string: quadrant 

System.out.println("Enter x coordinate");//prompt user to enter x coordinate 

do{System.out.println ("Error: Enter a number");}while (!input.hasNextDouble()); 

x = input.nextDouble()+.0; 

當我運行該程序時,我會在提示輸入值後直接收到錯誤消息。如果我輸入了一個無效的字符,它會啓動一個帶有錯誤的無限循環。如果我將do-while循環移到input.nextdouble之後,它什麼也不做。

完整代碼:

package distancefromorigin; 
/** 
* Distance from origin calculator application 
* Created for CSCI 11 
* Last modified 9/22/2013 2:30 AM 
* @author Steve Pesce 
*/ 
import java.util.Scanner;       //Start Scanner utility 

public class DistanceFromOrigin 
{ 
    public enum Quad //initialize enum Quad with values 1-4 in roman numerals 

    {I , II, III, IV} 


/** 
* main method calculates distance from a point to 
* the origin using the Cartesian coordinate system 
* and locates the quadrant that the point lies in 
*/  


    public static void main(String[] args) 
     { 

     Scanner input = new Scanner (System.in); // new scanner (imput) 

     Quad loc;         //new Quad : loc (quadrant #) 

     double dist;        //distance from (x,y) to (0,0) 

     double x;         // x coordinate 

     double y;         // y coordinate 

     String quadrant;       //new string: quadrant 


     System.out.println("Enter x coordinate");//prompt user to enter x coordinate 






     x = input.nextDouble()+.0;   //save next number entered as x 

     do 
     { 
      System.out.println ("Error: Enter a number"); 

     }while (!input.hasNextDouble()); 

     System.out.println("Enter y coordinate"); 

     do 
     { 
      System.out.println ("Error: Enter a number"); 
     } 
     while (!input.hasNextDouble()); 


     //prompt user to enter y coordinate 
     y = input.nextDouble()+ .0;   //save next number entered as y 


     if (x == 0)         //if x=0 and y doesn't = 0 
     {  

      if (y >= 0)     //and if y greater than or equal to zero 
      {     
       dist = y;          //then distance is y 
      } 

      else            //if y is negative 
      {  
       dist = -y ;        //then distance is y(-1) 
      } 
     }     

     else if ((y == 0) && (x != 0))    // if y=0 and x doesn't = 0 
     { 
      if (x > 0)         // and if x is positive 

      { 
        dist = x ;        // then distance is x 
      } 

      else            // if x is negative 
      { 
         dist = -x ;      // then distance is x(-1) 
      } 

     } 

     //if x and y both don't equal zero then use hypotenuse formula 
     else dist = (Math.hypot ((double) x , (double) y)); 


     if (x >= 0)   //if x is non negative 
     { 
      if (y < 0)   //and if y is negative 
      { 
       loc = Quad.IV;//then point is in quadrant IV 
      } 
      else loc = Quad.I;//if x and y are both nonnegative then the point 
     }      //is in quadrant I 
     else if (y < 0)  //if x is negative and y is negative 
     {      
      loc = Quad.III; //then the point is in quadrant III 
     } 
     else loc = Quad.II; //if x is negative and y is nonnegative then the 
          //point is in quadrant II 




     //Print "the point (x,y) is (dist) units away from the origin (0,0) 
     // and is in quadrant (loc)" 
      System.out.println("The point(" 
        + (int)x + "," + (int)y + ") is " + (double)dist 
        +" units from the origin (0,0) and is in Quadrant " + 
           loc + "."); 








}//end main() 
}//end class DistanceFromOrigin 
+0

這是Java.and此評論是足夠長的時間。 –

+0

'Scanner's和'System.out'是Java的東西。 –

+0

你永遠不會清除任何無效的輸入。這就是爲什麼它無限循環 – Sinkingpoint

回答

0

當您運行

do{ 
    System.out.println ("Error: Enter a number"); 
}while (!input.hasNextDouble()); 

input.hasNextDouble()檢查是否有double在命令行中等待。如果您得到的字符串是input.next(),並且每次檢查它是否是dobule,程序都會適當地等待。

可以代替做

double double1=-1; 
while (double1!=-1){ 
    String number=input.next(); 
    try{ 
     double1=Double.parseDouble(number); 
    }catch(NumberFormatException e){ 
     //that wasn't proper input. 
     System.out.println ("Error: Enter a number"); 

    } 
} 

看那DoubleScanner類的更多信息。

+0

好的謝謝。我想我可能會嘗試做比我迄今爲止學到的更先進的東西。 – user2809114

+0

試着解析一個充滿數字的文件,這樣你就不必等待用戶輸入,否則就認爲用戶輸入的數據是正確的,不要試圖驗證它。 –

+1

通過捕獲拋出的異常來檢查字符串是否是雙重的,當你試圖解析它時,檢查是非常容易的,然後當你離開循環時你也有了x解析。 –

0

我不認爲你的do-while循環結構正確。它會給出100%的錯誤信息。

試着這麼做:

,而(真){

String temp = input.next(); //get next token of input 
try{ 

x = Double.parseDouble(temp);  //try to parse a double 

break;        //break out of the loop if double was parsed 

}catch(NumberFormatException e){ //if the token isnt a double, handle exception 

System.out.println("Error message"); 

} 

}