二维数组
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
示例代码
/*二维数组训练
*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
异常
异常体系
/* Throwable{
Error//严重问题、不需要处理
Exception{//成为异常类、它表示程序本身可以处理的问题
RuntimeException//在编译期是不检查的,出现问题后修改代码
非RuntimeException//编译期就必须处理,否则无法通过编译
}
}*/
异常处理
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)