8.1 练习1
键盘录入 5 个学生的成绩,求总和以及平均成绩,并显示每一个学生的成绩。
示例:
import java.util.Scanner;
/**
* 键盘录入5个学生的成绩,求总和以及平均成绩,并显示每一个学生的成绩。
*
* @author maxiaoke.com
* @version 1.0
*/
public class ArrayExercise1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 定义数组
double[] scores = new double[5];
// 向数组中添加分数
for (int i = 0; i < scores.length; i++) {
System.out.print("请输入第" + (i + 1) + "个学生的成绩:");
scores[i] = input.nextDouble();
}
// 求总和以及平均分
double total = 0;
for (int i = 0; i < scores.length; i++) {
total += scores[i];
}
System.out.println("学生成绩的总和是:" + total);
System.out.println("学生成绩的平均分是:" + (total / scores.length));
// 显示每一个学生的成绩
for (int i = 0; i < scores.length; i++) {
System.out.println("第" + (i + 1) + "个学生的成绩:" + scores[i]);
}
}
}
8.2 练习2
② 求选手的最后得分(去掉一个最高分和一个最低分后其余 8 位评委打分的平均值)
示例:
/**
* 分析以下需求,并用代码实现:
* ①在编程竞赛中,有10位评委为参赛的选手打分,分数分别为:5,4,6,8,9,0,1,2,7,3。
* ②求选手的最后得分(去掉一个最高分和一个最低分后其余8位评委打分的平均值)。
*
* @author maxiaoke.com
* @version 1.0
*/
public class ArrayExercise2 {
public static void main(String[] args) {
// 创建数组对象用来保存评委打分成绩
int[] scores = {5, 4, 6, 8, 9, 0, 1, 2, 7, 3};
// 遍历数组,求最高分和最低分
int max = scores[0];
int min = scores[0];
for (int i = 0; i < scores.length; i++) {
int score = scores[i];
if (score >= max) {
max = score;
}
if (score <= min) {
min = score;
}
}
// 遍历数组,遇到最高分和最低分跳过
int total = 0;
for (int i = 0; i < scores.length; i++) {
int score = scores[i];
/* 如果遇到最高分或最低分直接跳过累加,那么就会将所有的最高分跳过,极限情况下,如果打分都是最高分,那么所有的分数都会跳过,最终的平均分为0。这是错误的。
if (score == max || score == min) {
continue;
}
*/
total += score;
}
// 根据评分规则,最高分和最低分只去掉一个
total -= max;
total -= min;
// 平均分
double avg = (double) total / (scores.length - 2);
System.out.println("选手的最后得分:" + avg);
}
}
8.3 练习3
提示:考虑闰年。
示例:
import java.util.Scanner;
/**
* 已知:一年12个月每个月的总天数是:{ 31,28,31,30,31,30,31,31,30,31,30,31},从键盘输入年,月,日后,计算这一天是这一年的第几天。
* 提示:考虑闰年。
*
* @author maxiaoke.com
* @version 1.0
*/
public class ArrayExercise3 {
public static void main(String[] args) {
// 创建数组并保存每个月的天数
int[] dayOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 创建Scanner对象
Scanner input = new Scanner(System.in);
System.out.print("请输入年份(四位数):");
int year = input.nextInt();
System.out.print("请输入月份(1~12):");
int month = input.nextInt();
System.out.print("请输入天(1~31):");
int day = input.nextInt();
// 声明一个变量用来保存天数累加的结果
int sum = 0;
/*
在计算天数总和的时,分为两部分考虑:
① 已经经过的整月(如果用户指定的是1月,那么前面没有经过的整月)。
② 用户指定的当前月。
*/
/* 通过循环计算①,假设用户输入的月份是2,那么经过的整月是1 */
for (int i = 0; i < month - 1; i++) {
sum += dayOfMonth[i];
}
/* 计算② */
// 当前月的总天数
int days = dayOfMonth[month - 1];
/*
* 如果用户输入的是2月28之前的天数(包括28号),则不需要考虑闰年
*/
// 如果用户输入的天数 > 当前月的总天数,就按照当前月的总天数计算
if (month == 1 || (month == 2 && day <= 28)) {
sum += Math.min(day, days);
} else {
/*
* 判断闰年
* ①若某个年份能被4整除但不能被100整除,则是闰年。
* ②若某个年份能被400整除,则也是闰年。
*/
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
sum++;
}
}
System.out.println("sum = " + sum);
}
}
8.4 练习4
import java.util.Scanner;
/**
* @author maxiaoke.com
* @version 1.0
*/
public class ArrayExercise4 {
public static void main(String[] args) {
// 保存星期英文单词的数组
String[] weeks = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
// 创建Scanner对象
Scanner input = new Scanner(System.in);
System.out.print("请输入(1~7):");
int week = input.nextInt();
System.out.println(weeks[week - 1]);
}
}
8.5 练习5
用一个数组存储 26 个英文字母的小写形式,并遍历显示小写字母以及它对应的大写字母,例如:a -> A 。
示例:
/**
* 用一个数组存储26个英文字母的小写形式,并遍历显示小写字母以及它对应的大写字母,例如:a->A
*
* @author maxiaoke.com
* @version 1.0
*/
public class ArrayExercise5 {
public static void main(String[] args) {
// 创建一个保存26个英文字母的小写形式的数组
char[] chs = new char[26];
// 数组遍历,将26个英文字母的小写保存到数组中
for (int i = 0; i < 26; i++) {
chs[i] = (char) (97 + i);
}
// 数组遍历,显示小写字母以及它对应的大写字母
for (int i = 0; i < chs.length; i++) {
System.out.println(chs[i] + "->" + (char) (chs[i] - 32));
}
}
}
8.6 练习6
从键盘输入本组学员人数。
声明两个数组,一个存储本组学员的姓名,一个存储本组学员的成绩。
从键盘输入每一个人的姓名和成绩,分别存到两个数组中。
找出最高分和最低分的学员的姓名。
示例:
import java.util.Scanner;
/**
* 从键盘输入本组学员人数。
* <p>
* 声明两个数组,一个存储本组学员的姓名,一个存储本组学员的成绩。
* <p>
* 从键盘输入每一个人的姓名和成绩,分别存到两个数组中。
* <p>
* 找出最高分和最低分的学员的姓名。
*
* @author maxiaoke.com
* @version 1.0
*/
public class ArrayExercise6 {
public static void main(String[] args) {
// 从键盘输入本组学员人数
Scanner input = new Scanner(System.in);
System.out.print("请输入学员的人数:");
int num = input.nextInt();
// 声明两个数组,一个存储本组学员的姓名,一个存储本组学员的成绩。
String[] names = new String[num];
double[] scores = new double[num];
// 从键盘输入每一个人的姓名和成绩,分别存到两个数组中。
for (int i = 0; i < num; i++) {
System.out.print("请输入第" + (i + 1) + "个学员的姓名:");
String name = input.next();
names[i] = name;
System.out.print("请输入第" + (i + 1) + "个学员的成绩:");
double score = input.nextDouble();
scores[i] = score;
}
// 找出最高分和最低分的学员的姓名
double maxScore = scores[0];
int maxScoreIndex = 0;
double minScore = scores[0];
int minScoreIndex = 0;
for (int i = 0; i < scores.length; i++) {
double score = scores[i];
if (score >= maxScore) {
maxScore = score;
maxScoreIndex = i;
}
if (score <= minScore) {
minScore = score;
minScoreIndex = i;
}
}
System.out.println("最高分是:" + maxScore);
System.out.println("最高分的学员的名称是:" + names[maxScoreIndex]);
System.out.println("最低分是:" + minScore);
System.out.println("最低分的学员的名称是:" + names[minScoreIndex]);
}
}