【问题标题】:Type mismatch: cannot convert from int to boolean in while loop类型不匹配:无法在 while 循环中从 int 转换为 boolean
【发布时间】:2014-04-07 21:09:26
【问题描述】:

我刚开始学习java,试图找到答案,但我想我不够聪明。 我正在尝试将从某个文件中获取的数组写入另一个文件。

问题是当我试图让“while”时,它说不能从 int 转换为 boolean。任何人都可以提出一些建议。先感谢您。 这就是我所拥有的:

public void savethefile() throws IOException{

File file1= new File("lala.ppm");
FileWriter save=new FileWriter(file1);
save.write(tytul);

while(i){
    save.write(arraycomment[i]);
    i++;
}

save.close();

【问题讨论】:

  • 顺便说一句:tytularraycomment 是什么?
  • while 中使用布尔值而不是 int。该错误确实说明了一切。如果您来自 C,这可能会令人困惑,但在 java 中有布尔值,它们不是整数。

标签: java boolean


【解决方案1】:

大概iint,而不是boolean。与 C/C++ 不同,它不会隐式转换为 boolean。您必须将您的条件明确声明为boolean

while (i < arraycomment.length) // or some other constant

【讨论】:

  • 是的,我开始在 C++ 和 Java 中看到这些不同。非常感谢!
  • 在发布问题 15 分钟后,您可以通过单击您认为最有帮助的答案旁边的复选标记来“接受”答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
  • 2021-05-31
  • 2013-03-09
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多