感觉可以简化,但是本事没到家,各位大佬可以指点下。
主类
public class Index {
public static void main(String[] args) {
new Processing();
}
}
工具类
package lianxi;
import java.util.Scanner;
public class Processing {
static Scanner sc = new Scanner(System.in);
static Stu stu = new Stu();
public Processing() {
show();
}
private void show() throws Nameerrors,Ageerror,MathAccomplishment,CHINESEAccomplishment,EnglishAccomplishment{
for (int i = 1;i<4;i++){
name(i);
age(i);
math(i);
chinese(i);
english(i);
System.out.println(stu);
}
}
//输入名字
private void name(int i){
try {
System.out.println("请输入第"+i+"个学生的姓名");
String name = sc.nextLine();
stu.setName(name);
} catch (Exception e) {
System.out.println("不能为空");
System.out.println("请输入第"+i+"个学生的姓名");
String name = sc.nextLine();
stu.setName(name);
}
}
//输入年龄
private void age(int i){
try {
System.out.println("请输入第"+i+"个学生的年龄");
int age = Integer.parseInt(sc.nextLine());
stu.setAge(age);
} catch (NumberFormatException e) {
System.out.println("年龄错误,请输入0~120之间");
System.out.println("请重新输入年龄");
int age = Integer.parseInt(sc.nextLine());
stu.setAge(age);
}
}
//输入数学成绩
private void math(int i){
try {
System.out.println("请输入第"+i+"个学生的数学成绩");
double math = Double.parseDouble(sc.nextLine());
stu.setMath(math);
} catch (NumberFormatException e) {
System.out.println("不能为空");
System.out.println("请重新输入学生数学成绩");
double math = Double.parseDouble(sc.nextLine());
stu.setMath(math);
}
}
//输入语文成绩
private void chinese(int i){
try {
System.out.println("请输入第"+i+"个学生的语文成绩");
double CHINESE = Double.parseDouble(sc.nextLine());
stu.setCHINESE(CHINESE);
} catch (NumberFormatException e) {
System.out.println("不能为空");
System.out.println("请重新输入语文成绩");
double CHINESE = Double.parseDouble(sc.nextLine());
stu.setCHINESE(CHINESE);
}
}
//输入英语成绩
private void english(int i){
try {
System.out.println("请输入第"+i+"个学生的英语成绩");
double english = Double.parseDouble(sc.nextLine());
stu.setEnglish(english);
} catch (NumberFormatException e) {
System.out.println("不能为空");
System.out.println("请重新输入英语成绩");
double english = Double.parseDouble(sc.nextLine());
stu.setEnglish(english);
}
}
}
学生类
package lianxi;
public class Stu {
String name;
int age;
double math;
double CHINESE;
double english;
public Stu() {
}
public Stu(String name, int age, double math, double CHINESE, double english) {
this.name = name;
this.age = age;
this.math = math;
this.CHINESE = CHINESE;
this.english = english;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
if (Nameerrors.isChineseWord(name)){
this.name = name;
}else {
throw new Nameerrors();
}
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
if (age>=0 && age<=100){
this.age = age;
}else {
throw new Ageerror();
}
}
/**
* 获取
* @return math
*/
public double getMath() {
return math;
}
/**
* 设置
* @param math
*/
public void setMath(double math) {
if (math>=0 && math<=100){
this.math = math;
}else {
throw new MathAccomplishment();
}
}
/**
* 获取
* @return CHINESE
*/
public double getCHINESE() {
return CHINESE;
}
/**
* 设置
* @param CHINESE
*/
public void setCHINESE(double CHINESE) {
if (CHINESE>=0 && CHINESE<=100){
this.CHINESE = CHINESE;
}else {
throw new CHINESEAccomplishment();
}
}
/**
* 获取
* @return english
*/
public double getEnglish() {
return english;
}
/**
* 设置
* @param english
*/
public void setEnglish(double english) {
if (english>=0 && english<=100){
this.english = english;
}else {
throw new EnglishAccomplishment();
}
}
public String toString() {
return "Stu{name = " + name + ", age = " + age + ", math = " + math + ", CHINESE = " + CHINESE + ", english = " + english + "}";
}
}
以下是自定义异常
姓名处理异常类
package lianxi;
import java.util.regex.Pattern;
import static lianxi.Processing.sc;
import static lianxi.Processing.stu;
public class Nameerrors extends RuntimeException {
public Nameerrors(){
System.out.println("请输入中文汉字");
System.out.println("请重新输入姓名");
String name = sc.nextLine();
stu.setName(name);
}
public static boolean isChineseWord(String str){
String pattern = "[\u4e00-\u9fa5]+";
boolean isMatch = Pattern.matches(pattern, str);
return isMatch;
}
}
年龄异常处理类
package lianxi;
import static lianxi.Processing.sc;
import static lianxi.Processing.stu;
public class Ageerror extends RuntimeException {
public Ageerror(){
System.out.println("年龄错误,请输入0~120之间");
System.out.println("请重新输入年龄");
int age = Integer.parseInt(sc.nextLine());
stu.setAge(age);
}
}
数学成绩处理异常类
package lianxi;
import static lianxi.Processing.sc;
import static lianxi.Processing.stu;
public class MathAccomplishment extends RuntimeException{
public MathAccomplishment(){
System.out.println("成绩不能为负数,不能大于100分");
System.out.println("请重新输入学生数学成绩");
double math = Double.parseDouble(sc.nextLine());
stu.setMath(math);
}
}
语文成绩处理异常类
package lianxi;
import static lianxi.Processing.sc;
import static lianxi.Processing.stu;
public class CHINESEAccomplishment extends RuntimeException{
public CHINESEAccomplishment(){
System.out.println("成绩不能为负数,不能大于100分");
System.out.println("请重新输入学生语文成绩");
double CHINESE = Double.parseDouble(sc.nextLine());
stu.setCHINESE(CHINESE);
}
}
英语成绩处理异常类
package lianxi;
import static lianxi.Processing.sc;
import static lianxi.Processing.stu;
public class EnglishAccomplishment extends RuntimeException{
public EnglishAccomplishment(){
System.out.println("成绩不能为负数,不能大于100分");
System.out.println("请重新输入学生英语成绩");
double English = Double.parseDouble(sc.nextLine());
stu.setEnglish(English);
}
}
(收集三条学生信息)目前只能写一条数据打印一条,希望大佬帮忙看下怎么打印全部。