我正在编写此代码,您可以为程序命名。
Java告诉我,如果变量“isACoolGuy”为false,则应该使用语句中的else。
if (isACoolGuy = true){
System.out.println("Thank you for this name... "+ name);
}else if(isACoolGuy = false){
System.out.println("Okay im changing my name since you are an idiot");
name = name = "jack";
System.out.println("My name is "+ name + " now");
之前有一个switch语句应该将“isACoolGuy”布尔值更改为false。
case "name":
System.out.println("You are an a******");
isACoolGuy = false;
break;
答案 0 :(得分:3)
你的错误
if (isACoolGuy == true){
System.out.println("Thank you for this name... "+ name);
}else if(isACoolGuy == false){
System.out.println("Okay im changing my name since you are an idiot");
name = "jack";
System.out.println("My name is "+ name + " now");
或更好
if (isACoolGuy){
System.out.println("Thank you for this name... "+ name);
}else if(!isACoolGuy){
System.out.println("Okay im changing my name since you are an idiot");
name = "jack";
System.out.println("My name is "+ name + " now");
并且在你的情况下最正确的方法是跳过else if并将其替换为像这样的其他
if (isACoolGuy){
System.out.println("Thank you for this name... "+ name);
}else {
System.out.println("Okay im changing my name since you are an idiot");
name = "jack";
System.out.println("My name is "+ name + " now");
和一些理论
isACoolGuy = true表示为isACoolGuy变量赋值true。 在if中使用它总是返回true isACoolGuy == true检查变量isACoolGuy是否具有true值。 这是比较
在if中你可以跳过比较布尔值,因为if具有以下格式
if(true)
{
}
所以如果(isACoolGuy)类似于if(isACoolGuy == true)
答案 1 :(得分:1)
不是检查它是否为真,而是将isACoolGuy指定为true,因此它总是计算为true。
if (isACoolGuy = true){
改为使用
if (isACoolGuy == true){
更好用: -
if (isACoolGuy)
答案 2 :(得分:0)
要检查条件,请使用==
而非=
进行分配,例如此if(isACoolGuy == true)
。
尝试删除第二个条件if(isACoolGuy == false)
没有必要,这就足以让关键字else
像这样:
isACoolGuy = true;
...
if (isACoolGuy){ // the positive control
System.out.println("Thank you for this name... "+ name);
}else{ // otherwise negative
...
}