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 go评论164阅读模式
英文:

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

问题

public class test {
    public static void main(String args[]) {
        new test();
    }

    public test() {
        System.out.println("You take the notebook with you and walk to the nearest");
        System.out.print("comic book store where you see a young girl being robbed by a");
        System.out.print("group of men. The man who appears to be the leader of the gang speaks.");
        System.out.print("\"My name is Takuo, Shibui-Maru, that’s Shibutaku for short. Heh heh.\"");
        System.out.print("You decide to write his name and see what happens.");
        System.out.print("Do you choose to kill him by:");

        int points = 50;
        System.out.println(" ");
        char kill = IBIO.inputChar("a)heart attack or b)fire: ");
        if (kill == 'a') // here's the corrected comparison
        {
            points += 10;
            System.out.println(" Within minutes, the man falls to the ground and the girl runs away. The death note works! " + points);
        } else {
            points -= 20;
            System.out.println(" All of a sudden, flames engulf the comic book store.");
            System.out.println(" You attempt to leave the store as quickly as possible but have trouble seeing.");
            System.out.println(" You return safely with the notebook in your hands, but an innocent old man fails to come outside in time. " + points);
        }
        System.out.println("So it wasn't a myth!");
    }
}
英文:

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 -->

public class test
{
    public static void main (String args[])
    {
        new test();
    }
    
    public test ()
    {
    System.out.println (&quot;You take the notebook with you and walk to the nearest&quot;);
    System.out.print (&quot; comic book store where you see a young girl being robbed by a&quot;); 
    System.out.print (&quot; group of men. The man who appears to be the leader of the gang speaks.&quot;); 
    System.out.print (&quot; \&quot;My name is Takuo, Shibui-Maru, that’s Shibutaku for short. Heh heh.\&quot;&quot;);
    System.out.print (&quot;You decide to write his name and see what happens.&quot;);
    System.out.print (&quot;Do you choose to kill him by:&quot;);
    
    int points = 50;
    System.out.println (&quot; &quot;);
    char kill = IBIO.inputChar (&quot;a)heart attack or b)fire: &quot;);
    if (kill = &#39;a&#39;) //heres the error
    {
    points = +10;
    System.out.println (&quot; Within minutes, the man falls to the ground and the girl runs away. The death note works! +points+ &quot;);
}
    else
    {
    points = -20;
    System.out.println (&quot; All of a sudden, flames engulf the comic book store.&quot;); 
    System.out.println (&quot; You attempt to leave the store as quickly as possible but have trouble seeing.&quot;);
    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;);
}
    System.out.println (&quot;So it wasen&#39;t a myth!&quot;);
}
}

<!-- end snippet -->

答案1

得分: 3

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

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

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

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:

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

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

发表评论

匿名网友

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

确定