java第⼗版基础篇第七章答案_Java语⾔程序设计(基础篇)(原书第10版)练习题答案第。。。
基础篇课后习题答案,做了⼤部分,虽然不⼀定是最佳代码,但是保证每个都能运⾏,如有更好的答案,欢迎讨论
7.1 指定等级
java程序设计基础视频public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输⼊学⽣⼈数:");
int num = Int();
int[] grade = new int[num];
System.out.print("请输⼊所有分数:");
for (int i = 0; i < num; i++) {
grade[i] = Int();
if (grade[i] > 100 || grade[i] < 0) {
System.out.print("⽆效成绩");
}
}
for (int i = 0; i < num; i++) {
System.out.println("第" + (i + 1) + "位学⽣的分数是" + grade[i] + "分,对应的成绩为" + result(grade[i]));
}
}
private static char result(int grade) {
char[] result = {'A', 'B', 'C', 'D', 'F'};
int i;
if (grade >= 90 && grade <= 100) {
i = 0;
} else if (grade >= 80 && grade < 90) {
i = 1;
} else if (grade >= 70 && grade < 80) {
i = 2;
} else if (grade >= 60 && grade < 70) {
i = 3;
} else {
i = 4;
}
return result[i];
}
7.2 数字倒置
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输⼊10个整数:");
int[] num = new int[10];
for (int i = 0; i < 10; i++) {
num[i] = Int();
}
for (int i : newNum(num)) {
System.out.print(i + " ");
}
}
private static int[] newNum(int[] num) {
int tmp;
for (int i = 0; i < num.length / 2; i++) {
tmp = num[i];
num[i] = num[num.length - 1 - i];
num[num.length - 1 - i] = tmp;
}
return num;
}
7.3 计算数字出现的次数
public static void main(String[] args) {
int[] num = createArrays();
Arrays.sort(num);
System.out.println("数据升序排列后为:" + String(num));
for (int i = 0; i < num.length; i++) {
if (i == 0 || (num[i] != num[i - 1])) {
System.out.println(num[i] + "在数组中出现了" + getCount(num[i], num) + "次"); }
}
}
private static int[] createArrays() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输⼊1~100之间的整数(以0作为结束):"); int[] num = new int[100];
int i = 0;
int input;
while (i >= 0) {
input = Int();
if (input != 0) {
num[i] = input;
i++;
} else {
break;
}
}
int zeroIndex = getZeroIndex(num);
int[] newNum = new int[zeroIndex];
System.arraycopy(num, 0, newNum, 0, zeroIndex);
return newNum;
}
private static int getZeroIndex(int[] num) {
for (int i = 0; i < num.length; i++) {
if (num[i] == 0) {
return i;
}
}
return -1;
}
private static int getCount(int indexNum, int[] num) {
int count = 0;
for (int i = 0; i < num.length; i++) {
if (indexNum == num[i]) {
count++;
}
}
return count;
}
7.4 分析成绩
public static void main(String[] args) {
int[] num = createArrays();
double averagePoint = averagePoint(num);
System.out.println("分数分别是:" + String(num)); System.out.println("平均分是:" + averagePoint); countResult(num, averagePoint);
}
private static int[] createArrays() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输⼊成绩(1~100之间的整数):");
int[] num = new int[100];
int i = 0;
int input;
while (i >= 0) {
input = Int();
if (input >= 0 && input <= 100) {
num[i] = input;
i++;
} else if (input > 100) {
System.out.print("输⼊的分数超出最⼤值!");
} else {
break;
}
}
int zeroIndex = getZeroIndex(num);
int[] newNum = new int[zeroIndex];
System.arraycopy(num, 0, newNum, 0, zeroIndex);
return newNum;
}
private static int getZeroIndex(int[] num) {
for (int i = 0; i < num.length; i++) {
if (num[i] == 0) {
return i;
}
}
return -1;
}
private static double averagePoint(int[] num) {
double averagePoint;
int length = num.length;
double totalPoint = 0;
for (int i = 0; i < num.length; i++) {
totalPoint = totalPoint + num[i];
}
averagePoint = totalPoint / length;
return averagePoint;
}
private static void countResult(int[] num, double averagePoint) { int count1 = 0, count2 = 0;
for (int i = 0; i < num.length; i++) {
if ((double) num[i] >= averagePoint) {
count1++;
}
if ((double) num[i] < averagePoint) {
count2++;
}
}
System.out.println("⼤于等于平均分的成绩个数为:" + count1); System.out.print("⼩于平均分的成绩个数为:" + count2);
}
7.5 打印不同的数
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论