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 % ;
       // 该语句不够精炼,有冗余,应该写成 Integer tensDigit=i/10;即可
Integer tensDigit = (i / ) % ;
Integer hundredsDigit = (i / ) % ;
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 / ;
Integer tensDigit = (i - hundredsDigit * ) / ;
Integer unitDigit = i - hundredsDigit * - tensDigit * ;
if (i == (unitDigit * unitDigit * unitDigit + tensDigit * tensDigit
* tensDigit + hundredsDigit * hundredsDigit * hundredsDigit)) {
resultList.add(i);
}
}
return resultList;
}

二、乘法口诀

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

代码如下:

 public static String generateMultiplication(Integer startNum, Integer endNum) {
String result = "";
for (int i = startNum; i <= endNum; i++) {
for (int j = startNum; j <= i; j++) {
result += j + "*" + i + "=" + i * j + " ";
}
result += "\n";
}
return result;
}
上一篇:Git安装以及常用命令(图文详解)


下一篇:HTTP POST GET 本质区别详解