实验六 Java异常处理与集合的使用

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

【问题描述】
键盘输入成绩,自定义异常类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

import java.util.Scanner;

public class MyException 
{
	public static void main(String[] args) 
	{
		System.out.println("Please input your score");
		Scanner in = new Scanner(System.in);
		int score = in.nextInt();
		if(score >= 0 && score <= 100)
		{
			System.out.println("Your score is:" + score);
		}
		else
		{
			System.out.println("Error,score should be in 0-100.");
		}
	}
}
import java.util.Scanner;

public class Main 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		System.out.println("Please input your score");
		int score = in.nextInt();
		
		try 
		{
			new Score(score);
		}
		catch(MyException e) 
		{
			System.out.println(e.message);
		}
	}
}

class MyException extends Exception
{
	String message;
	
	public MyException(int score) 
	{
		message = "Error,score should be in 0-100.";
	}
}

class Score
{
	public Score(int r) throws MyException
	{
		if(r<0 || r>100)
		{
			throw new MyException(r);
		}
		else 
		{
			System.out.println("Your score is:"+r);
		}
	}
}

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

【问题描述】
输入两个数,输出两个数的商,自定义异常类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!

import java.util.Scanner;

public class Main 
{
	public static void main(String[] args)
	{
		try (Scanner in = new Scanner(System.in)) {
			System.out.println("Please input first number: ");
			int firstnum = in.nextInt();
			System.out.println("Please input second number: ");
			int secondnum = in.nextInt();
			if(secondnum == 0) 
			{
				System.out.println("The divisor, 0,could not be zero!");
			}
			else if(secondnum < 0) 
			{
				System.out.println("The divisor, "+ secondnum +", could not be negative!");
			}
			else
			{
				System.out.println("Divisor is : " +(double)firstnum/secondnum);
			}
		}
		finally
		{
			System.out.println("finally!");
		}
	}
}

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

【问题描述】
输入学生个数和名字,使用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

import java.util.Scanner;
import java.util.ArrayList;
public class Main 
{
	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		System.out.println("How many students?");
		int num = in.nextInt();
		if(num>0) 
		{
			System.out.println("Please input the names of students:");
			String s; 
			ArrayList<Name> arrayList = new ArrayList<Name>();
			for (int i = 0; i < num; i++) 
			{
				s = in.next();//nextLine()会出错
				Name n = new Name(i,s);
				arrayList.add(n);
		    }
			System.out.println("All the students bellow:");
			for (Name e : arrayList) 
			{
				System.out.println(e);
			}	
		}
		else 
		{
			System.out.println("All the students bellow:");
		}
		in.close();	
	}
}

class Name {
	public int xuhao;
	public String name;
	public Name(int x, String n) 
	{
		xuhao = x+1;
		name = n;
	}
	public String toString()
	{
		return xuhao+":"+name;
	}
}

在这里插入图片描述
在这里插入图片描述

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a;
		int count;
		
		System.out.println("How many students?");
		a=in.nextInt();
		if(a==0) {
			System.out.println("All the students bellow:");
			return;
		}
		String[] arr =new String[a];
		String ss;
		in.nextLine();
		System.out.println("Please input the names of students:");
		for(int i=0;i<a;i++) {
			ss=in.nextLine();
			arr[i]=ss;
		}
		
		System.out.println("All the students bellow:");
		for(int i=0;i<a;i++) {
			System.out.println(i+1+":"+arr[i]);
		}
		
	}
}
  • 6
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值