扫描程序无法获取输入的循环

时间:2013-09-24 00:06:57

标签: java while-loop java.util.scanner

我在与原点计算器程序的距离中创建错误信息时出现问题。

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

2 个答案:

答案 0 :(得分: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课程以获取更多信息。

答案 1 :(得分: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");

}

}