【问题标题】:PrintWriter does not print inside ifPrintWriter 不会在内部打印 if
【发布时间】:2013-01-08 08:24:51
【问题描述】:

我在处理这个PrintWriter 时遇到了一些问题,它在for 循环之外工作,它确实打印了“Test1”和“Test2”,但它没有在If 内打印“example 1”

这行代码在我用调试器检查时被执行

 File file = new File ("output.txt");
 FileWriter wri;
 try {
    wri = new FileWriter(file);

    PrintWriter writer = new PrintWriter(wri);
    writer.println("Test1");

    boolean result=false;

    for(i=0;i<this.lung;i++){

       for(j=0;j<this.lung;j++){

            if(wordToFind.charAt(0)==mat[i][j]){

                if((i>=wordLength-1)&&(results!=true))
                    if (wordToFind.charAt(1)==mat[i-1][j])
                        if(result=continue(i,j,i-1,j,1))
                            writer.println("example 1");
                  /*Same if as the above but with differente parameters are down here*/
                  }
             } 
         }
      }
      writer.print("Test2");
      writer.close();

  } catch (IOException e) {
      System.out.println("Error: " + e);
 }

【问题讨论】:

  • 您是如何检查该行是否实际执行的?
  • 你会因为使用continue作为方法名而编译失败!
  • 我已将单词更改为“继续”,以便将其发布在 StackOverflow 上,因为原始代码是另一种“语言”,所以应该不是问题
  • 在这种情况下,显示 continue(...) 方法是合适的
  • 该方法有效,因为如果我尝试使用 System.out.println 输出它,它会在控制台中打印它。

标签: java file output


【解决方案1】:

这两个 results / result 是同一个变量吗,如果是,则尝试将第一个更改为 result;

// 假设'因为我没有在你的代码中找到任何 results boolean 变量

if((i>=wordLength-1)&&(results!=true))  // results -> result
                    if (wordToFind.charAt(1)==mat[i-1][j])
                        if(result=continue(i,j,i-1,j,1)) 
                            writer.println("example 1");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多