首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >你在这段代码/程序中发现了什么错误?错误:它不会运行,除非你先输入一个数字

你在这段代码/程序中发现了什么错误?错误:它不会运行,除非你先输入一个数字
EN

Stack Overflow用户
提问于 2012-03-18 17:13:42
回答 3查看 154关注 0票数 2

所以我的问题是:这个程序应该计算圆的面积,用户输入一个数字,得到答案,也不允许任何无效的输入,我使用try-catch,但它不起作用…

非常感谢大家抽出时间来:))

代码如下:

代码语言:javascript
复制
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);

    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-18 17:20:03

您必须像这样捕获InputMismatchException异常:

代码语言:javascript
复制
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.
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2012-03-18 17:15:29

你应该把这两行-

代码语言:javascript
复制
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r); 

try块内部-

代码语言:javascript
复制
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块下面添加以下块-

代码语言:javascript
复制
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.
}
票数 0
EN

Stack Overflow用户

发布于 2012-03-18 17:16:33

我编辑了答案,这样如果输入错误,它会再次询问用户

您需要将nextDouble放在try子句中:

代码语言:javascript
复制
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后面添加了这一行,就好像您无法解析数字一样,它们不应该被打印出来。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9757193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档