我正在尝试试运行书中的英语标尺方法,但是试运行有点令人困惑,而且我的输出和原始输出不同。
public static void drawRuler(int nInches, int majorlength)
{
drawLine(majorLength, 0);
for (int j =1; j<= nInches, j++)
{
drawInterval(majorLength-1);
drawLine(majorLength, j);
}
}
private static void drawInterval (int centralLength)
{
if (centralLength>=1)
{
drawInterval(centralLength - 1);
drawLine(centralLength);
drawInterval(centrakLength-1);
}
}
public static void drawLine(int tickLength, int tickLabel)
{
for (int j=0; j<ticklength; j++)
System.out.print("-")
if (tickLabel>=0)
System.out.print(" "+tickLable);
System.out.print("\n");
}
private static void drawLine(int tickLength)
{
drawLine(tickLength, -1);
}
第一次,我进入nInches = 1 and majorlength 3,
1- 将使用 (3,0) // 刻度长度和刻度标签调用drawLine
public static void drawLine(3, 0)
{
for (int j=0; j<3; j++)
System.out.print("-")
if (tickLabel>=0)
System.out.print(" "+tickLable);
System.out.print("\n");
}
输出:
--- 0
2-现在下面的循环将从drawRuler函数运行
for (int j =1; j<=1, j++)
{
drawInterval(majorLength-1);
* Point1: 意味着上面的线会调用drawInterval(2) ?*
drawLine(majorLength, j);
}
3-我们转到以 2 作为参数的函数drawInterval
private static void drawInterval (2)
{
if (centralLength>=1) // true
{
drawInterval(centralLength - 1);
**Point 2: what the point of calling same function with 1 ? and will it call itself again without drawing anything? and function will go on nextline after drawInterval become 0?**
drawLine(centralLength);
drawInterval(centrakLength-1);
}
}
Point3:drawLine(tickLength, -1);为什么我们用这个-1?
幕布斯7119047
相关分类