if的右半拉大括号没写
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入当前天气:");
String wea = sc.next();
System.out.println("请输入当前性别:");
String gen = sc.next();
if(wea.equals("sun")){
if(gen.equals("girl")){
System.out.println("擦防晒霜");
}else {
System.out.println("戴墨镜");
}
}
if(wea.equals("ruin")){
if(gen.equals("girl")){
System.out.println("带一把小花伞");
}else {
System.out.println("带一把大黑伞");
}
}
}
else别写在if里面
if{
}else{
}
这样才对
这个放到这里,底下的也相同,语法是这样的
if(1==1){
}else{
}
语法有误,正确语法为if(){}else{}
,而不是if(){else{}}
,修改后的代码如下。
public class dayo5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入当前天气:");
String wea = scanner.next();
System.out.println("请宿儒当前性别:");
String gen = scanner.next();
if ("sun".equals(wea) || "gril".equals(gen)) {
System.out.println("擦防晒霜");
} else {
System.out.println("戴墨镜");
}
if ("ruin".equals(wea) || "boy".equals(gen)) {
System.out.println("带一把大黑伞");
} else {
System.out.println("带一把小花伞");
}
}
}
程序执行结果如下:
if(){
代码块
}
else{
代码块
}
再看看 if else 的语法
最后面加个括号}
注意if语法
if(条件){
}else{
}