英文:
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 ("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') //heres the error
{
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 wasen't a myth!");
}
}
<!-- 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 == 'a') {
// ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论