所以我的问题是:这个程序应该计算圆的面积,用户输入一个数字,得到答案,也不允许任何无效的输入,我使用try-catch,但它不起作用…
非常感谢大家抽出时间来:))
代码如下:
import java.util.Scanner;
import java.io;
/**
*
* @author Osugule
*/
public class AreaCircle {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
System.out.println("Enter radius:");//Print to screen
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
try {
}
catch( NumberFormatException e ) {
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
}
}发布于 2012-03-18 17:20:03
您必须像这样捕获InputMismatchException异常:
import java.util.InputMismatchException;
import java.util.Scanner;
public class AreaCircle {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
System.out.println("Enter radius:");//Print to screen
try {
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
}
catch( InputMismatchException e ) {
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
catch( NumberFormatException e ) {
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
}
}发布于 2012-03-18 17:15:29
你应该把这两行-
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r); 在try块内部-
import java.util.Scanner;
import java.io;
/**
*
* @author Osugule
*/
public class AreaCircle
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
boolean correct_input = false;
while(correct_input == false)
{
System.out.println("Enter radius:");//Print to screen
try
{
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
correct_input = true;
}
catch( NumberFormatException e )
{
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
catch( InputMismatchException e )
{
System.out.println("Input Mismatch, please enter a number");
//put a message or anything you want to tell the user that there is an
//input mismatch.
}
}
}
}从documentation -
公共双nextDouble()
将输入的下一个标记扫描为双精度。如果下一个标记无法转换为有效的双精度值,则此方法将抛出InputMismatchException。如果翻译成功,扫描程序将通过匹配的输入。
抛出:
InputMismatchException -如果下一个标记与浮点正则表达式不匹配,或超出范围。
NoSuchElementException -如果输入耗尽
IllegalStateException -如果此扫描仪已关闭
所以如果你输入了一个无效的字符,sc.nextDouble()可能会抛出一个不会被catch块捕获的InputMismatchException,因为它只捕获NumberFormatException。因此,您的程序将在抛出异常后终止,但您不会收到任何消息。
要处理此场景,请在当前的catch块下面添加以下块-
catch( NumberFormatException e )
{
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
// New catch block
catch( InputMismatchException e )
{
System.out.println("Input Mismatch, please enter a number");
//put a message or anything you want to tell the user that there is an
//input mismatch.
}发布于 2012-03-18 17:16:33
我编辑了答案,这样如果输入错误,它会再次询问用户
您需要将nextDouble放在try子句中:
boolean inputProcessed = false;
while (!inputProcessed) {
try {
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
// this indicates that we were able to process the input, and we should stop now.
inputProcessed = true;
}
catch( NumberFormatException e ) {
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
}注意,我在try子句的try-catch后面添加了这一行,就好像您无法解析数字一样,它们不应该被打印出来。
https://stackoverflow.com/questions/9757193
复制相似问题