java - 控制台没有输出任何内容! (Java、Eclipse 火星)

标签 java eclipse console eclipse-mars

所以基本上我正在尝试编写一个路径查找程序,该程序可以找到从 10*10 网格中的某个点到另一个点的路径,这很好。

我有一个类Path,它是一个GridSquare的ArrayList(它们只是美化的坐标)。

我在 Path 类中编写了一个小方法来显示路径,这就是出现问题的地方,这个问题虽小但非常令人恼火。

当我尝试运行代码并调用 displayPath 时,没有任何内容 输出到控制台,并且程序终止且没有错误。

这是 displayPath 的代码:

public void displayPath(){
    System.out.println("This is displayPrint"); //This line is included to make sure the program calls the method correctly.
    for(int i=1; i==10; i++){
        for(int j=1; j==10; j++){
            if(this.includesSquare(i, j)){
                System.out.print("[x]");
            } else {
                System.out.print("[ ]");
            }
        }
        System.out.print("\n");
    }
}

我添加了第一行,以确保 console/System.out.print() 正常工作,并且每次调用该方法时都会显示该行。

这是includesSquare的代码:

public boolean includesSquare(int x, int y){
    for(GridSquare square : this.path){
        if(square.getX()==x && square.getY()==y){
            return true;
        }
    }
    return false;
}

我已经卸载并重新安装了 Eclipse,将 java 文件复制到一个新项目等中,似乎没有任何区别。我知道控制台工作正常,因为它正确显示了 displayPath 的第一行。

非常感谢任何帮助!

最佳答案

for(int i=1; i==10; i++)for(int j=1; j==10; j++)不会工作。

中间条件( i==10 )应该说明何时应该执行循环。事实上,您是说您只想在 i 时执行循环。等于 10。自 i最初等于 1,它将直接跳过循环。

您可能想要的是

for(int i=1; i<10; i++)

这样,当 i等于1,满足小于10的条件,因此循环将被执行,i会增加。这将持续发生,直到 i等于 10,此时条件 i<10失败,因此循环将退出。

简而言之,您希望条件为“循环这是真的”,而不是“循环直到这是真的”。

关于java - 控制台没有输出任何内容! (Java、Eclipse 火星),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32895764/

相关文章:

java - 如何找到一组的长度?

没有协议(protocol)的 Java URL

eclipse - Eclipse Java 编辑器中的 "Close all tabs to the right"选项

java - Hibernate 按属性获取记录不起作用

java - 末尾带有转义右括号的正则表达式

python - Eclipse 模板中的条件语句

java - 在 OS X 10.8 下的 Eclipse (4.3 Kepler) 中安装 GWT 插件并正常工作时出现问题

windows - Windows 上 Haskell 的彩色外壳?

scala - 在 Scala 控制台中使用颜色

c++ - 如何在C/C++中输出unicode字符