• java1234教程系列笔记 S1 Java SE chapter 02 写乘法口诀表


    一、水仙花数

    1、方式一:这是我的思路,取各个位数的方式。我个人习惯于使用取模运算。

    public static List<Integer> dealNarcissiticNumberMethodOne(
                Integer startNum, Integer endNum) {
            List<Integer> resultList = new LinkedList<Integer>();
            for (Integer i = startNum; i <= endNum; i++) {
                Integer unitDigit = i % 10;
           // 该语句不够精炼,有冗余,应该写成 Integer tensDigit=i/10;即可 Integer tensDigit
    = (i / 10) % 10; Integer hundredsDigit = (i / 100) % 10; if (i == (unitDigit * unitDigit * unitDigit + tensDigit * tensDigit * tensDigit + hundredsDigit * hundredsDigit * hundredsDigit)) { resultList.add(i); } } return resultList; }

    2、方式二:视频的主讲人的方式如下:跟小学学习计数的方式一样。此时显示出数学理论的重要。

    public static List<Integer> dealNarcissiticNumberMethodTwo(
                Integer startNum, Integer endNum) {
            List<Integer> resultList = new LinkedList<Integer>();
            for (Integer i = startNum; i <= endNum; i++) {
                Integer hundredsDigit = i / 100;
                Integer tensDigit = (i - hundredsDigit * 100) / 10;
                Integer unitDigit = i - hundredsDigit * 100 - tensDigit * 10;
                if (i == (unitDigit * unitDigit * unitDigit + tensDigit * tensDigit
                        * tensDigit + hundredsDigit * hundredsDigit * hundredsDigit)) {
                    resultList.add(i);
                }
            }
            return resultList;
        }

    二、乘法口诀

    一段代码,调试了四次。需要运行看结果才倒推代码的缺陷。理想情况,应当是,先把逻辑梳理清楚。再梳理。切记

    代码如下:

     1 public static String generateMultiplication(Integer startNum, Integer endNum) {
     2         String result = "";
     3         for (int i = startNum; i <= endNum; i++) {
     4             for (int j = startNum; j <= i; j++) {
     5                 result += j + "*" + i + "=" + i * j + " ";
     6             }
     7             result += "
    ";
     8         }
     9         return result;
    10     }
  • 相关阅读:
    Win10下 Docker Flask实例
    4.1 线性映射的概念
    桥梁的基本组成和分类
    Qt5字符串编码转换学习
    在右键菜单中添加用Jupyter Notebook打开
    左右手(直角)坐标系叉乘计算公式
    __new__方法与单键实例
    向量组的秩
    从线性组合的角度理解三维运算
    Hexo使用小结
  • 原文地址:https://www.cnblogs.com/siesteven/p/6235362.html
Copyright © 2020-2023  润新知