I'm coding part of an RPG game and there's an error saying "incompatible types: char cannot be converted into boolean" (java, bluej)

huangapple 未分类评论72阅读模式
英文:

I'm coding part of an RPG game and there's an error saying "incompatible types: char cannot be converted into boolean" (java, bluej)

问题

  1. public class test {
  2. public static void main(String args[]) {
  3. new test();
  4. }
  5. public test() {
  6. System.out.println("You take the notebook with you and walk to the nearest");
  7. System.out.print("comic book store where you see a young girl being robbed by a");
  8. System.out.print("group of men. The man who appears to be the leader of the gang speaks.");
  9. System.out.print("\"My name is Takuo, Shibui-Maru, that’s Shibutaku for short. Heh heh.\"");
  10. System.out.print("You decide to write his name and see what happens.");
  11. System.out.print("Do you choose to kill him by:");
  12. int points = 50;
  13. System.out.println(" ");
  14. char kill = IBIO.inputChar("a)heart attack or b)fire: ");
  15. if (kill == 'a') // here's the corrected comparison
  16. {
  17. points += 10;
  18. System.out.println(" Within minutes, the man falls to the ground and the girl runs away. The death note works! " + points);
  19. } else {
  20. points -= 20;
  21. System.out.println(" All of a sudden, flames engulf the comic book store.");
  22. System.out.println(" You attempt to leave the store as quickly as possible but have trouble seeing.");
  23. System.out.println(" You return safely with the notebook in your hands, but an innocent old man fails to come outside in time. " + points);
  24. }
  25. System.out.println("So it wasn't a myth!");
  26. }
  27. }
英文:

I'm pretty new to this coding stuff and I have an assignment due soon, where we have to code an RPG game. I'm working on a chunk of my Death-Note based game where Kira is choosing what way to kill using If statements. For some reason it says "incompatible types: char cannot be converted into boolean" and I have no clue why.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. public class test
  2. {
  3. public static void main (String args[])
  4. {
  5. new test();
  6. }
  7. public test ()
  8. {
  9. System.out.println (&quot;You take the notebook with you and walk to the nearest&quot;);
  10. System.out.print (&quot; comic book store where you see a young girl being robbed by a&quot;);
  11. System.out.print (&quot; group of men. The man who appears to be the leader of the gang speaks.&quot;);
  12. System.out.print (&quot; \&quot;My name is Takuo, Shibui-Maru, thats Shibutaku for short. Heh heh.\&quot;&quot;);
  13. System.out.print (&quot;You decide to write his name and see what happens.&quot;);
  14. System.out.print (&quot;Do you choose to kill him by:&quot;);
  15. int points = 50;
  16. System.out.println (&quot; &quot;);
  17. char kill = IBIO.inputChar (&quot;a)heart attack or b)fire: &quot;);
  18. if (kill = &#39;a&#39;) //heres the error
  19. {
  20. points = +10;
  21. System.out.println (&quot; Within minutes, the man falls to the ground and the girl runs away. The death note works! +points+ &quot;);
  22. }
  23. else
  24. {
  25. points = -20;
  26. System.out.println (&quot; All of a sudden, flames engulf the comic book store.&quot;);
  27. System.out.println (&quot; You attempt to leave the store as quickly as possible but have trouble seeing.&quot;);
  28. System.out.println (&quot; You return safely with the notebook in your hands, but an innocent old man fails to come outside in time. +points&quot;);
  29. }
  30. System.out.println (&quot;So it wasen&#39;t a myth!&quot;);
  31. }
  32. }

<!-- end snippet -->

答案1

得分: 3

因为您在您的 if 语句中使用了单个等号,所以出现了这个问题。单个等号用于赋值,您应该使用双等号运算符 (==) 来比较两个值。编译器提示您的 if 语句期望一个 boolean 值(或者一个求值结果为 boolean 的表达式),但您传递的是一个赋值而不是一个比较。

请尝试使用以下代码替代:

  1. if (kill == 'a') {
  2. // ...
  3. }
英文:

This happens because you're using a single equal sign in your if. This is used for assignment, you're looking for the double equal operator (==) which compares two values.

The compiler is telling you that the if is expecting a boolean (or an expression which resolves to a boolean) but you're passing in an assignment instead of a comparison.

Try this instead:

  1. if (kill == &#39;a&#39;) {
  2. // ...
  3. }

huangapple
  • 本文由 发表于 2020年3月15日 08:57:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/60688775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定