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]);
}
}
}