在if else语句中同时使用多个条件

时间:2018-10-05 22:33:16

标签: java if-statement

我有一个问题,涉及是否其他语句使用Java。我在如何在括号内放置两个条件还是什至有可能?

到目前为止,这是我的代码:

public class rollerCoaster {



    int age = 11;
    int weight = 81;

    if ( age <= 10 && weight < 10 ) {
            System.out.println("This person needs to ride the black roller coaster.");
    }
    else if ( age <= 10 && weight >= 80 && <= 200  ) {

    }
    else {

    }

    //The new part is this:

    else if ( condition_two ) {

    }

};

3 个答案:

答案 0 :(得分:0)

对于关系运算符'<=',您需要提供两个操作数。因此,您的if语句中的else if应该显示为:

else if ( age <= 10 && weight >= 80 && weight <= 200 ) {

}

答案 1 :(得分:0)

在类名中,所有约定的firt图表的首字母必须大写,并且代码的结构我想应该是这样的

public class RollerCoaster {

static int age = 11;
static int weight = 81;


public static void main(String args[]) {
    if(age <= 10) {
        if(weight < 10) {
            System.out.println("This person needs to ride the black roller coaster.");  
        }else if(weight >= 80 && weight <= 200) {
            System.out.println("This person needs to ride the red roller coaster.");
        }else {
            System.out.println("The roller coaster is not able for this person");
        }
    }else{
        if(weight < 10) {
            System.out.println("This person needs to ride the black roller coaster.");  
        }else if(weight >= 80 && weight <= 200) {
            System.out.println("This person needs to ride the red roller coaster.");
        }else {
            System.out.println("The roller coaster is not able for this person");
        }           
    }
}

}

答案 2 :(得分:0)

  public class RollerCoaster {

    static int age, weight; 

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);//this help take input from user

        System.out.println("Enter Your Age Below:");
            age = input.nextInt();
        System.out.println("Enter Your Weight Below:");
            weight = input.nextInt();

System.out.println("You entered "+age+" as your age, and "+ weight+" as your weight!");

//****** First Codition
        if(age < 11){
            if(weight <10){
                //Do somthing
            }else if(weight > 79 && weight < 201){
                //Do another thing
            }else{
                //Do something else
            }  
        }else{
//***** Second Codition
//no need to check if age > 10 since it obviously will
            if(weight >10){
                    //Do somthing
                }else if(weight < 80 && weight < 201){
                    //Do another thing
                }else{
                //Do something else
                }  
            }   
    }
}