Java课堂作业第十周

该代码示例展示了Java中标准库异常的处理,包括数组越界、除以零、空指针等常见异常,并通过`getMessage()`打印错误信息。同时,实现了自定义异常`ScoreExceedsMaxValueException`,用于检查学生分数的有效性,统计正常和异常分数的分布情况。
摘要由CSDN通过智能技术生成

Java课堂作业第十周

1 标准库异常练习,编写程序,产生如下列表的异常,捕获并打印getMessage()字符串
要求,不允许使用throw new AarrayIndexOutOfBoundException方式抛出。
2 自定义异常练习
目的:检查学生分数的有效性并统计输出结果。
1)设计student类,包含私有整型成员变量score
2)创建student数组(包含15000个student对象)并为每个student对象生产[1,150]的随机整数分数
3)定义分数超出正常值的异常类,命名为scoreexceedmaxvalueexception,在类中设置成员变量,存储导致异常产生的原因(大于100的分数值)
4)编写iscore(student)函数,分析传递过来的student的学生分数,检查学生分数的合法性,若分数大于100,抛出上述异常。
在这里插入图片描述

题目1

package ch10;

import java.util.Scanner;

public class a {
    public static int test1(){
        int a[]={0,1};
        try{
            int b=a[101];
            return b;
        }catch (Exception e){
            e.printStackTrace();
            String msg=e.getMessage();
            System.out.println(msg);
            return -1;
        }
    }
    public static int test2(){
        int a=5,b=0;
        Scanner scanner=new Scanner(System.in);
        //b=scanner.nextInt();
        try{
            int c=a/b;
            return c;
        }catch (Exception e){
            e.printStackTrace();
            String msg=e.getMessage();
            System.out.println(msg);
            return -1;
        }
    }
    public static String test3(){
        try{
            Object object=null;
            String string=object.toString();
            return string;
        }catch (Exception e){
            e.printStackTrace();
            String msg=e.getMessage();
            System.out.println(msg);
            return "error";
        }
    }
    public static int test4(){
        try{
            String num="1a";
            System.out.println(Integer.parseInt(num));
            int b=Integer.parseInt(num);
            return b;
        }catch (Exception e){
            e.printStackTrace();
            String msg=e.getMessage();
            System.out.println(msg);
            return -1;
        }
    }
    public static int test5(){
        try{
            Scanner scanner=new Scanner(System.in);
            int a=scanner.nextInt();

            return a;
        }catch (Exception e){
            e.printStackTrace();
            String msg=e.getMessage();
            System.out.println(msg);
            return -1;
        }
    }
    public static void main(String[] args) {
        a t1=new a();
        t1.test1();
        //System.out.println(t1.test1());
        a t2=new a();
        t2.test2();
        //System.out.println(t2.test2());
        a t3=new a();
        t3.test3();
        //System.out.println(t3.test3(3));
        a t4=new a();
        t4.test4();
        a t5=new a();
        t5.test5();
    }
}

题目2

package ch10;

public class Main {
    public static void main(String[] args) {
        int a[] = new int[15000];
        for (int i = 0; i < 15000; i++) {
            a[i] = (int) (Math.random() * 150 + 1);
        }
        Student stu = new Student();
        for (int i = 0; i < 15000; i++) {
            try {
                stu.isScoreValid(a[i]);

            } catch (Exception e) {
                System.out.println(e);
            }
        }
        int zhengchang=0,yichang=0,zhengchangscore=0,yichangscore=0;
        for(int i=0;i<15000;i++){
            if(a[i]>100){
                yichang+=1;
                yichangscore+=a[i];
            }
            else{
                zhengchang+=1;
                zhengchangscore+=a[i];
            }
        }
        System.out.println("正常分数人数"+zhengchang);
        System.out.println("正常分数平均分"+zhengchangscore/zhengchang);
        System.out.println("异常分数人数"+yichang);
        System.out.println("异常分数平均分"+yichangscore/yichang);
    }
}

class Student {

    private int score;

    public void isScoreValid(int a) throws ScoreExceedsMaxValueException {
        String yichang="";
        if (a > 100){
            yichang+=a;
            throw new ScoreExceedsMaxValueException(yichang+" ");
        }
    }
}

class ScoreExceedsMaxValueException extends Exception {
    public ScoreExceedsMaxValueException() {
        super();
    }

    public ScoreExceedsMaxValueException(String message) {
        super(message);
    }
}

题目3

package ch10;

public class c {
    public static int calculate(int upLimit) {
        int maxLen = 0;
        int maxId = 3;
        for (int i = 3; i < upLimit; i++) {
            int len = calculateCollatzLen(i);
            if (len > maxLen) {
                maxLen = len;
                maxId = i;
            }
        }
        return maxId;
    }
    public static int calculateCollatzLen(int i) {
        long n = i;
        int count = 1;
        while( n != 1 ){
            if( n % 2 == 0){
                n = n / 2;
            }else{
                n = n * 3 + 1;
            }
            count ++;
        }
        return count;
    }
    public static void main(String[] args) {
        System.out.println(c.calculate(1000000));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值