那些年,我们一起做过的 Java 课后练习题(16-20)
2021/6/7 14:21:11
本文主要是介绍那些年,我们一起做过的 Java 课后练习题(16-20),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
实例 16
题目
输出 9 * 9 口诀。
分析
直接两层循环即可,要注意换行!
实现
/** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @website : https://cunyu1943.github.io * @date : 2021/6/3 13:03 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example16 * @description : */ public class Example16 { public static void main(String[] args) { for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + " * " + i + " = " + (i * j) + "\t\t"); } System.out.println(); } } }
结果
实例 17
题目
猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一般,还不过瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第 10 天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
分析
反向思考,从第十天开始,到第一天,每天吃桃子的个数是 ( sum+1 )* 2 其中 sum 是后一天的 (sum+1)* 2, 那么循环天数为九次,因为一到十,只经过了九天。
实现
/** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @website : https://cunyu1943.github.io * @date : 2021/6/3 13:07 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example17 * @description : */ public class Example17 { public static void main(String[] args) { int sum = 1; for (int i = 2; i <= 10; i++) { sum = (sum + 1) * 2; } System.out.println("第一天的桃子数:" + sum); } }
结果
实例 18
题目
两个乒乓球队进行比赛各出三人,甲队为 a、b、c 三人,乙队为 x、y、z 三人。已知 a 不和 x 比,c 不和 x、z 比,求比赛名单!
分析
分别将 a、b、c 和 x、y、z 进行配对,然后除开不符合题意的组合,最后得出的结果就是比赛名单。主要还是利用 for
循环和 if
条件判断来实现!
实现
/** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @website : https://cunyu1943.github.io * @date : 2021/6/3 13:08 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example18 * @description : */ public class Example18 { static char[] teamA = {'a', 'b', 'c'}; static char[] teamB = {'x', 'y', 'z'}; public static void main(String[] args) { int size = teamA.length; System.out.println("对战名单如下:"); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (teamA[i] == 'a' && teamB[j] == 'x') { continue; } else if (teamA[i] == 'a' && teamB[j] == 'y') { continue; } else if ((teamA[i] == 'c' && teamB[j] == 'x') || (teamA[i] == 'c' && teamB[j] == 'z')) { continue; } else if (((teamA[i] == 'b' && teamB[j] == 'z') || (teamA[i] == 'b' && teamB[j] == 'y'))) { continue; } else { System.out.println(teamA[i] + " VS " + teamB[j]); } } } } }
结果
实例 19
题目
实现打印输出一个菱形。
* *** ***** ******* ********* ******* ***** *** *
分析
将菱形分为上下部分,然后穿插打印空格和 *
即可。
实现
import java.util.Scanner; /** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @website : https://cunyu1943.github.io * @date : 2021/6/3 13:30 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example19 * @description : */ public class Example19 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入需要打印的行数"); int row = scanner.nextInt(); if (row % 2 == 0) { // 计算菱形大小,将其分为上下部分 row++; } for (int i = 0; i < row / 2 + 1; i++) { for (int j = row / 2 + 1; j > i + 1; j--) { // 输出左上角位置的空白 System.out.print(" "); } for (int j = 0; j < 2 * i + 1; j++) { // 输出菱形上半部边缘 System.out.print("*"); } System.out.println(); // 换行 } for (int i = row / 2 + 1; i < row; i++) { for (int j = 0; j < i - row / 2; j++) { // 输出菱形左下角空白 System.out.print(" "); } for (int j = 0; j < 2 * row - 1 - 2 * i; j++) { // 输出菱形下半部边缘 System.out.print("*"); } // 换行 System.out.println(); } } }
结果
实例 20
题目
有一个分数序列:2/1、3/2、5/3、8/5、……,求出该数列的前 20 项之和。
分析
观察序列可知,从第二项开始,当前分数的分子就等于上一个分数的分子分母之和,分母就等于上一个分数的分子,根据此规律,对分数序列进行求和即可!
实现
/** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @website : https://cunyu1943.github.io * @date : 2021/6/3 13:30 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example20 * @description : */ public class Example20 { public static void main(String[] args) { double sum = 0.0d; // 分母 int denominator = 1; // 分子 int numerator = 2; for (int i = 1; i <= 20; i++) { sum = sum + (double) numerator / denominator; int tmp = denominator; denominator = numerator; numerator = tmp + denominator; System.out.println("前 " + i + " 项之和:" + sum); } } }
结果
这篇关于那些年,我们一起做过的 Java 课后练习题(16-20)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南