小楼昨夜-听风雨
2021-05-24 10:40
采纳率: 100%
浏览 42

java异常问题求解

下面代码运行出来输入5和0.5出现错误求:用

try{

}catch(){

}finally{

}来解决答案

package edu.sdp.java.textbook;

import java.util.Scanner;

public class CalculatorTest {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入第一个操作数(整数)");
		int oper1 = scan.nextInt();
		System.out.println("请输入第二个操作数(整数)");
		int oper2 = scan.nextInt();
		System.out.println("------四则运算结果-------");
		System.out.println(oper1+"+"+oper2+"="+Calculator.add(oper1, oper2));
		System.out.println(oper1+"-"+oper2+"="+Calculator.subract(oper1, oper2));
		System.out.println(oper1+"*"+oper2+"="+Calculator.multiply(oper1, oper2));
		System.out.println(oper1+"/"+oper2+"="+Calculator.divide(oper1, oper2));
	}

}









package edu.sdp.java.textbook;

import java.util.Scanner;

public class CalculatorTest2 {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		try{
		System.out.println("请输入第一个操作数(整数)");
		int oper1 = scan.nextInt();
		System.out.println("请输入第二个操作数(整数)");
		int oper2 = scan.nextInt();
		System.out.println("------四则运算结果-------");
		System.out.println(oper1+"+"+oper2+"="+Calculator.add(oper1, oper2));
		System.out.println(oper1+"-"+oper2+"="+Calculator.subract(oper1, oper2));
		System.out.println(oper1+"*"+oper2+"="+Calculator.multiply(oper1, oper2));
		System.out.println(oper1+"/"+oper2+"="+Calculator.divide(oper1, oper2));
		}catch(ArithmeticException ex){
			System.out.println("除数不能为0,请重新输入");
		}finally{
			scan.close();
		}
	}

}

错误问题

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • 答题大神 2021-05-24 14:48
    已采纳
    Scanner scan = new Scanner(System.in);
            try {
                System.out.println("请输入第一个操作数(整数)");
                int oper1 = scan.nextInt();
                System.out.println("请输入第二个操作数(整数)");
                int oper2 = scan.nextInt();
                System.out.println("------四则运算结果-------");
                System.out.println(oper1+"+"+oper2+"="+Calculator.add(oper1, oper2));
    		    System.out.println(oper1+"-"+oper2+"="+Calculator.subract(oper1, oper2));
    		    System.out.println(oper1+"*"+oper2+"="+Calculator.multiply(oper1, oper2));
    		    System.out.println(oper1+"/"+oper2+"="+Calculator.divide(oper1, oper2));
            } catch (ArithmeticException ex) {
                System.out.println("除数不能为0,请重新输入");
            } catch (InputMismatchException ex2){//多加一个异常类型处理,跟处理分母不能为0一样
                System.out.println("输入的不是整数,请重新输入");
            }finally{
                scan.close();
            }

    这样子就可以处理那个报错了,记得采纳哦

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • CSDN专家-sinjack 2021-05-24 12:33
     public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            try {
                System.out.println("请输入第一个操作数(整数)");
                int oper1 = scan.nextInt();
                System.out.println("请输入第二个操作数(整数)");
                int oper2 = scan.nextInt();
                System.out.println("------四则运算结果-------");
                System.out.println(oper1+"+"+oper2+"="+Calculator.add(oper1, oper2));
    		    System.out.println(oper1+"-"+oper2+"="+Calculator.subract(oper1, oper2));
    		    System.out.println(oper1+"*"+oper2+"="+Calculator.multiply(oper1, oper2));
    		    System.out.println(oper1+"/"+oper2+"="+Calculator.divide(oper1, oper2));
            } catch (ArithmeticException ex) {
                System.out.println("除数不能为0,请重新输入");
            } catch (InputMismatchException ex2){//把异常类型加上就好了。跟处理分母不能为0一样
                System.out.println("输入的不是整数,请重新输入");
            }finally{
                scan.close();
            }
        }

    把异常类型加上就好了。跟处理分母不能为0一样

    评论
    解决 无用
    打赏 举报
  • 答题大神 2021-05-24 10:49

    该问题是因为nextInt()只能输入整数,你输入0.5,数据类型转换错误啦

    评论
    解决 无用
    打赏 举报
  • 小楼昨夜-听风雨 2021-05-24 11:47

    我的意思是用

    try{

    }catch(){

    }finally{

    }

    的形式改正错误

    评论
    解决 无用
    打赏 举报
  • 有问必答小助手 2021-05-26 18:19

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题