Java 扫描仪不获取输入的循环
我在我到原点的距离计算器程序中创建错误消息时遇到问题Java 扫描仪不获取输入的循环,java,while-loop,java.util.scanner,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");
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
包装与原产地的距离;
/**
*距离原点计算器应用程序
*为CSCI 11创建
*最后修改日期:2013年9月22日凌晨2:30
*@作者史蒂夫·佩斯
*/
导入java.util.Scanner//启动扫描仪实用程序
公共类与原点的距离
{
公共枚举四元//使用罗马数字中的值1-4初始化枚举四元
{I、II、III、IV}
/**
*main方法计算从一点到另一点的距离
*使用笛卡尔坐标系的原点
*并定位点所在的象限
*/
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);//新扫描仪(输入)
Quad loc;//新的Quad:loc(象限#)
双距离;//从(x,y)到(0,0)的距离
双x;//x坐标
双y;//y坐标
字符串象限;//新字符串:象限
System.out.println(“输入x坐标”);//提示用户输入x坐标
x=input.nextDouble()+.0;//将输入的下一个数字另存为x
做
{
System.out.println(“错误:输入一个数字”);
}而(!input.hasNextDouble());
System.out.println(“输入y坐标”);
做
{
System.out.println(“错误:输入一个数字”);
}
而(!input.hasNextDouble());
//提示用户输入y坐标
y=input.nextDouble()+.0;//将输入的下一个数字另存为y
if(x==0)//如果x=0而y不=0
{
如果(y>=0)//如果y大于或等于零
{
dist=y;//那么距离就是y
}
else//如果y为负
{
dist=-y;//那么距离就是y(-1)
}
}
else if((y==0)&&(x!=0))//如果y=0,x不=0
{
如果(x>0)//如果x为正
{
dist=x;//那么距离就是x
}
else//如果x为负
{
dist=-x;//那么距离就是x(-1)
}
}
//如果x和y都不等于零,那么使用斜边公式
else dist=(Math.hypot((双)x,(双)y));
if(x>=0)//如果x是非负的
{
if(y<0)//如果y为负
{
loc=Quad.IV;//那么点在象限IV中
}
else loc=Quad.I;//如果x和y都是非负的,则点
}//在象限I中
else如果(y<0)//如果x为负,y为负
{
loc=Quad.III;//那么该点位于象限III中
}
else loc=Quad.II;//如果x为负,y为非负,则
//点在象限二
//打印“点(x,y)距离原点(0,0)为(距离)单位
//并且位于象限(loc)”
System.out.println(“点(”
+(int)x+”,“+(int)y+”)是“+(双)距离
+“距离原点(0,0)的单位,且位于象限中”+
loc+”);
}//尾干管()
}//结束类与原点的距离
当您运行
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");
}
}
查看和类以了解更多信息。我认为do-while循环的结构不正确。它会在100%的时间内给出错误消息 尝试以下方法: while(true){
这是Java。这条评论足够长。
Scanner
s和System.out
都是Java的东西。你从来不会清除任何无效的输入。这就是为什么它是无限循环的。谢谢。我想我可能正在尝试做一些比我目前所学的更高级的事情。试着解析一个满是数字的文件,这样你就不必ait用于用户输入,或者假设用户已正确输入数据,而不尝试验证它。检查字符串是否为双精度字符串要容易得多,只需捕获尝试解析时抛出的异常,然后在离开循环时解析x。
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");
}
}