栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

异常+二维数组

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

异常+二维数组

文章目录
  • 二维数组
    • 案例:36选7
  • 异常
    • 异常体系
    • 异常处理
      • try...catch
      • throw
      • 自定义异常

二维数组

java中的二维数组与其他语言的有差异;Java中二维数组是数组的数组;
例如:

int[][] a = {(2,3),(2,3,4,5),(2,4,5)}

实质上表示

int[][] a = new int[3][];
a[0] = new int[2];
a[1] = new int[4];
a[2] = new int[3];

但是不可以写成

int[][] a = new int [][4];

这样写在Java中是非法的,这是与c++不同的一点。

案例:36选7

示例代码

public class test_01 {
    public static void main(String[] args) {
        System.out.println("输出结果为:");
        int[] a = new int[7];

        for (int i = 0; i < a.length; i++) {
            one_num:
            while (true) {
                a[i] = (int) (Math.random() * 36 + 1);

                for (int j = 0; j < i; j++) {
                    if (a[i] == a[j]) {
                        continue one_num;
                    }
                }
                break;
            }
        }
        for (int num : a) System.out.print(num + " ");
        System.out.println();
    }
}
输出结果为:
21 27 26 34 30 12 4 

异常 异常体系
 
异常处理 
try…catch 

格式:

try{
    可能出现异常的代码;
    }catch(异常名类 变量名){
    异常的处理代码;
    }
}

三个方法: e.printStackTrace();
e.getMessage();
e.toString();

public class yichang {
    public static void main(String[] args) {
        //System.out.println("action");

        method();
        System.out.println("jieshu");


    }

    public static void method() {
        int[] a = {1, 2, 3,};

        try {
            System.out.println(a[3]);
        } catch (ArrayIndexOutOfBoundsException e) {
        //    e.printStackTrace();
            //System.out.println(e.getMessage());
            System.out.println(e.toString());
        }
    }
}
throw

格式:

throws 异常类名;

throws只是抛出异常,try…catch才能使得程序进行下去;
采用throws时,将来谁调用谁处理;可以采取try…catch

示例:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class yichang {
    public static void main(String[] args) {
        //System.out.println("action");

        method();
        System.out.println("jieshu");
        try {//throws只是抛出异常,try...catch才能使得程序进行下去
            method2();
        } catch (ParseException e) {
            e.printStackTrace();
        }


    }

    //throws 异常类名;
    //运行时异常
    public static void method() {
        int[] a = {1, 2, 3,};

        try {
            System.out.println(a[3]);
        } catch (ArrayIndexOutOfBoundsException e) {
            //    e.printStackTrace();
            //System.out.println(e.getMessage());
            System.out.println(e.toString());
        }
    }
	
	//编译时异常
    public static void method2() throws ParseException {

        //      try {
        String ss = "2048-02-12";
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
        Date d = s.parse(ss);
        System.out.println(d);
        //   } catch (ParseException e) {
        //       e.printStackTrace();
    }


}
输出结果为:
java.lang.ArrayIndexOutOfBoundsException: 3
jieshu
Wed Feb 12 00:00:00 CST 2048
自定义异常

格式

public class 异常类名 extends Exception{
    无参构造
            带参构造
}

//例如
public class ScoreException extends Exception{
    public ScoreException(){}
    public ScoreException(String message){
        super(message);
    }
}

代码示例

public class ScoreException extends Exception {
    public ScoreException() {
    }

    public ScoreException(String message) {
        super(message);
    }
}
public class teacher {

    public void checkScore(int score) throws ScoreException{//
        if (score<0||score>100){
            //throw new ScoreException();//抛出异常对象
            throw new ScoreException("分数应该在0-100之间");
        }else
            System.out.println("分数正常");
    }

}
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        System.out.println("输出结果为: ");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的成绩:");
        int next = sc.nextInt();

        teacher a = new teacher();
        try {
            a.checkScore(next);
        } catch (ScoreException e) {
            e.printStackTrace();
        }
    }
}
输出结果为: 
请输入你的成绩:
120
mook.test02.zidingyi_yichang.ScoreException: 分数应该在0-100之间
	at mook.test02.zidingyi_yichang.teacher.checkScore(teacher.java:8)
	at mook.test02.zidingyi_yichang.test.main(test.java:14)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/588450.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号