system.exit(输入一个非零的数);
表示程序退出。
system.err.println
表示输出的是红色的字体
java中所有的异常都是Exception(父类)
除数为0的异常,ArithmeticException(子类)
输入格式不正确,InputMismathException(子类)
作业:
1、
package com.yichang;
import java.util.Scanner;
public class LianXi {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入课程代号(1~3之间的数):");
try {
int daihao = 0;
daihao = sc.nextInt();
if(daihao == 1) {
System.out.println("c#编程");
}else if(daihao == 2) {
System.out.println("java编程");
}else if(daihao == 3) {
System.out.println("php编程");
}else {
System.err.println("输入错误!");
}
} catch (Exception e) {
System.err.println("输入错误!");
}finally {
System.out.println("欢迎提出建议!");
}
}
}
2、
package com.yichang;
public class Person {
private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) throws Exception{
if(age>1 && age<100) {
this.age = age;
}else {
throw new Exception("年龄必须在1到100之间");
}
}
public void print() {
System.out.println(this.getName()+"-"+this.getSex()+"-"+this.getAge());
}
}
package com.yichang;
public class Test2 {
public static void main(String[] args) {
Person p = new Person();
try {
p.setAge(150);
p.print();
} catch (Exception e) {
e.printStackTrace();
}
}
}