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

本文涵盖了Java编程基础的50道练习题,包括数学计算、转换、财务应用、游戏逻辑等多个方面,旨在提升编程技能。从统计正负数、ASCII码表到复利计算、彩票游戏等,每道题目都详细解析,适合初学者巩固基础。
摘要由CSDN通过智能技术生成

心血来潮补一下之前没做完的课后题,争取全部做完,欢迎大家评论指正。

目录

在这里插入图片描述

需要书籍或者相关资料可以私聊!!!

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
  • 7
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值