测试 NFE 的 Catch 语句 [英] Testing Catch Statements of a NFE

查看:31
本文介绍了测试 NFE 的 Catch 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我是编程课程的入门学生,我正在尝试测试 NFE 的 catch 语句.我不知道如何正确格式化代码,但在这里.

Alright so I am an intro student in a programming class and I am trying to test a catch statement of a NFE. I don't know how to format the code properly, but here it is.

import java.util.Scanner;

public class Geo {

    public static void main(String[] args) {

        try {
            Scanner inp = new Scanner(System.in);
            System.out.print("Name?");
            String name = inp.nextLine();
            System.out.print("Number?");
            double num = inp.nextDouble();
            System.out.print("Integer?");
            int num2 = inp.nextInt();
        } catch(NumberFormatException e) {
            System.out.println("Usage error");
        }
        System.out.println(name);
        System.out.println(num);
        System.out.println(num2);   
    }

}

它一直说变量,name,num 和 num2 是未定义的.我在这里做错了什么,因为我正在回顾一个旧实验室,这正是我以前所做的.有什么提示吗?

It keeps saying the variables, name, num, and num2 are undefined. What am I doing wrong here, because I was looking back at an old lab and that is exactly how I had done it before. Any hints?

现在我修复了它,代码看起来像这样

Now I fixed it so the code looks like this

public static void main(String[] args) {

public static void main(String[] args) {

        try {
            Scanner inp = new Scanner(System.in);
            System.out.print("Name?");
            String name = inp.nextLine();
            System.out.print("Number?");
            double num = inp.nextDouble();
            System.out.print("Integer?");
            int num2 = inp.nextInt();
            System.out.println(name);
            System.out.println(num);
            System.out.println(num2);
        } catch(NumberFormatException e) {
            System.out.println("Usage error");
        }

    }

但是捕获没有运行.这怎么解决.就像我希望它完全运行 try 一样,但如果出现问题,它会继续运行,然后尝试退出,然后发现问题.

but the catch isnt running. How does that get fixed. Like I want it run the try completely, but if something is wrong it keeps running, then out of try, then catches the issue.

推荐答案

你必须在 try/catch 之上初始化变量.

You must initial variables on top of try/catch.

public static void main(String[] args) {
    String name = null;
    double num = 0;
    int num2 = 0;
    try {
        Scanner inp = new Scanner(System.in);
        System.out.print("Name?");
        name = inp.nextLine();
        System.out.print("Number?");
        num = inp.nextDouble();
        System.out.print("Integer?");
        num2 = inp.nextInt();
    } catch (Exception e) {
        System.out.println("Usage error");
    }
    System.out.println(name);
    System.out.println(num);
    System.out.println(num2);
}

public static void main(String[] args) {
    try {
        Scanner inp = new Scanner(System.in);
        System.out.print("Name?");
        String name = inp.nextLine();
        System.out.print("Number?");
        double num = inp.nextDouble();
        System.out.print("Integer?");
        int num2 = inp.nextInt();

        System.out.println(name);
        System.out.println(num);
        System.out.println(num2);
    } catch (Exception e) {
        System.out.println("Usage error");
    }
}

这篇关于测试 NFE 的 Catch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆