心血来潮补一下之前没做完的课后题,争取全部做完,欢迎大家评论指正。
目录
-
- 5-1 统计正数和负数的个教然后计算这些数的平均值
- 5-2 重复加法
- 5-3 将千克转换成磅
- 5-4 将英里转换成千米
- 5-5 千克与磅之间的互换
- 5-6 英里与千米之间的互换
- 5-7 财务应用程序:计算将来的学费
- 5-8 找出最高分
- 5-9 找出两个分教最高的学生
- 5-10 找出能被5和6整除的数
- 5-11 找出能被5或6整除,但不能被两者同时整除的数
- 5-12 求满足n^2^>12000的n的最小值
- 5-13 求满足n^3^<12000的n的最大值
- 5-14 计算最大公约数
- 5-15 显示ACSII码字符表
- 5-16 找出一个整数的因子
- 5-17 显示金字塔
- 5-18 使用循环语句打印4个图案
- 5-19 打印金字塔形的数字
- 5-20 打印2到1000之间的素数
- 5-21 财务应用程序:比较不同利率下的贷款
- 5-22 财务应用程序:显示分期还贷时间表
- 5-23 示例抵消错误
- 5-24 数列求和
- 5-25 计算π
- 5-26 计算e
- 5-27 显示闰年
- 5-28 显示每月第一天是星期几
- 5-29 显示日历
- 5-30 财务应用程序:复利值
- 5-31 财务应用程序:计算CD价值
- 5-32 游戏:彩票
- 5-33 完全数
- 5-34 游戏:石头、剪刀、布
- 5-35 加法
- 5-36 商业应用程序:检测ISBN
- 5-37 十进制到二进制
- 5-38 十进制到八进制
- 5-39 财务应用程序:求出销售总额
- 5-40 模拟:正面或反面
- 5-41 最大数的出现次数
- 5-42 财务应用程序:求出销售额
- 5-43 数学方面:组合
- 5-44 计算机体系结构:比特级的操作
- 5-45 统计:计算平均值和标准方差
- 5-46 倒排一个字符串
- 5-47 商业:检测ISBN-13
- 5-48 处理字符串
- 5-49 对元音和辅音进行计数
- 5-50 对大写字母计数
- 5-51 最长的共同前缀
需要书籍或者相关资料可以私聊!!!
5-1 统计正数和负数的个教然后计算这些数的平均值
import java.util.Scanner;
public class Program5_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer, the input ends if it is 0: ");
int a = input.nextInt();
int sum = 0,count1 = 0,count2 = 0;
double average;
while(a != 0) {
sum += a;
if(a > 0)
count1++;
else
count2++;
a = input.nextInt();
}
average = sum * 1.0 / (count1 + count2);
System.out.println("The number of positives is " + count1);
System.out.println("The number of negatives is " + count2);
System.out.println("The total is " + sum);
System.out.println("The average is " + average);
input.close();
}
}
5-2 重复加法
import java.util.Scanner;
public class Program5_2 {
public static void main(String[] args) {
final int NUMBER_OF_QUESTIONS = 10; // Number of questions
int correctCount = 0; // Count the number of correct answers
int count = 0; // Count the number of questions
long startTime = System.currentTimeMillis();
String output = ""; // output string is initially empty
Scanner input = new Scanner(System.in);
while (count < NUMBER_OF_QUESTIONS) {
int number1 = (int)(Math.random() * 15) + 1;
int number2 = (int)(Math.random() * 15) + 1;
System.out.print(
"What is " + number1 + " + " + number2 + "? ");
int answer = input.nextInt();
if (number1 + number2 == answer) {
System.out.println("You are correct!");
correctCount++;
}
else
System.out.println("Your answer is wrong.\n" + number1
+ " + " + number2 + " should be " + (number1 + number2));
// Increase the count
count++;
output += "\n" + number1 + "+" + number2 + "=" + answer +
((number1 + number2 == answer) ? " correct" : " wrong");
}
long endTime = System.currentTimeMillis();
long testTime = endTime - startTime;
System.out.println("Correct count is " + correctCount +
"\nTest time is " + testTime / 1000 + " seconds\n" + output);
input.close();
}
}
5-3 将千克转换成磅
public class Program5_3 {
public static void main(String[] args) {
System.out.printf("%-8s%5s\n", "千克", "磅");
for (int i = 1; i <= 199; i += 2)
System.out.printf("%-10d%5.1f\n", i, i*2.2);
}
}
5-4 将英里转换成千米
public class Program5_4 {
public static void main(String[] args) {
System.out.printf("%-8s%5s\n", "英里", "千米");
for (int i = 1; i <= 10; i++)
System.out.printf("%-10d%7.3f\n", i, i*1.609);
}
}
5-5 千克与磅之间的互换
public class Program5_5 {
public static void main(String[] args) {
System.out.printf("%-8s%5s %-8s%5s\n", "千克", "磅", "磅", "千克");
for (int i = 1, j = 20; i <= 199; i += 2, j += 5) {
System.out.printf("%-10d%5.1f ", i, i * 2.2);
System.out.printf("%-10d%5.2f\n", j, j / 2.2);
}
}
}
5-6 英里与千米之间的互换
public class Program5_6 {
public static void main(String[] args) {
System.out.printf("%-8s%5s %-8s%5s\n", "英里", "千米", "千米", "英里");
for (int i = 1, j = 20; i <= 10; i++, j += 5) {
System.out.printf("%-10d%7.3f ", i, i * 1.609);
System.out.printf("%-10d%7.3f\n", j, j / 1.609);
}
}
}
5-7 财务应用程序:计算将来的学费
public class Program5_7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double m = 10000;
for(int i = 1;i <= 10;i++)
m *= 1.05;
System.out.println("The tuition after 10 years is " + ((int)(m * 100) / 100.00));
double sum = m;
for(int i = 1;i <= 3;i++){
m *= 1.05;
sum += m;
}
System.out.println("The tuition for 4 years after 10 years is " + ((int)(sum * 100) / 100.00));
}
}
5-8 找出最高分
import java.util.Scanner;
public class Program5_8 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int n = input.nextInt();
int maxScore = 0;
String maxName = null;
for (int i = 1; i <= n; i++) {
System.out.print(i + " name and score: ");
String name = input.next();
int score = input.nextInt();
if(maxScore < score){
maxScore = score;
maxName = name;
}
}
System.out.println("The highest score is " + maxName + " : " + maxScore);
input.close();
}
}
5-9 找出两个分教最高的学生
import java.util.Scanner;
public class Program5_9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int n = input.nextInt();
System.out.print("1 name and score: ");
String Name1 = input.next();
int score1 = input.nextInt();
System.out.print("2 name and score: ");
String Name2 = input.next();
int score2 = input.nextInt();
int max1Score,max2Score;
String max1Name,max2Name;
if(score1 >= score2) {
max1Score = score1;
max1Name = Name1;
max2Score = score2;
max2Name = Name2;
}
else {
max1Score = score2;
max1Name = Name2;
max2Score = score1;
max2Name = Name1;
}
for(int i = 3;i <= n;i++) {
System.out.print(i + " name and score: ");
Name1 = input.next();
score1 = input.nextInt();
if(score1 >= max1Score) {
max2Score = max1Score;
max2Name = max1Name;
max1Score = score1;
max1Name = Name1;
}
else if(score1 >= max2Score) {
max2Score = score1;
max2Name = Name1;
}
}
System.out.println("The highest score is " + max1Name + " : " + max1Score);
System.out.println("The second highest score is " + max2Name + " : " + max2Score);
input.close();
}
}
5-10 找出能被5和6整除的数
public class Program5_10 {
public static void main(String[] args) {
final int numberPerLine = 10;
int cnt = 0;
for (int i = 100; i <= 1000; i++) {
if (i % 5 == 0 && i % 6 == 0) {
cnt++;
System.out.printf("%-6d", i);
if(cnt % 10 == 0)
System.out.println();
}
}
}
}
5-11 找出能被5或6整除,但不能被两者同时整除的数
public class Program5_11 {
public static void main(String[] args) {
final int NUMBER_PER_LINE = 10;
int cnt = 0;
for (int i = 100; i <= 200; i++) {
if (i % 5 == 0 ^ i % 6 == 0) {
cnt++;
System.out.printf("%-6d", i);
if(cnt % NUMBER_PER_LINE == 0)
System.out.println();
}
}
}
}
5-12 求满足n2>12000的n的最小值
public class Program5_12 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 1;
while(n*n <= 12000)
n++;
System.out.println(n);
}
}
5-13 求满足n3<12000的n的最大值
public class Program5_13 {
public static void main(String[] args) {
int n = 1;
while(n*n*n < 12000)
n++;
System.out.println(n-1);
}
}
5-14 计算最大公约数
import java.util.Scanner;
public class Program5_14 {
public static void main(String