- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试试运行书中的英文标尺方法,但是试运行有点困惑,而且我的输出和原始输出不同。
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 和majorlength 3
,
1-drawLine 将使用 (3,0)//刻度长度和刻度标签调用
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
?
最佳答案
看来您已经从书中获取了代码Data Structures and Algorithms in Java .
但是你的代码甚至无法编译,有很多语法错误和变量用词不当。您是否通过扫描 + OCR 从书中获取了源代码,而没有尝试使用 Java 运行它?
所以首先让我们修复错误,并将这段零散的代码变成一个带有 main 方法的类,好吗?
package de.scrum_master.stackoverflow;
public class EnglishRuler {
public static void main(String[] args) {
drawRuler(2, 4);
}
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(centralLength - 1);
}
}
private static void drawLine(int tickLength, int tickLabel) {
for (int j = 0; j < tickLength; j++)
System.out.print("-");
if (tickLabel >= 0)
System.out.print(" " + tickLabel);
System.out.print("\n");
}
private static void drawLine(int tickLength) {
drawLine(tickLength, -1);
}
}
drawRuler(1, 3)
打印:
--- 0
-
--
-
--- 1
drawRuler(1, 5)
打印:
----- 0
-
--
-
---
-
--
-
----
-
--
-
---
-
--
-
----- 1
drawRuler(2, 4)
打印:
---- 0
-
--
-
---
-
--
-
---- 1
-
--
-
---
-
--
-
---- 2
这一切都在预料之中。现在让我们向程序添加一些可选的调试输出:
package de.scrum_master.stackoverflow;
public class EnglishRuler {
private static boolean DEBUG = true;
private static String indent = "";
public static void main(String[] args) {
drawRuler(1, 3);
}
public static void drawRuler(int nInches, int majorLength) {
if (DEBUG)
System.out.println("drawRuler(" + nInches + ", " + majorLength + ")");
drawLine(majorLength, 0);
for (int j = 1; j <= nInches; j++) {
drawInterval(majorLength - 1);
drawLine(majorLength, j);
}
}
private static void drawInterval(int centralLength) {
indent += " ";
if (DEBUG)
System.out.println(indent + "drawInterval(" + centralLength + ")");
if (centralLength >= 1) {
drawInterval(centralLength - 1);
drawLine(centralLength);
drawInterval(centralLength - 1);
}
indent = indent.substring(2);
}
private static void drawLine(int tickLength, int tickLabel) {
indent += " ";
if (DEBUG)
System.out.println(indent + "drawLine(" + tickLength + ", " + tickLabel + ")");
for (int j = 0; j < tickLength; j++)
System.out.print("-");
if (tickLabel >= 0)
System.out.print(" " + tickLabel);
System.out.print("\n");
indent = indent.substring(2);
}
private static void drawLine(int tickLength) {
drawLine(tickLength, -1);
}
}
只要 DEBUG
为 false
,就不会更改输出。如果将其设置为 true
,则 drawRuler(1, 3)
的日志将变为:
drawRuler(1, 3)
drawLine(3, 0)
--- 0
drawInterval(2)
drawInterval(1)
drawInterval(0)
drawLine(1, -1)
-
drawInterval(0)
drawLine(2, -1)
--
drawInterval(1)
drawInterval(0)
drawLine(1, -1)
-
drawInterval(0)
drawLine(3, 1)
--- 1
这里有一个自动生成的试运行版本。
至于你的问题:
In the first go, I am entering
nInches = 1
andmajorlength = 3
,1)
drawLine
will be called with(3,0)
(tickLength
andtickLabel
)
正确。
Point1: Mean above line will call
drawInterval(2)
?
正确。
Point3:
drawLine(tickLength, -1)
. Why we use this-1
?
因为在drawLine(int tickLength, int tickLabel)
中它说:
if (tickLabel >= 0)
System.out.print(" " + tickLabel);
因此,使 tickLabel
的值小于零只是当我们不在主间隔而是在其间较小的子间隔时避免打印标签的一种方法。
更新:我还根据递归级别向具有调试输出的程序版本添加了缩进,并且还更新了日志输出以缩进以便OP更好地理解。
<小时/>更新 2:您可以通过内联便捷方法 drawLine(int tickLength)
来简化程序,如下所示:
private static void drawInterval(int centralLength) {
// ...
drawInterval(centralLength - 1);
drawLine(centralLength, -1); // Note the additional ", -1"
drawInterval(centralLength - 1);
// ...
}
然后删除这个,因为现在它不再使用:
// Delete me!
private static void drawLine(int tickLength) {
drawLine(tickLength, -1);
}
<小时/>
更新3:因为你似乎很恼火,因为我没有为便捷方法drawLine(int tickLength)
打印日志输出,所以这里是另一个扩展版本原始程序也为该方法生成输出,现在完全复制您的笔和纸试运行:
package de.scrum_master.stackoverflow;
public class EnglishRuler {
private static boolean DEBUG = true;
private static String indentText = "";
public static void main(String[] args) {
drawRuler(1, 3);
}
public static void drawRuler(int nInches, int majorLength) {
debugPrint("drawRuler(" + nInches + ", " + majorLength + ")");
drawLine(majorLength, 0);
for (int j = 1; j <= nInches; j++) {
drawInterval(majorLength - 1);
drawLine(majorLength, j);
}
}
private static void drawInterval(int centralLength) {
indent();
debugPrint("drawInterval(" + centralLength + ")");
if (centralLength >= 1) {
drawInterval(centralLength - 1);
drawLine(centralLength);
drawInterval(centralLength - 1);
}
dedent();
}
private static void drawLine(int tickLength, int tickLabel) {
indent();
debugPrint("drawLine(" + tickLength + ", " + tickLabel + ")");
for (int j = 0; j < tickLength; j++)
System.out.print("-");
if (tickLabel >= 0)
System.out.print(" " + tickLabel);
System.out.print("\n");
dedent();
}
private static void drawLine(int tickLength) {
indent();
debugPrint("drawLine(" + tickLength + ")");
drawLine(tickLength, -1);
dedent();
}
private static void debugPrint(String message) {
if (DEBUG)
System.out.println(indentText + message);
}
private static void indent() {
indentText += " ";
}
private static void dedent() {
indentText = indentText.substring(2);
}
}
更新后的控制台日志变为:
drawRuler(1, 3)
drawLine(3, 0)
--- 0
drawInterval(2)
drawInterval(1)
drawInterval(0)
drawLine(1)
drawLine(1, -1)
-
drawInterval(0)
drawLine(2)
drawLine(2, -1)
--
drawInterval(1)
drawInterval(0)
drawLine(1)
drawLine(1, -1)
-
drawInterval(0)
drawLine(3, 1)
--- 1
我发现这是不必要的,但如果它对你有帮助,我很高兴。
关于java - 英文标尺递归问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58121986/
我对 YAML 数据 block 有疑问。给定以下 pandoc 文档: --- papersize: a4 geometry: "left=1.5cm,right=1.5cm,top=3cm,bot
我对 YAML 数据 block 有疑问。给定以下 pandoc 文档: --- papersize: a4 geometry: "left=1.5cm,right=1.5cm,top=3cm,bot
在 Xcode 中,界面构建器是否具有某种可自定义的标尺(如 Photoshop 中的标尺)来帮助我正确定位元素? 我知道它会出现那些红线,但我的意思是你可以定位自己的线。 谢谢,丹。 最佳答案 我找
我有疑问。我正在编写像 this 这样的编辑器,我还想要一个标尺来添加它(它通过鼠标滚动(放大和缩小)更改值) 可是我做不到。我在 github 上尝试了所有标尺,但它们只运行 body 标签。 很遗
我更新了 chrome,当我打开 devtools 时,我看到了这个。 如何去掉标尺(上部和右侧的黑色部分)?它需要很大的空间,而且我有一个 13.3 英寸的小屏幕。 最佳答案 F12;左上角望远镜图
我是一名优秀的程序员,十分优秀!