weixin_43436017 2018-10-25 06:09 采纳率: 33.3%
浏览 579
已结题

第8行为什么出错了?求大神指点

//捕捉被零除的异常(ArithmeticException类型的异常)
import java.io.*;
public class ExceptionTester3 {
public static void main(String args[]) {
int number1=0,number2=0,result=0;
try {
System.out.println("Enter the first number:");
number1=Integer.valueOf(Keyboard.getString()).intValue();
System.out.println("Enter the second number:");
number2=Integer.valueOf(Keyboard.getString()).intValue();
result=number1/number2;
}catch(NumberFormatException e) {
System.out.println("Invalid integer entered!");
System.exit(-1);
}catch(ArithmeticExecption e) {
System.out.println("Second number is 0,cannot do division!");
System.exit(-1);
}
System.out.println(number1+"/"+number2+"="+result);
}
}

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Keyboard cannot be resolved
Keyboard cannot be resolved
ArithmeticExecption cannot be resolved to a type

at ExceptionTester3.main(ExceptionTester3.java:8)
  • 写回答

4条回答

  • 千寻啊千寻 2018-10-25 06:25
    关注

    你这个代码贴的很有问题啊,不知道你第八行是哪里,另外不知道Keyboard是啥?于是乎,我写了一下,如下:

    Test.java

    package com.qianxunclub.chapter3;
    
    public class Test {
    
            public static void main(String args[]) {
                int number1=0,number2=0,result=0;
                try {
                    System.out.println("Enter the first number:");
                    number1=Integer.valueOf(Keyboard.getString()).intValue();
                    System.out.println("Enter the second number:");
                    number2=Integer.valueOf(Keyboard.getString()).intValue();
                    result=number1/number2;
                }catch(NumberFormatException e) {
                    System.out.println("Invalid integer entered!");
                    System.exit(-1);
                }catch(Exception e) {
                    System.out.println("Second number is 0,cannot do division!");
                    System.exit(-1);
                }
                System.out.println(number1+"/"+number2+"="+result);
            }
    }
    

    下面是Keyboard.java

    package com.qianxunclub.chapter3;
    
    import java.util.Scanner;
    
    public class Keyboard {
    
        static Scanner in = new Scanner(System.in);
    
        public static String getString() {
    
            return in.next();
    
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题