学生成绩录入项目
一,题目要求‘
总要求:
* 现在要求设计一个表示学生的类,里面有学生的三项成绩:计算机成绩、数学成绩、英语成绩
* 要求可以求总分、平均分、最高、最低分,并且可以输出一个学生的完整信息。 问:此类该如何设计?
* 分析:先给出一个问题的解决思路
* · 根据需要设计出类
* · 根据提示的内容定义类中的属性并指定好相关的类型
* · 所有的属性必须封装,封装之后的属性通过 setter 及 getter 方法设置和取得
* · 根据属于对象的行为应该被设计为方法定义在类中,不应该出现在类之外。
* · 如果有需要则可以增加构造方法,通过构造设置属性内容,但是在编写构造的时候强烈建议,一定要保留无参构造方法。
* 这一点在以后的开发中非常重要。
学生管理系统的几个阶段
*
* 1. 不带控制台录入的 学生成绩管理系统
*
* 2. 带录入的 (录入一个学生成绩)
*
* 3. 带校验的
*
* 4. 带录入多个学生 以及校验
第一 二个阶段:
设计一个类 ,属性有,姓名,英语成绩,数学成绩,计算机成绩 。 方法:总分, 平均分,最高分,最低分,分数展示
然后成绩录入
public class Student { private String name; private int math; private int english; private int computer; public Student() { } public Student(String name, int math, int english, int computer) { this.name = name; this.math = math; this.english = english; this.computer = computer; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMath() { return math; } public void setMath(int math) { this.math = math; } public int getEnglish() { return english; } public void setEnglish(int english) { this.english = english; } public int getComputer() { return computer; } public void setComputer(int computer) { this.computer = computer; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", math=" + math + ", english=" + english + ", computer=" + computer + '}'; } //总分 public int sumScore(){ int total = math + english + computer; return total; } //平均分 public int avgScore(){ int total = sumScore(); return total /3; } //最到分 public int maxScore(){ int max = math; if (computer > math){ max = computer; } if (english > max){ max = english; } return max; } //最低分 public int minScore(){ int min = math; if (computer < math){ min = computer; } if (english < math){ min = english; } return min; } //分数展示 public void showScore(){ System.out.println("姓名 :" + name + "数学 :" + math + "英语 :" + english + "计算机:" + computer + "总分 :" + sumScore() + " 平均分:" + avgScore() + "最高分 " + maxScore() + "最低分" + minScore()); } }
public class StudentScore { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Student student = new Student(); System.out.println("请输入你的姓名"); String name = scanner.nextLine(); student.setName(name); System.out.println("请输入数学成绩"); int math = scanner.nextInt(); student.setMath(math); System.out.println("请输入英语成绩"); int english = scanner.nextInt(); student.setEnglish(english); System.out.println("请输入计算机成绩"); int computer = scanner.nextInt(); student.setComputer(computer); student.showScore(); } }
第二个阶段
// 单个学生成绩录入(带校验)
// 校验 :
// 基本校验: 不能为空
// 1. 姓名: 不能为空
// 2. 成绩: 不能为负数, 要在指定范围内
// 输入的必须是一个整数, 但是对方是可以输入字符串的, 如果输入了别的字符串,则应重新输入
// todo 额外校验 加分项 可以不添加
// 1. 输入的数字带有+号
// 2. 输入的数字 直接是回车
// 3. 名字过长
// 4. 重名校验
// 5. 名字中间有空格 非法字符
1. 对名字进行校验,必须在setName()方法中进行校验,代码如下 名字不能为空,名字不能有空格
public void setName(String name) { //判断字符串是否为空 // 如果对方输入的是空, 则需要对方重新输入姓名, 否则则录入成功 Scanner scanner = new Scanner(System.in); name = name.trim(); // 去除空格的意思 while (true) { if (name.isEmpty()){ System.err.println("您输入名称是空, 请重新输入"); name = scanner.nextLine(); name = name.trim(); }else { break; } } this.name = name; }
2. 对成绩进行校验
注意事项
1.)
在setMath()进行校验
1》 输入的必须是一个整数, 但是对方是可以输入字符串的, 如果输入了别的字符串,则应重新输入
2》不能为负数, 要在指定范围内
用字符串接收,如果客户输入的是数字则将数字字符串转换成数字
System.out.println("请输入数学成绩");
String math = scanner.nextLine();
student.setMath(math);
//==========================================
//判断传入字符串是否是纯数字字符串
public boolean isNumberString(String str){
//首先拆开这个字符串 得到字符数组
char[] chars = str.toCharArray();
// 遍历这个数组, 判断每个字符是否在 '0' '9'的ASCII码值之内
// 还需要判断第一个字符是否是负数
for (int i = 0; i < chars.length; i++) {
char aChar = chars[i];
if (i == 0){
if (aChar == '-'){
continue;
}
}
if (aChar < '0' || aChar > '9' ){
return false;
}
}
return true;
}
========================================
public void setMath(String math) {
Scanner scanner = new Scanner(System.in);
while (true) {
if (!isNumberString(math)){
System.out.println("你输入的不是纯数字, 请重新录入:");
math = scanner.nextLine();
}else {
int mathScore = Integer.parseInt(math);
if (mathScore >= 0 && mathScore <= 100){
break;
}else {
System.out.println("您输入的数字超过范围(0-100), 请重新录入:");
math = scanner.nextLine();
}
}
}
this.math = Integer.parseInt(math);
}
英语 计算机成绩 如法炮制
public void setEnglish(String english) { Scanner scanner = new Scanner(System.in); while (true) { if (!isNumberString(english)){ System.out.println("请输入正确的成绩"); english = scanner.nextLine(); }else { int englishScore = Integer.parseInt(english); if (englishScore >= 0 && englishScore <= 100){ break; }else { System.out.println("请输入正确的成绩"); english = scanner.nextLine(); } } } this.english = Integer.parseInt(english); } //================================================================================= public void setComputer(String computer) { Scanner scanner = new Scanner(System.in); while (true) { if (!isNumberString(computer)){ System.out.println("请输入正确的成绩"); computer = scanner.nextLine(); }else { int computerScore = Integer.parseInt(computer); if (computerScore >= 0 && computerScore <= 100){ break; }else { System.out.println("你输入的成绩超范围"); computer = scanner.nextLine(); } } } this.computer = Integer.parseInt(computer); }
思路: 录入多个学生
1. 接收学生数量
2. 定义一个学生数组
3. 把学生放进学生数组 然后遍历
4. 确认是否还要继续接收 yes
public class StudentScore { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("有几个学生要录入"); int num = scanner.nextInt(); scanner.nextLine(); // 接收空格键 Student[] students = new Student[num]; //定义学生数组 for (int i = 0; i < num; i++) { Student student = new Student(); System.out.println("请输入你的姓名"); String name = scanner.nextLine(); student.setName(name); System.out.println("请输入数学成绩"); String math = scanner.nextLine(); student.setMath(math); System.out.println("请输入英语成绩"); String english = scanner.nextLine(); student.setEnglish(english); System.out.println("请输入计算机成绩"); String computer = scanner.nextLine(); student.setComputer(computer); students[i] = student; System.out.println("=============学生录入完成============="); System.out.println("------------------------------------"); System.out.println("是否继续录入?输入yes 表示继续"); String goOn = scanner.nextLine(); if (!goOn.equalsIgnoreCase("yes")){ System.out.println("学生信息录入完成"); break; } } for (Student student : students) { student.showScore(); } } }
优化代码 可以减少代码的冗余
public String shuziJiaoYan(String shuzi){ Scanner scanner = new Scanner(System.in); while (true) { if (!isNumberString(shuzi)){ System.out.println("你输入的不是纯数字, 请重新录入:"); shuzi = scanner.nextLine(); }else { int mathScore = Integer.parseInt(shuzi); if (mathScore >= 0 && mathScore <= 100){ break; }else { System.out.println("您输入的数字超过范围(0-100), 请重新录入:"); shuzi = scanner.nextLine(); } } } return shuzi; } //===================================================================================================================== public void setMath(String math) { math = shuziJiaoYan(math); this.math = Integer.parseInt(math); }
重点难点:名字和成绩的校验 , 代码的提取

浙公网安备 33010602011771号