本文已同步至:村雨遥

实例 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);        }    }}

后果