异常处理与IO

12.2(InputMismatchException异常)编写一个程序,提示用户读取两个整数,然后显示他们的和。程序应该在输入不正确时提示用户在此读取数值。

import java.util.Scanner;
import java.util.InputMismatchException;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
        	     try {
				System.out.print("请输入第一个整数: ");
                int num1 = scanner.nextInt();
                System.out.print("请输入第二个整数: ");
                 int num2 = scanner.nextInt();

                    int sum = num1 + num2;
                    System.out.println("两数之和为: " + sum);
                    break;
                }
            
			    catch (InputMismatchException e) {
				
                System.out.println("输入错误,请重新输入。");
                scanner.nextLine();
			   }
           }         
   }
}

12.3(ArrayIndexOutBoundsException异常)编写一个满足下面要求的程序:
创建一个由100个随机选取的整数构成的数组。
提示用户输入数组的下标,然后显示对应的元素值。如果指定的下标越界,则显示消息“Out
of Bounds".   

import java.util.Scanner;
import java.util.Random;


public class Array {
    public static void main(String[] args) {
        int[] array = new int[100];
        Random random = new Random();

        // 生成 100 个随机整数并填充数组
        for (int i = 0; i < 100; i++) {
            array[i] = random.nextInt(1000);
        }

       
while(true) {
        try {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入数组下标: ");
        int index = scanner.nextInt();
        System.out.println("对应元素值为: " + array[index]);
        break;
        } 
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Out of Bounds");
          
        }
    }
}
}

12.4    IllegalArgumentException异常    修改程序清单10-2 中的 Loan类,如果贷款总额、利率、   
年数小于或等于零,则抛出IllegalArgumentException异常。

public class Load {
    private double loanAmount;
    private double interestRate;
    private int years;

    public Load(double loanAmount, double interestRate, int years) {
        if (loanAmount <= 0 || interestRate <= 0 || years <= 0) {
            throw new IllegalArgumentException("贷款总额、利率、年数不能小于或等于零");
        }
        this.loanAmount = loanAmount;
        this.interestRate = interestRate;
        this.years = years;
    }

    // 其他方法
}

12.7(NumberFormatException 异常)编写 bin2Dec(String binaryString) 方法,将一个二进制字符串转换为一个十进制数。实现 bin2Dec 方法,在字符串不是一个二进制字符串时抛出 NumberFormatException 异常

public class jinzhi2 {

	public static void main(String[] args) {
		try {
		            int result = bin2Dec("10101");
		            System.out.println("转换后的十进制数: " + result);
		        } 
       catch (NumberFormatException e) {
		            System.out.println(e.getMessage());
		        }
		    }
		

		    public static int bin2Dec(String binaryString) {
		        // 检查字符串是否只包含 0 和 1
		        for (char c : binaryString.toCharArray()) {
		            if (c!= '0' && c!= '1') {
		                throw new NumberFormatException("输入的不是二进制字符串");
		            }
		        }

		        int decimal = 0;
		        int power = 0;

		        // 从右往左遍历二进制字符串
		        for (int i = binaryString.length() - 1; i >= 0; i--) {
		            char bit = binaryString.charAt(i);
		            if (bit == '1') {
		                decimal += Math.pow(2, power);
		            }
		            power++;
		        }

		        return decimal;
		    }
		        

	}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值