简体   繁体   中英

endless erors within my logical operator if statement

im pretty new to java, as such i am unsure of how fix the errors on my codes, trying out a logical operator on java but got overwhelmed the with the repetitive errors on every statement lines ("illegal start of type", "illegal start of expression", "not a statement" and " ';' expected") did i use the wrong form of code?

   if (gender = "m") {
        if (age >=18 & <30 && vo2max >= 40 && <=60){
            EligibleSport = "Basketball";
        }
        if (age >= 18 & <26 && vo2max >= 62 & <=74){
            EligibleSport = "Biycling";
        }
        if (age >= 18 & <26 && vo2max >= 55 & <=67){
            EligibleSport = "Canoeing";
        }
        if (age >= 18 & <22 && vo2max >= 52 & <=58){
            EligibleSport = "Gymnastics";
        }
        if (age >= 10 & <25 && vo2max >= 50 & <=70){
            EligibleSport = "Swimming";
        }

Issues

  1. The comparison operators are generally binary operators. So they need two parameters.
  2. Java expects comparisons to return boolean and that is achieved by && or || . Some of the operators used in the question as bitwise & and they will return boolean only if the operand is boolean . If incorrectly used to eventually result in boolean , it will return unexpected results
  3. string should be compared with "equals" or "equalsIgnoreCase`

Solution

        if ("m".equals(gender)) {
            if (age >= 18 && age < 30 && vo2max >= 40 && vo2max <= 60) {
                eligibleSport = "Basketball";
            }
            if (age >= 18 && age < 26 && vo2max >= 62 && vo2max <= 74) {
                eligibleSport = "Biycling";
            }
            if (age >= 18 && age < 26 && vo2max >= 55 && vo2max <= 67) {
                eligibleSport = "Canoeing";
            }
            if (age >= 18 && age < 22 && vo2max >= 52 && vo2max <= 58) {
                eligibleSport = "Gymnastics";
            }
            if (age >= 10 && age < 25 && vo2max >= 50 && vo2max <= 70) {
                eligibleSport = "Swimming";
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM