1.编写一个计算N个整数平均值的程序。程序应该提示用户输入N的值,如何必须输入所有N个数。如果用户输入的值是一个负数,则应该抛出一个异常并捕获,提示“N必须是正数或者0”。并提示用户再次输入该数。
思路:
(1)首先定义用户输入N个数的总和sum,用Scanner键盘录入用户输入的数。
(2)然后在for循环里,让用户依次输入N个数。
(3)其次,先判断输入的数是否为整数或零,再根据输入的数进行判断,如果小于零,就抛出一个异常捕获,再用键盘录入让用户重新输入,最后对用户再次输入的数进行累加。
代码如下:
import java.util.Scanner;
public class IntegerMean {
public static void main(String[] args) {
double sum = 0;
System.out.println("----------------------------");
Scanner sc = new Scanner(System.in);
System.out.println("要计算几个整数的平均值呢:");
int a = sc.nextInt();
for (int i = 0; i < a; i++) {
System.out.println("请输入" + (i + 1) + "个数");
int i1 = sc.nextInt();
// 判断输入的数是否为正数或零
if (i1 >=0) {
sum += i1;
}
try {
if (i1 < 0) {
method();
}
} catch (Exception e) {
System.out.println(e);
}if (i1 < 0) {
System.out.println("请重新输入"+(i+1)+"个数");
int i2 = sc.nextInt();
sum += i2;
}
}
System.out.println("一共" + a + "个数" + "," + "和为:" + sum + "," + "平均值为:" + sum / a);
System.out.println("----------------------------");
}
public static void method() throws Exception{
throw new Exception("输入的数必须是正数或者0");
}
}
运行效果如下:
总结:
抛出异常,这里是在方法中进行抛出的,没有进行自定义异常,代码也有很多要优化的地方,也有很多不足。
2.定义一个Computer类,有属性private int state 表示为状态码,有2个方法,一个是run()方法,当state 为1时,在方法里正常显示“电脑成功运行”,当state 为2时,在方法里抛出自定义异常LanPingException,显示"电脑蓝屏了",当state 为3时,在在方法里抛出自定义异常MaoYanException,显示"电脑冒烟了",还有一个方法recst(),打印”电脑重启中...“......
思路:
(1)首先先自定义定义三个异常类,分别是LanPingException、MaoYanException和NoPlanException。
(2)其次, 在Computer类中定义它的私有属性state,在它的run()方法中定义状态码的三个情形下的不同显示结果,在不同的状态码下捕获异常并抛出。
(3) 然后定义一个Teacher类,定义属性Computer cmp,
在方法attendClass()new Computer()对象赋值给属性cmp,并设置state状态码,用Random随机生成(1-3的范围)的整数。
(4)最后创建测试类,创建Teacher对象,运行attendClass()上课方法,并对该方法进行try-catch处理,最后打印异常信息。
代码如下:
三个自定义异常类:
public class LanPingException extends Exception{
public LanPingException() {};
public LanPingException(String message) {
super(message);
}
}
public class MaoYanException extends Exception{
public MaoYanException() {};
public MaoYanException(String message) {
super(message);
}
}
public class NoPlanException extends Exception{
public NoPlanException() {};
public NoPlanException(String message) {
super(message);
}
}
Computer类:
public class Computer extends Exception{
private int state;
public Computer() {
}
public Computer(int state) {
this.state = state;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public void run() throws LanPingException,MaoYanException{
if (state == 1) {
System.out.println("电脑运行");
System.out.println("老师开始上课了");
} else if (state == 2) {
throw new LanPingException("蓝屏了");
} else if (state == 3) {
throw new MaoYanException("电脑冒烟了");
}
}
public void recst() {
if (state == 2) {
System.out.println("电脑重启中....");
System.out.println("老师开始上课");
}
}
}
Teacher类:
import java.util.Random;
public class Teacher {
private Computer cmp;
public Teacher() {
}
public Teacher(Computer cmp) {
this.cmp = cmp;
}
public Computer getCmp() {
return cmp;
}
public void setCmp(Computer cmp) {
this.cmp = cmp;
}
public void attendClass() throws NoPlanException {
cmp = new Computer();
// 这里random只取得到[0,3),所以要在此基础上加1,才可以满足前面定义的状态码的情形
int i1 = ((int)(Math.random()*3)+1);
cmp.setState(i1);
try {
cmp.run();
}catch (MaoYanException e) {
e.printStackTrace();
throw new NoPlanException("上课无法继续,因为电脑冒烟了,需要进行修理");
}catch (LanPingException e) {
System.out.println("电脑蓝屏了");
cmp.recst();
}
}
}
最后的测试类:
public class ExceptionText {
public static void main(String[] args) throws LanPingException,MaoYanException,NoPlanException{
Teacher t = new Teacher();
t.attendClass();
}
}
运行效果如下:
当state为1:
当state为2:
当state为3: