异常处理、Collatz问题(JAVA)


2021.05.24上机题

题目描述

在这里插入图片描述

Question1

package Practice;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Exercise_01 extends Exception {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> arrayList = new ArrayList<>();


        try{
            char a[] = new char[2];
            a[3] = 10;

        }catch (ArrayIndexOutOfBoundsException  e){
            //e.printStackTrace();
            System.out.println("ArrayIndexOutOfBoundsException:"+e.getMessage());
            //数组越界
        }

        try{
            int t1 = 5;
            int t2 = 0;
            int num = t1/t2;
        }catch(ArithmeticException e){
            //  e.printStackTrace();
            System.out.println("ArithmeticException:"+e.getMessage());
            //算数运算异常
        }

        ArrayList<Integer>[] array = new ArrayList[3];
        try{
//            int tt=0;
//            array[1].add(tt);
//            arrayList.add(0,Integer.parseInt(scanner.nextLine()));
            String str = null;
            System.out.println(str.length());
        }catch(NullPointerException n){
            //  n.printStackTrace();
            System.out.println("NullPointerException:"+n.getMessage());
            //空指针异常
        }
        try{
            String a = "2 ";
            int b = Integer.valueOf(a);
            System.out.println(b);
        }catch(NumberFormatException n){
            // n.printStackTrace();
            System.out.println("NumberFormatException:"+n.getMessage());
            //数字格式异常
        }
//        try{
//            System.out.println("请输入字符串");
//            arrayList.add(0,Integer.parseInt(scanner.nextLine()));
//        }catch (NumberFormatException e){
//            System.out.println("NumberFormatException:"+e.getMessage());
//        }
//        try{
//            Scanner s = new Scanner(System.in);
//            System.out.print("请输入数字:");
//            int getnum = s.nextInt();
//            System.out.println(" ");
//            System.out.println(getnum);
//        }catch(InputMismatchException i){
//            // i.printStackTrace();
//            System.out.println("InputMismatchException:"+i.getMessage());
//            //输入不匹配异常
//        }
        try{
            int[] a = {1,9,8,1,0,4};
            System.out.println("请输入字符串(输入的字符串,结果用的nextInt,因此会报错误的输入匹配)");
            a[0] = scanner.nextInt();
        }catch (InputMismatchException e){
//            System.out.println(e);
            System.out.println("InputMismatchException:"+e.getMessage());
        }

        System.out.println("end");
    }
}

效果图
在这里插入图片描述

Question2

//package Practice;

public class Exercise_02 {
    public static void main(String[] args) {
        Student[] students = new Student[15000];
        boolean flag = true;
        int numOfRegular = 0;
        int sumOfRegular = 0;
        int numOfException = 0;
        int sumOfException = 0;

        for (int i = 0; i < students.length; i++) {
            students[i] = new Student();
            students[i].setScore((int) (Math.random() * 150 + 1)); //[1,150]的随机整数分数
            try{
                isScoreValid(students[i]);
                numOfRegular++;
                sumOfRegular += students[i].getScore();
            }catch(Exception s){
                numOfException++;
                sumOfException += students[i].getScore();
            }
        }

        System.out.println("正常分数人数:"+numOfRegular);
        System.out.println("平均分:"+sumOfRegular / numOfRegular);
        System.out.println("异常分数人数:"+numOfException);
        System.out.println("平均分:"+sumOfException / numOfException);
    }

    public static void isScoreValid(Student s) throws ScoreExceedsMaxValueException{

        int score = s.getScore();
        if (score > 100)
            throw new ScoreExceedsMaxValueException();

    }
}

class Student {
    private int Score;

    public int getScore() {
        return Score;
    }

    public void setScore(int score) {
        Score = score;
    }
}

class ScoreExceedsMaxValueException extends Exception { //超出正常值的异常类
    private String s = "Exception! The score is beyond 100";

    public ScoreExceedsMaxValueException() {
    }

    public String getS() {
        return s;
    }
}

效果图
在这里插入图片描述

Question3

package Practice;

import java.util.HashMap;

public class Exercise_03{
    public static int flag = 0;
    public static HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();

    public static void main(String args[]) {
        int sum=0;
        for(int i=2;i<1000000;i++){
            if(sum<collatzArg(i,i)){
                sum=collatzArg(i, i);
            }
        }
        for(int j=2;j<1000000;j++){
            if(hashMap.get(j)==sum){
                System.out.println(j);
            }
        }
    }

    public static int collatzArg(double d, int n) {
        flag++;
        if (d > 1.0) {
            if (d % 2 == 0) {
                collatzArg(d / 2, n);
            } else {
                collatzArg(d * 3 + 1, n);
            }
        }else{
            hashMap.put(n, flag);
            flag=0;
        }
        return hashMap.get(n);
    }
}

效果图
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值