public class BakeryBusiness {
public static void main(String[] args){
}
public void yearsOfBusiness(){
int myBusinessStarts = 2023;
}
public void itemsToSell(){
String item1 = "birthdayCake";
String item2 = "pastry";
String item3 = "coffee";
String item4 = "bubbleTea";
System.out.println(item1);
}
public boolean optionsToChoose(){
boolean sweetIsChose = true;
if(sweetIsChose = true){
System.out.println("You have chose a dessert! What will it be?");
}else{
System.out.println("Are you craving for salty foods? Choose what you want!");
return sweetIsChose;
}
}
}
我在optionsToChoose方法的哪里放置return语句?我要打印“您选择了甜点!它将是什么?”
答案 0 :(得分:0)
您需要从方法的每个分支中返回。对于您来说,您是从else
分支返回的,而不是从if
返回的。
if (sweetIsChose == true)
System.out.println("You have chose a dessert! What it it be?");
return sweeIsChose; //<- you were missing this
}
请注意,您使用==
进行布尔比较,但是您只使用了一个=
,用于分配。因此应该是sweetIsChose == true
。
答案 1 :(得分:0)
如果sweetIsChose
为false
,则仅返回一个值。因此,您也必须在if语句中指定它。
if (sweetIsChose == true)
System.out.println("You have chose a dessert! What it it be?");
return sweetIsChose;
} else {
System.out.println("Are you craving for salty foods? Choose what you want!");
return sweetIsChose;
}
但是由于您在两个语句中都返回了相同的内容,因此您可以使整个过程更加清晰:
if (sweetIsChose == true)
System.out.println("You have chose a dessert! What it it be?");
} else {
System.out.println("Are you craving for salty foods? Choose what you want!");
}
return sweetIsChose;
您的一个小错误是,您尝试在if语句中为局部变量sweetIsChose
分配一个新值。因此,请使用sweetIsChose = true
而不是sweetIsChose == true
。但是,您可以省去麻烦,只需将其传递给if (sweetIsChose)
。