实验六 java IO及异常处理机制

1

【问题描述】

键盘输入成绩,自定义异常类MyException用于检测输入的成绩大于100或者小于0时,抛出异常,否则就输出成绩。

 

【输入形式】

请输入你的成绩:

 

【输出形式】

如果成绩在【0-100】:你的成绩为:

如果成绩不在【0-100】:错误,成绩应该在【0-100】之间

 

【输入输出样例1】 

Please input your score

101

Error,score should be in 0-100.

 

【输入输出样例2】

Please input your score

90

Your score is:90

 

【评分标准】

结果完全正确得满分,格式不对得部分分数。

package com;

import java.util.Scanner;

class coreerror{
	public int coreerror(int num) throws MyException {
		int g=num;
		if (num > 100 || num<0) {
			throw new MyException("Error,score should be in 0-100.");
		}
		return g;
	}
}

class MyException extends Exception{
	MyException(String msg){
		super(msg);
	}
}

public class test {
	public static void main(String[] args) {
        System.out.println("Please input your score"); 
		Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int g;
        coreerror d = new coreerror();
		try {
			int s = d.coreerror(num);
			System.out.println("Your score is:"+s);	
		}
		catch(MyException e){
			System.out.println("Error,score should be in 0-100.");
		}
	}
}

2

【问题描述】

输入两个数,输出两个数的商,自定义异常类NegativeException和ZeroException用于检测输入的除数为负数和零时,抛出异常。

 

【输入形式】

请输入第一个数:

 

请输入第二个数:

 

【输出形式】

商是:

 

【输入输出样例1】 

Please input first number: 

5

Please input second number: 

2

Divisor is : 2.5

finally!

 

【输入输出样例2】

Please input first number: 

5

Please input second number: 

-2

The divisor, -2, could not be negative!

finally!

 

【输入输出样例3】  

Please input first number: 

5

Please input second number: 

0

The divisor, 0,could not be zero!

finally!

 

【评分标准】

结果完全正确得满分,格式错误得部分分数。

package comm;

import java.util.Scanner;

class coreerror{
	public double coreerror(double a,double b) throws NegativeException,ZeroException{
		if(b<0) {
			throw new NegativeException("The divisor, "+(int)b+", could not be negative!");
		}
		else if(b==0) {
			throw new ZeroException("The divisor, "+(int)b+",could not be zero!");
		}
		double g = a/b;
		return g;
	}
}

class NegativeException extends Exception{
	NegativeException(String msg){
		super(msg);
	}
}

class ZeroException extends Exception{
	ZeroException(String msg){
		super(msg);
	}
}

public class test1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        System.out.println("Please input first number:" ); 
        double a = sc.nextInt();
        System.out.println("Please input second number:" );
        double b = sc.nextInt();
        coreerror d = new coreerror();
		try {
			double s = d.coreerror(a,b);
			System.out.println("Divisor is : "+s);	
		}
		catch(NegativeException e){
            System.out.println(e.getMessage());
		}
		catch(ZeroException e) {
			System.out.println(e.getMessage());
		}
		finally {
			System.out.println("finally!");
		}
	}
}

注意:

这个程序与第一个程序放在同一个包里,coreerror会出现重复。

 

3

【问题描述】

输入学生个数和名字,使用ArrayList集合存储学生序号和名字,并显示所有的学生信息。

 

【输入形式】

一共有几个学生?

 

请输入学生的名字:

 

【输出形式】

所有的学生如下:

(序号:姓名)

 

【输入输出样例1】 

How many students?

1

Please input the names of students:

Jack

All the students bellow:

1:Jack

 

【输入输出样例2】

How many students?

2

Please input the names of students:

tom

jerry

All the students bellow:

1:tom

2:jerry

 

【输入输出样例3】

How many students?

3

Please input the names of students:

tom

jerry

snooby

All the students bellow:

1:tom

2:jerry

3:snooby

 

【评分标准】

结果完全正确得满分,格式错误得部分分数。

package com;
import java.util.ArrayList;
import java.util.Scanner;

class Student{
	private String name;
	private int Sno;
	
	public Student() {
	}
	
	public Student(String name,int Sno) {
		this.name=name;
		this.Sno=Sno;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSno() {
		return Sno;
	}
	public void setSno(int sno) {
		this.Sno = sno;
	}
}


public class test2 {
	public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          System.out.println("How many students?");
          String chang = sc.nextLine();
          int length = Integer.parseInt(chang);
          ArrayList<Student> list = new ArrayList<>(length);
          
          String name;
          int i;
  		
  		  if(length > 0) {
  			System.out.println("Please input the names of students:");
  			for(int count = 0; count < length; count++) {
  				name = sc.nextLine();
  				i = count+1;
  				Student stu = new Student(name,i);
  				list.add(stu);
  			}
  		   }
  		  
  		  System.out.println("All the students bellow:");
  		  for(int count = 0 ;count < length;count ++) {
  			    Student s = list.get(count);
  			    System.out.println(s.getSno()+":"+s.getName());
  		  }
	}
}

 

  • 11
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值