Java语言程序设计(基础篇)(原书第10版) 练习题答案 第5章

时间:2021-03-10 21:49:38

基础篇课后习题答案,做了大部分,虽然不一定是最佳代码,但是保证每个都能运行,如有更好的答案,欢迎讨论


5.1 计算平均值等数学题

Scanner scanner = new Scanner(System.in);
System.out.print("请输入数字,以0结束:");
int num = scanner.nextInt();
int positive = 0, negative = 0;
double total = 0d, average = 0d;
if (num != 0) {
    while (num != 0) {
        if (num > 0) {
            positive++;
        } else {
            negative++;
        }
        total = total + num;
        num = scanner.nextInt();
    }
    average = total / (positive + negative);
} else {
    System.out.println("起始数字不能为0");
}
System.out.println("正数的个数是:" + positive);
System.out.println("负数的个数是:" + negative);
System.out.println("总和是:" + total);
System.out.println("平均数是:" + average);


5.2 重复加法

Scanner scanner = new Scanner(System.in);
int count = 0;
int correct = 0;
while (count <= 20) {
    int num1 = (int) (Math.random() * 10);
    int num2 = (int) (Math.random() * 10);
    System.out.print(num1 + " + " + num2 + " = ");
    int answer = scanner.nextInt();
    if (answer == num1 + num2) {
        System.out.println("你答对了!");
        correct++;
    } else {
        System.out.println("你答错了!");
    }
    count++;
}
System.out.println("共20道题,你总共答对了" + correct + "道题");


5.3 千克转换

System.out.printf("%-5s%8s\n", "千克", "镑");
for (int i = 1; i < 200; i = i + 2) {
    System.out.printf("%-5d%10.1f\n", i, i * 2.2f);
}


5.4 英里转换

System.out.printf("%-5s%8s\n", "英里", "千米");
for (int i = 1; i <= 10; i++) {
    System.out.printf("%-12d%-1.3f\n", i, i * 1.609f);
}


5.5 千克转换

System.out.printf("%-10s%-10s%-10s%-10s\n", "千克", "镑", "镑", "千克");

for (int i = 1, j = 20; i < 200 && j <= 515; i = i + 2, j = j + 5) {
    System.out.printf("%-8d%5.1f%11d%12.2f\n", i, i * 2.2f, j, j / 2.2f);
}


5.7 计算未来学费

double fee = 10000;
for (int i = 1; i <= 10; i++) {
    fee = fee * (1 + 0.05);
}
System.out.println("10年后的学费是:" + (int) Math.round(fee));

for (int i = 1; i <= 4; i++) {
    fee = fee * (1 + 0.05);
}
System.out.println("10年后4年内的学费是:" + (int) Math.round(fee));


5.8 找出最高分

Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生个数:");
int count = scanner.nextInt();
String nameList[] = new String[count];
int point[] = new int[count];
int best = 0;

Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
for (int i = 0; i < count; i++) {
    System.out.print("请输入第" + (i + 1) + "个学生的姓名:");
    nameList[i] = input1.nextLine();
    System.out.print("请输入第" + (i + 1) + "个学生的成绩:");
    point[i] = input2.nextInt();

    if (i > 0 && (point[i] > point[best])) {
        best = i;
    }
}
System.out.println("成绩最高的学生是:" + nameList[best]);
System.out.print("对应的分数是:" + point[best]);


5.9 找出两个高分学生

Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生个数:");
int count = scanner.nextInt();
String nameList[] = new String[count];
int point[] = new int[count];
int best = 0, best_array = 0;
int second = 0, second_array = 0;

Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
for (int i = 0; i < count; i++) {
    System.out.print("请输入第" + (i + 1) + "个学生的姓名:");
    nameList[i] = input1.nextLine();
    System.out.print("请输入第" + (i + 1) + "个学生的成绩:");
    point[i] = input2.nextInt();

    if (point[i] > best && point[i] > second) {
        second = best;
        best = point[i];
    } else if (point[i] < best && point[i] > second) {
        second = point[i];
    }
}
for (int i = 0; i < count; i++) {
    if (point[i] == best) {
        best_array = i;
    } else if (point[i] == second) {
        second_array = i;
    }
}
System.out.println("成绩最高的学生是:" + nameList[best_array] + ",对应的分数是:" + point[best_array]);
System.out.println("成绩第二的学生是:" + nameList[second_array] + ",对应的分数是:" + point[second_array]);


5.10 找出能被5和6整除的数

int count = 0;
for (int i = 100; i <= 1000; i++) {
    if (i % 5 == 0 && i % 6 == 0) {
        System.out.print(i + " ");
        count++;
        if (count % 10 == 0) {
            System.out.println();
        }
    }
}


5.11 找出能但不能同时被5和6整除的数

int count = 0;
for (int i = 100; i <= 200; i++) {
    if (i % 5 == 0 ^ i % 6 == 0) {
        System.out.print(i + " ");
        count++;
        if (count % 10 == 0) {
            System.out.println();
        }
    }
}


5.12 求最小值

int num = 0;
while (true) {
    if (Math.pow(num, 2) > 12000) {
        System.out.println("大于12000的n的二次方的最小整数是:" + num);
        break;
    }
    num++;
}


5.13 求最大值

int num = 0;
while (true) {
    if (Math.pow(num, 3) > 12000) {
        System.out.println("小于12000的n的三次方的最大整数是:" + (num - 1));
        break;
    }
    num++;
}


5.14 最大公约数

Scanner scanner = new Scanner(System.in);
System.out.print("请输入数字1:");
int num1 = scanner.nextInt();
System.out.print("请输入数字2:");
int num2 = scanner.nextInt();
int min = (num1 < num2) ? num1 : num2;

while (min > 0) {
    if (num1 % min == 0 && num2 % min == 0) {
        System.out.print(num1 + "和" + num2 + "的最大公约数是:" + min);
        break;
    }
    min--;
}


5.17 输出金字塔

Scanner scanner = new Scanner(System.in);
System.out.print("请输入数字:");
int num = scanner.nextInt();
for (int i = 1; i <= num; i++) {
    for (int j = num - i; j >= 1; j--) {
        System.out.print(" ");
    }
    for (int j = i; j >= 1; j--) {
        System.out.print(j);
    }
    for (int j = 2; j < i + 1; j++) {
        System.out.print(j);
    }
    System.out.println();
}


Java语言程序设计(基础篇)(原书第10版) 练习题答案 第5章