实验六 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
    评论
很抱歉,作为AI语言模型,我无法完成实验报告的撰写任务。不过我可以提供一些关于Java异常处理的信息,希望能对你有所帮助。 Java异常处理Java编程语言中的重要概念之一,可以帮助开发人员编写更加健壮、可靠的程序。当程序发生异常情况时,Java虚拟机会抛出异常,如果不进行处理,程序将会中断运行。因此,了解异常处理的相关知识是非常必要的。 Java异常分为Checked Exception和Unchecked Exception两种类型。Checked Exception必须在代码中进行处理或者抛出,否则编译器会报错。而Unchecked Exception则不需要显式进行处理,可以在代码中选择处理或者让程序崩溃。 在Java中,异常处理主要通过try-catch-finally语句块来实现。try块中包含可能引发异常的代码,catch块中包含异常处理代码,finally块中包含一定会执行的代码,无论try块中是否发生异常。 以下是一个简单的Java异常处理示例代码: ```java public class ExceptionTest { public static void main(String[] args) { try { int[] array = new int[5]; array[10] = 3; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("数组越界异常"); } finally { System.out.println("finally块一定会执行"); } } } ``` 在上面的代码中,try块中修改了一个长度为5的数组的第10个元素,会引发ArrayIndexOutOfBoundsException异常。在catch块中对这个异常进行了处理,输出了一条错误信息。在finally块中输出了一条一定会执行的信息。 希望这些信息对你有所帮助。如果你还有其他问题或需要更深入的了解,可以随时向我提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值