1.1 分解项目 |
|
1.1.1 创建类 |
|
Student
属性
方法
package com.ffyc; public class Student { private String num; private String name; private double score; public Student() { } public Student(String num, String name, double score) { this.num = num; this.name = name; this.score = score; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getName() { return name; } public void setName(String name) { |
this.name = name; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } } | ||
1.1.2 创建数组 |
| |
|
Student[] students = new Student[5];
1.1.3 对数组操作 |
| |
| ||
1.2 扩展 |
|
Student类扩展toString()方法
|
package com.ffyc; import java.util.Scanner; /** * 管理学生 */ public class Sims { private Student[] students = new Student[10000]; private int count; private Scanner sc = new Scanner(System.in); public double sum(){ double sum = 0.0; for (int i = 0; i < count; i++) { sum += students[i].getScore(); } return sum; } public Student maxScore(){ Student max = students[0]; for(int i=0; i<count; i++){ if(students[i].getScore()> max.getScore()){ max = students[i]; } } return max; } public double avg(){ return sum()/count; } public void addStudent(){ while (true) { |
System.out.print("请输入学号: ");
String num = sc.nextLine();
System.out.print("请输入姓名: ");
String name = sc.nextLine();
System.out.print("请输入成绩: ");
double score = Double.parseDouble(sc.nextLine());
Student student = new Student(num, name, score);
students[count++] = student;
System.out.print("是否继续(y/n)");
String yesOrNo = sc.nextLine();
if ("n".equals(yesOrNo)) {
break;
}
}
}
}
1.2.1 成绩第二大 |
|
在数组中找到第二大数
/**
* 找出第二个大成绩
*
* @return
*/
public Student secondMaxScore() {
if (count < 2) return null;
Student max = students[0];
Student second = students[1];
//检查max是最大的成绩
if (second.getScore() > max.getScore()) {
Student tmp = max;
max = second;
second = tmp;
}
for (int i = 2; i < count; i++) {
Student current = students[i];
if (current.getScore() > max.getScore()) {
second = max;
max = current;
} else if (current.getScore() > second.getScore() && current.getScore() <
max.getScore()) {
second = current;
} else {
continue;
}
}
return second;
} | ||
1.2.2 查找某个学生的信息 |
| |
| ||
2 辅助类 |
| |
2.1 System |
| |
| ||
2.2 Scanner |
| |
| ||
2.3 Random |
| |
| ||
2.4 Math |
| |
| ||
2.5 日期 |
| |
2.5.1 Date |
|
2.5.1.1 获取当前时间 |
| |
|
while(true){
Date dx = new Date(); System.out.println(dx); System.out.println(dx.getTime()); //延迟1秒 Thread.sleep(1000); } |
2.5.2 System.currentTimeMillis() |
| |
| ||
2.5.3 SimpleDateFormat |
| |
|
2.5.3.1 Date对象格式化输出 |
| |
|
Date d = new Date();
//格式化输出 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String today = simpleDateFormat.format(d); System.out.println(today); |
2.5.3.2 将字符串转换为Date 2.5.4 Calendar 日历 |
| |
|
//今天是今天第?天
//创建日历对象 Calendar instance = Calendar.getInstance(); System.out.println("年: "+instance.get(Calendar.YEAR)); System.out.println("月: "+(instance.get(Calendar.MONTH)+1)); System.out.println("日: "+instance.get(Calendar.DATE)); System.out.println("第几天: "+ instance.get(Calendar.DAY_OF_YEAR)); |
问题:计算7.10日还有多少天。