📋 作业概览
本文档包含了Java学习第1天的三个核心作业,从基础变量操作到复杂的数组处理,帮助您快速掌握Java基础语法。
作业列表
- 个人信息管理系统 - 变量、条件判断、BMI计算
- 成绩统计器 - 数组操作、循环、统计计算
- 简易银行系统 - 综合应用、业务逻辑模拟
🏃♂️ 作业一:个人信息管理系统
📝 作业要求
- 创建变量存储:姓名、年龄、身高、体重、是否学生
- 计算BMI指数 (体重kg / (身高m)²)
- 根据BMI判断体重状态:<18.5偏瘦,18.5-24正常,>24偏重
- 输出完整的个人信息报告
🎯 学习重点
- 变量声明 :
String
,int
,double
,boolean
- 条件判断 :
if-else if-else
语句 - 数学计算:基本算术运算
- 字符串格式化 :
String.format()
💻 完整代码
java
/**
* 📝 第1天作业 - 个人信息管理系统
*/
public class PersonalInfoManager {
public static void main(String[] args) {
System.out.println("=== 个人信息管理系统 ===");
// 第1步 - 创建个人信息变量
String name = "张三"; // 姓名
int age = 25; // 年龄
double height = 1.75; // 身高(米)
double weight = 70.0; // 体重(公斤)
boolean isStudent = true; // 是否为学生
// 第2步 - 计算BMI指数
double bmi = weight / (height * height);
// 第3步 - 判断体重状态
String weightStatus;
String healthAdvice;
if (bmi < 18.5) {
weightStatus = "偏瘦";
healthAdvice = "建议增加营养摄入,适当增重";
} else if (bmi >= 18.5 && bmi < 24) {
weightStatus = "正常";
healthAdvice = "体重正常,保持健康的生活方式";
} else if (bmi >= 24 && bmi < 28) {
weightStatus = "偏重";
healthAdvice = "建议控制饮食,增加运动";
} else {
weightStatus = "肥胖";
healthAdvice = "建议制定减重计划,必要时咨询医生";
}
// 第4步 - 输出完整报告
System.out.println("\n=== 个人信息报告 ===");
System.out.println("姓名:" + name);
System.out.println("年龄:" + age + " 岁");
System.out.println("身高:" + height + " 米");
System.out.println("体重:" + weight + " 公斤");
System.out.println("学生身份:" + (isStudent ? "是" : "否"));
System.out.println("\n=== 健康分析 ===");
System.out.println("BMI指数:" + String.format("%.2f", bmi));
System.out.println("体重状态:" + weightStatus);
System.out.println("健康建议:" + healthAdvice);
// 扩展功能:年龄段分析
System.out.println("\n=== 年龄段分析 ===");
String ageGroup;
String lifeStage;
if (age < 18) {
ageGroup = "未成年人";
lifeStage = "成长发育期,注意营养均衡";
} else if (age < 30) {
ageGroup = "青年人";
lifeStage = "事业起步期,保持身体健康很重要";
} else if (age < 50) {
ageGroup = "中年人";
lifeStage = "事业黄金期,注意工作生活平衡";
} else {
ageGroup = "中老年人";
lifeStage = "经验丰富期,注重养生保健";
}
System.out.println("年龄段:" + ageGroup);
System.out.println("人生阶段:" + lifeStage);
// 扩展功能:综合评分
System.out.println("\n=== 综合健康评分 ===");
int healthScore = 100;
// BMI评分
if (bmi < 18.5 || bmi >= 28) {
healthScore -= 20;
} else if (bmi >= 24) {
healthScore -= 10;
}
// 年龄评分
if (age > 50) {
healthScore -= 5;
}
System.out.println("健康评分:" + healthScore + "/100");
if (healthScore >= 90) {
System.out.println("评级:优秀 ⭐⭐⭐⭐⭐");
} else if (healthScore >= 80) {
System.out.println("评级:良好 ⭐⭐⭐⭐");
} else if (healthScore >= 70) {
System.out.println("评级:一般 ⭐⭐⭐");
} else {
System.out.println("评级:需改善 ⭐⭐");
}
System.out.println("\n==================================================");
System.out.println("个人信息管理系统运行完成!");
}
// 扩展方法:计算标准体重
public static double calculateStandardWeight(double height) {
double heightInCm = height * 100;
return (heightInCm - 100) * 0.9;
}
// 扩展方法:获取BMI分类
public static String getBMICategory(double bmi) {
if (bmi < 18.5) return "偏瘦";
else if (bmi < 24) return "正常";
else if (bmi < 28) return "偏重";
else return "肥胖";
}
}
🎮 运行结果示例
diff
=== 个人信息管理系统 ===
=== 个人信息报告 ===
姓名:张三
年龄:25 岁
身高:1.75 米
体重:70.0 公斤
学生身份:是
=== 健康分析 ===
BMI指数:22.86
体重状态:正常
健康建议:体重正常,保持健康的生活方式
=== 年龄段分析 ===
年龄段:青年人
人生阶段:事业起步期,保持身体健康很重要
=== 综合健康评分 ===
健康评分:100/100
评级:优秀 ⭐⭐⭐⭐⭐
📊 作业二:成绩统计器
📝 作业要求
- 创建一个包含10个学生成绩的数组
- 计算平均分、最高分、最低分
- 统计各个分数段的人数:90+优秀,80-89良好,60-79及格,<60不及格
- 输出详细统计报告
🎯 学习重点
- 数组操作:声明、初始化、遍历
- 循环结构 :
for
循环、增强for循环 - 统计算法:求和、最值、计数
- 排序算法:冒泡排序
💻 完整代码
java
/**
* 📝 第1天作业 - 成绩统计器
*/
public class GradeStatistics {
public static void main(String[] args) {
System.out.println("=== 学生成绩统计系统 ===");
// 第1步 - 创建成绩数组
int[] scores = {85, 92, 78, 96, 73, 88, 91, 67, 89, 94};
String[] studentNames = {
"张三", "李四", "王五", "赵六", "孙七",
"周八", "吴九", "郑十", "钱一", "孙二"
};
System.out.println("班级人数:" + scores.length + " 人");
// 显示所有学生成绩
System.out.println("\n=== 学生成绩列表 ===");
for (int i = 0; i < scores.length; i++) {
System.out.println((i + 1) + ". " + studentNames[i] + ":" + scores[i] + " 分");
}
// 第2步 - 计算基本统计数据
// 计算总分
int totalScore = 0;
for (int score : scores) {
totalScore += score;
}
// 计算平均分
double averageScore = (double) totalScore / scores.length;
// 找最高分
int maxScore = scores[0];
String topStudent = studentNames[0];
for (int i = 1; i < scores.length; i++) {
if (scores[i] > maxScore) {
maxScore = scores[i];
topStudent = studentNames[i];
}
}
// 找最低分
int minScore = scores[0];
String bottomStudent = studentNames[0];
for (int i = 1; i < scores.length; i++) {
if (scores[i] < minScore) {
minScore = scores[i];
bottomStudent = studentNames[i];
}
}
// 第3步 - 统计各分数段人数
int excellentCount = 0; // 90+ 优秀
int goodCount = 0; // 80-89 良好
int passCount = 0; // 60-79 及格
int failCount = 0; // <60 不及格
for (int score : scores) {
if (score >= 90) {
excellentCount++;
} else if (score >= 80) {
goodCount++;
} else if (score >= 60) {
passCount++;
} else {
failCount++;
}
}
// 第4步 - 输出统计报告
System.out.println("\n=== 成绩统计报告 ===");
// 基本统计信息
System.out.println("--- 基本统计 ---");
System.out.println("总分:" + totalScore + " 分");
System.out.println("平均分:" + String.format("%.2f", averageScore) + " 分");
System.out.println("最高分:" + maxScore + " 分 (" + topStudent + ")");
System.out.println("最低分:" + minScore + " 分 (" + bottomStudent + ")");
System.out.println("分数范围:" + (maxScore - minScore) + " 分");
// 分数段统计
System.out.println("\n--- 分数段统计 ---");
System.out.println("优秀 (90-100分):" + excellentCount + " 人 " +
String.format("(%.1f%%)", (double) excellentCount / scores.length * 100));
System.out.println("良好 (80-89分):" + goodCount + " 人 " +
String.format("(%.1f%%)", (double) goodCount / scores.length * 100));
System.out.println("及格 (60-79分):" + passCount + " 人 " +
String.format("(%.1f%%)", (double) passCount / scores.length * 100));
System.out.println("不及格 (<60分):" + failCount + " 人 " +
String.format("(%.1f%%)", (double) failCount / scores.length * 100));
// 及格率统计
int totalPassCount = excellentCount + goodCount + passCount;
double passRate = (double) totalPassCount / scores.length * 100;
System.out.println("\n--- 通过率分析 ---");
System.out.println("及格人数:" + totalPassCount + " 人");
System.out.println("及格率:" + String.format("%.1f%%", passRate));
if (passRate >= 90) {
System.out.println("班级表现:优秀!");
} else if (passRate >= 80) {
System.out.println("班级表现:良好");
} else if (passRate >= 70) {
System.out.println("班级表现:一般");
} else {
System.out.println("班级表现:需要提高");
}
// 扩展分析:成绩排名 (前5名)
System.out.println("\n--- 成绩排名 (前5名) ---");
// 使用冒泡排序
int[] sortedScores = new int[scores.length];
String[] sortedNames = new String[studentNames.length];
// 复制数组
for (int i = 0; i < scores.length; i++) {
sortedScores[i] = scores[i];
sortedNames[i] = studentNames[i];
}
// 按成绩降序排序
for (int i = 0; i < sortedScores.length - 1; i++) {
for (int j = 0; j < sortedScores.length - 1 - i; j++) {
if (sortedScores[j] < sortedScores[j + 1]) {
// 交换成绩
int tempScore = sortedScores[j];
sortedScores[j] = sortedScores[j + 1];
sortedScores[j + 1] = tempScore;
// 交换姓名
String tempName = sortedNames[j];
sortedNames[j] = sortedNames[j + 1];
sortedNames[j + 1] = tempName;
}
}
}
// 显示前5名
int displayCount = Math.min(5, sortedScores.length);
for (int i = 0; i < displayCount; i++) {
String medal = "";
switch (i) {
case 0: medal = "🥇"; break;
case 1: medal = "🥈"; break;
case 2: medal = "🥉"; break;
default: medal = " "; break;
}
System.out.println("第" + (i + 1) + "名:" + medal + " " +
sortedNames[i] + " - " + sortedScores[i] + "分");
}
System.out.println("\n==================================================");
System.out.println("成绩统计分析完成!");
}
// 扩展方法:获取等级字符串
public static String getGradeLevel(int score) {
if (score >= 90) return "优秀";
else if (score >= 80) return "良好";
else if (score >= 60) return "及格";
else return "不及格";
}
// 扩展方法:计算中位数
public static double calculateMedian(int[] scores) {
// 复制并排序数组
int[] sortedScores = new int[scores.length];
for (int i = 0; i < scores.length; i++) {
sortedScores[i] = scores[i];
}
// 简单排序
for (int i = 0; i < sortedScores.length - 1; i++) {
for (int j = 0; j < sortedScores.length - 1 - i; j++) {
if (sortedScores[j] > sortedScores[j + 1]) {
int temp = sortedScores[j];
sortedScores[j] = sortedScores[j + 1];
sortedScores[j + 1] = temp;
}
}
}
// 计算中位数
int length = sortedScores.length;
if (length % 2 == 0) {
return (sortedScores[length / 2 - 1] + sortedScores[length / 2]) / 2.0;
} else {
return sortedScores[length / 2];
}
}
}
🎮 运行结果示例
scss
=== 学生成绩统计系统 ===
班级人数:10 人
=== 学生成绩列表 ===
1. 张三:85 分
2. 李四:92 分
3. 王五:78 分
...
=== 成绩统计报告 ===
--- 基本统计 ---
总分:853 分
平均分:85.30 分
最高分:96 分 (赵六)
最低分:67 分 (郑十)
--- 分数段统计 ---
优秀 (90-100分):4 人 (40.0%)
良好 (80-89分):4 人 (40.0%)
及格 (60-79分):2 人 (20.0%)
不及格 (<60分):0 人 (0.0%)
--- 成绩排名 (前5名) ---
第1名:🥇 赵六 - 96分
第2名:🥈 孙二 - 94分
第3名:🥉 李四 - 92分
第4名: 周八 - 91分
第5名: 钱一 - 89分
🏦 作业三:简易银行系统
📝 作业要求
- 模拟银行账户:初始余额1000元
- 模拟一系列操作:存款、取款、查询余额
- 记录每次操作后的余额变化
- 最后输出操作历史和最终余额
🎯 学习重点
- 综合应用:变量、数组、循环、条件判断
- 业务逻辑:银行操作模拟
- 数据记录:操作历史追踪
- 格式化输出:专业报告展示
💻 完整代码
java
/**
* 📝 第1天挑战题 - 简易银行系统
*/
public class SimpleBankSystem {
public static void main(String[] args) {
System.out.println("=== 💰 简易银行系统 ===");
System.out.println("欢迎使用Java学习银行!");
// 初始化账户信息
String accountName = "张三";
String accountNumber = "6228480012345678";
double balance = 1000.0; // 初始余额1000元
String bankName = "Java学习银行";
// 操作记录数组
String[] operationHistory = new String[20];
double[] balanceHistory = new double[20];
int operationCount = 0;
System.out.println("\n=== 账户信息 ===");
System.out.println("银行名称:" + bankName);
System.out.println("账户持有人:" + accountName);
System.out.println("卡号:" + accountNumber);
System.out.println("初始余额:¥" + String.format("%.2f", balance));
// 开始模拟银行操作
System.out.println("\n=== 开始模拟银行操作 ===");
// 操作1:查询余额
System.out.println("\n--- 操作1:查询余额 ---");
System.out.println("当前余额:¥" + String.format("%.2f", balance));
operationHistory[operationCount] = "查询余额";
balanceHistory[operationCount] = balance;
operationCount++;
// 操作2:存款500元
System.out.println("\n--- 操作2:存款 ---");
double depositAmount = 500.0;
if (depositAmount > 0) {
balance += depositAmount;
System.out.println("存款金额:¥" + String.format("%.2f", depositAmount));
System.out.println("存款成功!当前余额:¥" + String.format("%.2f", balance));
operationHistory[operationCount] = "存款 ¥" + String.format("%.2f", depositAmount);
balanceHistory[operationCount] = balance;
operationCount++;
}
// 操作3:取款200元
System.out.println("\n--- 操作3:取款 ---");
double withdrawAmount = 200.0;
if (withdrawAmount > 0 && withdrawAmount <= balance) {
balance -= withdrawAmount;
System.out.println("取款金额:¥" + String.format("%.2f", withdrawAmount));
System.out.println("取款成功!当前余额:¥" + String.format("%.2f", balance));
operationHistory[operationCount] = "取款 ¥" + String.format("%.2f", withdrawAmount);
balanceHistory[operationCount] = balance;
operationCount++;
}
// 操作4:尝试大额取款(余额不足)
System.out.println("\n--- 操作4:尝试大额取款 ---");
double largeWithdraw = 2000.0;
System.out.println("尝试取款:¥" + String.format("%.2f", largeWithdraw));
if (largeWithdraw <= balance) {
balance -= largeWithdraw;
System.out.println("取款成功!当前余额:¥" + String.format("%.2f", balance));
operationHistory[operationCount] = "取款 ¥" + String.format("%.2f", largeWithdraw);
} else {
System.out.println("❌ 取款失败:余额不足!");
System.out.println("当前余额:¥" + String.format("%.2f", balance));
System.out.println("缺少金额:¥" + String.format("%.2f", largeWithdraw - balance));
operationHistory[operationCount] = "取款失败 - 余额不足";
}
balanceHistory[operationCount] = balance;
operationCount++;
// 操作5-7:连续小额存款
System.out.println("\n--- 操作5-7:连续小额存款 ---");
double[] deposits = {100.0, 50.0, 25.0};
for (int i = 0; i < deposits.length; i++) {
double amount = deposits[i];
balance += amount;
System.out.println("第" + (i + 1) + "次存款:¥" + String.format("%.2f", amount) +
" | 余额:¥" + String.format("%.2f", balance));
operationHistory[operationCount] = "小额存款 ¥" + String.format("%.2f", amount);
balanceHistory[operationCount] = balance;
operationCount++;
}
// 操作8:转账
System.out.println("\n--- 操作8:转账 ---");
String transferTo = "李四";
double transferAmount = 300.0;
System.out.println("转账给:" + transferTo);
System.out.println("转账金额:¥" + String.format("%.2f", transferAmount));
if (transferAmount <= balance) {
balance -= transferAmount;
double fee = 1.0; // 手续费1元
balance -= fee;
System.out.println("转账成功!");
System.out.println("手续费:¥" + String.format("%.2f", fee));
System.out.println("当前余额:¥" + String.format("%.2f", balance));
operationHistory[operationCount] = "转账给" + transferTo + " ¥" + String.format("%.2f", transferAmount);
} else {
System.out.println("转账失败:余额不足!");
operationHistory[operationCount] = "转账失败 - 余额不足";
}
balanceHistory[operationCount] = balance;
operationCount++;
// 最终查询
System.out.println("\n--- 操作9:最终余额查询 ---");
System.out.println("最终余额:¥" + String.format("%.2f", balance));
operationHistory[operationCount] = "最终查询";
balanceHistory[operationCount] = balance;
operationCount++;
// 输出操作历史报告
System.out.println("\n==================================================");
System.out.println("=== 📊 账户操作历史报告 ===");
System.out.println("==================================================");
System.out.println("账户持有人:" + accountName);
System.out.println("银行卡号:" + accountNumber);
System.out.println("总操作次数:" + operationCount + " 次");
System.out.println("\n--- 详细操作记录 ---");
for (int i = 0; i < operationCount; i++) {
System.out.printf("第%2d次操作:%-25s | 余额:¥%8.2f%n",
(i + 1), operationHistory[i], balanceHistory[i]);
}
// 统计分析
System.out.println("\n--- 📈 操作统计分析 ---");
double initialBalance = 1000.0;
double finalBalance = balance;
double totalChange = finalBalance - initialBalance;
System.out.println("初始余额:¥" + String.format("%.2f", initialBalance));
System.out.println("最终余额:¥" + String.format("%.2f", finalBalance));
System.out.println("余额变动:¥" + String.format("%.2f", totalChange) +
(totalChange >= 0 ? " (增加)" : " (减少)"));
// 账户等级评估
System.out.println("\n--- 💎 账户等级评估 ---");
String accountLevel;
if (finalBalance >= 2000) {
accountLevel = "金卡客户";
} else if (finalBalance >= 1000) {
accountLevel = "银卡客户";
} else {
accountLevel = "普通客户";
}
System.out.println("账户等级:" + accountLevel);
// 理财建议
System.out.println("\n--- 💡 理财建议 ---");
if (finalBalance < 500) {
System.out.println("💰 建议:余额较低,建议增加储蓄");
} else if (finalBalance < 2000) {
System.out.println("📈 建议:可以考虑定期存款");
} else {
System.out.println("🏆 建议:可以考虑理财产品投资");
}
System.out.println("\n==================================================");
System.out.println("🎉 银行系统操作演示完成!");
System.out.println("感谢使用 " + bankName + "!");
System.out.println("==================================================");
System.out.println("\n💡 编程学习总结:");
System.out.println("✓ 使用了变量存储账户信息");
System.out.println("✓ 使用了数组记录操作历史");
System.out.println("✓ 使用了循环处理连续操作");
System.out.println("✓ 使用了条件判断验证操作");
System.out.println("✓ 使用了字符串格式化显示金额");
System.out.println("✓ 实现了简单的业务逻辑");
}
// 扩展方法:验证金额有效性
public static boolean isValidAmount(double amount) {
return amount > 0 && amount <= 50000;
}
// 扩展方法:计算转账手续费
public static double calculateTransferFee(double amount) {
double fee = amount * 0.001;
if (fee < 1.0) fee = 1.0;
if (fee > 50.0) fee = 50.0;
return fee;
}
// 扩展方法:获取账户等级
public static String getAccountLevel(double balance) {
if (balance >= 5000) return "白金卡";
else if (balance >= 3000) return "金卡";
else if (balance >= 1000) return "银卡";
else return "普通卡";
}
}
🎮 运行结果示例
diff
=== 💰 简易银行系统 ===
欢迎使用Java学习银行!
=== 账户信息 ===
银行名称:Java学习银行
账户持有人:张三
卡号:6228480012345678
初始余额:¥1000.00
=== 开始模拟银行操作 ===
--- 操作1:查询余额 ---
当前余额:¥1000.00
--- 操作2:存款 ---
存款金额:¥500.00
存款成功!当前余额:¥1500.00
...
=== 📊 账户操作历史报告 ===
账户持有人:张三
银行卡号:6228480012345678
总操作次数:9 次
--- 详细操作记录 ---
第 1次操作:查询余额 | 余额:¥ 1000.00
第 2次操作:存款 ¥500.00 | 余额:¥ 1500.00
第 3次操作:取款 ¥200.00 | 余额:¥ 1300.00
...
--- 💎 账户等级评估 ---
账户等级:银卡客户
--- 💡 理财建议 ---
📈 建议:可以考虑定期存款
🎯 学习总结
✅ 完成的学习目标
通过这三个作业,您已经掌握了:
-
基础语法
- 变量声明和使用
- 基本数据类型:
int
,double
,String
,boolean
- 运算符使用
-
控制结构
- 条件判断:
if-else if-else
- 循环结构:
for
循环、增强for循环 - 分支语句:
switch-case
- 条件判断:
-
数组操作
- 数组声明和初始化
- 数组遍历和操作
- 多数组协同处理
-
方法定义
- 静态方法的创建
- 参数传递和返回值
- 方法重载概念
-
实际应用
- 业务逻辑实现
- 数据统计和分析
- 格式化输出
📈 进阶建议
-
代码优化
- 尝试使用Java 8的Stream API重写统计逻辑
- 学习使用
ArrayList
替代固定大小数组 - 引入异常处理机制
-
功能扩展
- 为银行系统添加多账户支持
- 实现数据持久化(文件读写)
- 添加简单的用户界面
-
学习路径
- 继续学习面向对象编程
- 掌握集合框架的使用
- 学习数据库操作基础
🎊 恭喜完成第1天学习!
您已经成功完成了Java学习的第一天,从基础语法到实际应用,为后续的学习打下了坚实的基础。继续保持这种学习节奏,您很快就能掌握Java的核心技能!