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 learn;
public class MyException extends Exception{
String message;
public MyException(){
message="Error,score should be in 0-100.";
}
public String warnMess(){
return message;
}
}
package learn;
public class Score {
int score;
public Score(int score) throws MyException{
if(score<0 || score>100){
throw new MyException();
}else {
this.score=score;
System.out.println("Your score is:"+score);
}
}
}
package learn;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Please input your score");
int score=in.nextInt();
try {
Score s = new Score(score);
}catch (MyException m){
System.out.println(m.warnMess());
}
}
}
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 learn;
public class Division {
public int dividend;
public int divisor;
public Division(int dividend,int divisor) throws ZeroException, NegativeException {
this.dividend=dividend;
if(divisor==0){
throw new ZeroException();
}else if(divisor<0){
throw new NegativeException(divisor);
}else {
this.divisor = divisor;
}
}
public double getResult(){
return (double) dividend/divisor;
}
}
package learn;
public class ZeroException extends Exception{
String message;
public ZeroException(){
message="The divisor, 0,could not be zero!";
}
public String warnMess(){
return message;
}
}
package learn;
public class NegativeException extends Exception {
String message;
public NegativeException(int d){
message="The divisor, "+d+", could not be negative!";
}
public String warnMess(){
return message;
}
}
package learn;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Please input first number:");
int d1=in.nextInt();
System.out.println("Please input second number:");
int d2=in.nextInt();
try {
Division d = new Division(d1,d2);
System.out.println("Divisor is : "+d.getResult());
}catch (NegativeException n){
System.out.println(n.warnMess());
}catch (ZeroException z){
System.out.println(z.warnMess());
}finally {
System.out.println("finally!");
}
}
}
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
【输入输出样例4】
How many students?
0
All the students bellow:
package learn;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
ArrayList<Student> array=new ArrayList<>();
System.out.println("How many students?");
int x=in.nextInt();
if(x>0) {
System.out.println("Please input the names of students:");
}
System.out.println("All the students bellow:");
for(int i=1;i<=x;i++){
String name=in.next();
array.add(new Student(i,name));
}
for(Student s:array){
System.out.println(s.num+":"+s.name);
}
}
}
package learn;
public class Student {
public int num;
public String name;
public Student(int num,String name){
this.num=num;
this.name=name;
}
}