这是驱动程序类。 问题出在开关上,直到我输入带字母的长度/宽度值来测试它是否捕获到异常,它都能正常工作,它将捕获该异常,但是从那一点开始,它将不允许您输入新的长度/宽度值每次我输入新的长度/宽度值时,它只会显示“输入介于0.0-20.0之间的长度值:请输入介于0.0-20.0之间的数字”
package week.pkg6.assignment.assignment.pkg7;
import java.util.*;
public class TestRectangle {
public static void main(String[] args)throws Exception
{
Scanner input = new Scanner( System.in );
Rectangle TestRectangle = new Rectangle();
int choice = getMenuChoice();
//week6assignment:Exercise 8.4 (TestRectangle)
while ( choice != 3 )
{
switch ( choice )
{
case 1:
try{
//prompt user to enter a length value between 0.0 - 20.0
System.out.print( "Enter length value between 0.0 - 20.0: " );
//user enters a length value
TestRectangle.setLength( input.nextDouble());
}catch(Exception e)
{System.out.println("Please enter a number between 0.0-20.0:");}
break;
case 2:
try{
//prompt user to enter a width value between 0.0 - 20.0
System.out.print ( "Enter width value between 0.0 - 20.0: " );
//user enters a width value
TestRectangle.setWidth( input.nextDouble());
}catch(Exception e)
{System.out.println("Please enter a number between 0.0-20.0:");}
break;
} // end switch
System.out.println ( TestRectangle.toString() );
System.out.println();
choice = getMenuChoice();
}// end while
} // end main
private static int getMenuChoice()throws Exception {
Scanner input = new Scanner( System.in );
System.out.println( "1. Set Length" );
System.out.println( "2. Set Width" );
System.out.println( "3. Exit" );
System.out.print( "Choice: " );
try{
return input.nextInt();
}catch(Exception e){System.out.println("Please enter 1, 2, or 3");}
return 0;
}//end method getMenuChoice
}//end clss
这是上述课程所驾驶的课程
package week.pkg6.assignment.assignment.pkg7;
public class Rectangle
{
private double length; // the length of the rectangle
private double width; // the width of the rectangle
public Rectangle()
{
setLength( 1.0f );
setWidth( 1.0f );
} // end Rectangle no-argument constructor
// constructor with length and width supplied
public Rectangle( double theLength, double theWidth ){
setLength( theLength );
setWidth( theWidth );
} // end Rectangle two-argument constructor
// validate and set length
public void setLength( double theLength )
{
length = ( theLength >= 0.0 && theLength <= 20.0 ? theLength : 1.0f );
}// end method setLength
// validate and set width
public void setWidth( double theWidth ){
width = ( theWidth >= 0.0 && theWidth <= 20.0 ? theWidth : 1.0f );
} // end method setWidth
// get value of length
public double getLength()
{
return length;
} // end method getLength
// get value of width
public double getWidth()
{
return width;
} // end method getWidth
// calculate rectangle's perimeter
public double perimeter()
{
return 2 * length + 2 * width;
} // end method perimeter
// calculate rectangle's area
public double area()
{
return length * width;
} // end method area
public String toString()
{
return String.format( "%s: %f\n%s: %f\n%s: %f\n%s: %f",
"Length", length, "Width", width,
"Perimeter", perimeter(), "Area", area() );
}//end method string
}//end class
答案 0 :(得分:0)
删除此行:
Scanner input = new Scanner( System.in );
也来自getMenuChoice()
和main()
的
并放在类级别,因此您在整个代码中仅使用1个Scanner
对象。
同时存在多个打开的Scanner
对象可能是问题的根源。