JAVA初学——week13第十二章课本练习12.6-12.10

12.6

import java.util.Scanner;

public class NumberformatexceptionException12_6 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a hex number: ");
        String hex = input.nextLine();
        hexToDec(hex.toUpperCase());
    }

    public static void hexToDec(String hex){
        int num = 0;
        try{
            for(int i = 0;i < hex.length(); ++i){
                char hexChar = hex.charAt(i);
                if(hexChar >= '0' && hexChar <= '9'){
                    num = num * 16 + hexChar - '0';
                }else if( hexChar >= 'A' && hexChar <= 'F'){
                    num = num * 16 + hexChar - 'A' + 10;
                }else{
                    throw new Exception("NumberFormatException");
                }
            }
            System.out.println("The decimal value for hex number " + hex + " is " + num);
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}

12.7

import java.util.Scanner;

public class NumberformatexceptionException12_7 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a hex number: ");
        String hex = input.nextLine();
        bin2Dec(hex);
    }

    public static void bin2Dec(String hex){
        int num = 0;
        try{
            for(int i = 0;i < hex.length(); ++i){
                char binChar = hex.charAt(i);
                if(binChar == '1' || binChar == '0'){
                    num = num * 2 + binChar - '0';
                }
                else{
                    throw new Exception("NumberFormatException");
                }
            }
            System.out.println("The decimal value for bin number " + hex + " is " + num);
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}

12.8

import java.util.Scanner;
class HexFormatException extends Exception{
    HexFormatException(String msg){
        super(msg);
    }
}
public class HexformatexceptionException12_8 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a hex number: ");
        String hex = input.nextLine();
        hexToDec(hex.toUpperCase());
    }

    public static void hexToDec(String hex){
        int num = 0;
        try{
            for(int i = 0;i < hex.length(); ++i){
                char hexChar = hex.charAt(i);
                if(hexChar >= '0' && hexChar <= '9'){
                    num = num * 16 + hexChar - '0';
                }else if( hexChar >= 'A' && hexChar <= 'F'){
                    num = num * 16 + hexChar - 'A' + 10;
                }else{
                    throw new HexFormatException("It isn't a hex number!");
                }
            }
            System.out.println("The decimal value for hex number " + hex + " is " + num);
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}

12.9

import java.util.Scanner;
class BianryFormatException extends Exception{
    BianryFormatException(String msg){
        super(msg);
    }
}
public class BinaryformatexceptionException12_9 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a hex number: ");
        String hex = input.nextLine();
        bin2Dec(hex);
    }

    public static void bin2Dec(String hex){
        int num = 0;
        try{
            for(int i = 0;i < hex.length(); ++i){
                char binChar = hex.charAt(i);
                if(binChar == '1' || binChar == '0'){
                    num = num * 2 + binChar - '0';
                }
                else{
                    throw new BianryFormatException("It isn't a bin number!");
                }
            }
            System.out.println("The decimal value for bin number " + hex + " is " + num);
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}

12.10

import sun.awt.windows.WPrinterJob;

import java.util.Scanner;

public class OutofmemoryerrorError12_10 {
    //给数组存的长度设置为int的最大值,就会引起爆栈
    public static void main(String[] args) {
        try{
            int maxL = Integer.MAX_VALUE;
            int a[] = new int[maxL];
            System.out.println(maxL);
        }catch (OutOfMemoryError e){
            System.out.println("OutOfMemoryError!");
        }
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值