简体   繁体   中英

Use switch case as conditional statement in Java

I am trying to use switch case for selecting out conditions based on variable, but I'm getting error. What would be the correct process to do so? Is it possible using switch case, or should i use nested iffs?

public class Grader {
   // no attributes required
    public Grader() {
// no code required
}


public String grade(int mark) {
    String grade = null;
// code to determine grade goes here
    switch (mark){
        //For marks 85-100, Grade is HD
        case ((100>=mark>=85)):
            grade="HD";
            break;
        //For marks 75-84, Grade is D
        case (mark>=75 && mark<=84):
            grade="D";
            break;
        //For marks 65-74, Grade is C
        case (mark>=65 && mark<=74):
            grade="C";
            break;
        //For marks 50-64, Grade is P
        case (mark>=50 && mark<=64):
            grade="P";
            break;
        //For marks 49-0, Grade is HD
        case (mark<=49 && mark>=0):
            grade="F";
            break;






    }
    return grade;
}
public boolean pass(int mark) {
    boolean pass = false;
// code to determine pass goes here

    return pass;
    } 
}

I

Switch only takes constant values in it's cases. You cannot add expressions in cases which evaluate run time.

The best here is to go with traditional if-else-if .

 public String grade(int mark) {
        String grade = null;
        // code to determine grade goes here
        if (mark >= 100 && mark <= 85) {
            grade = "HD";
        } else if (mark >= 75 && mark <= 84) {
            grade = "D";
        } else if (mark >= 65 && mark <= 74) {
            grade = "C";
        } else if (mark >= 50 && mark <= 64) {
            grade = "P";
        } else if (mark <= 49 && mark >= 0) {
            grade = "F";
        } else {
            grade = "Not found";
        }
        return grade;
    }

Unfortunately, Java doesn't have switch-ranges like Javascript has, so one way to get you're desired result would be to divide the mark by 10 and rounding up if necessary (eg 8.5 becomes 9 and etc.). Then, for each number from 0 to 10, have a case for it like so,

double markDividedByTen = mark/10.0;
int mark = (int) (markDividedByTen + 0.5); //round to whole number
switch (mark){
        case 10:
            grade="HD";
            break;
        case 9:
            grade="HD";
            break;
        case 8:
            grade="D";
            break;
        case 7:
            grade="C";
            break;
        case 6:
            grade="P";
            break;
        case 5:
            grade="P";
            break;
        case 4:
            grade="F";
            break;
        case 3:
            grade="F";
            break;
        case 2:
            grade="F";
            break;
        case 1:
            grade="F";
            break;
        case 0:
            grade="F";
            break;
    }

The only issue with this approach is that, a mark from 45 to 49 would technically receive a "P" grade, but I just want to show you an alternative approach to if/else if statements that still utilized switch statements.

you can use the if-else statements for this as switch cannot have expressions. You can use this.

public class Grader {
   // no attributes required
    public Grader() {
// no code required
}


public String grade(int mark) {
    String grade = null;
// code to determine grade goes here
    if(mark>=85 && mark<=100)
    {
     grade = "HD";
    }
  else if(mark>=75 && mark<=84)
  {
    grade = "D";
  }
  else if(mark>=65 && mark<=74)
  {
    grade = "C";
  }
  else if(mark>=50 && mark<=64)
  {
    grade = "P";
  }
  else if(mark>=0 && mark<=49)
  {
    grade = "F";
  }
  return grade;

}
public boolean pass(int mark) {
    boolean pass = false;
// code to determine pass goes here

    return pass;
    } 
}

The problem is that you are trying to generate boolean value while checking if the statement is less or greater than some values.In switch , you cannot control it if you are iterating through integer value type.

if (100 >= mark && 85 <= mark) {
        // grade ...
    } else if (84 >= mark && 75 <= mark) {
        // grade ...
    } else if (74 >= mark && 65 <= mark) {
        // grade ...
    } else if (64 >= mark && 50 <= mark) {
        // grade ...
    } else if (49 >= mark && 0 <= mark) {
        // grade ...
    } else if (-1 >= mark || 101 <= mark) {
        // Error,mark cannot be lower than 0 and higher than 100 
    }

This should solve your problem.But there are many approaches that spare you from multiple check statements.Check enums and mapping technique

For this situation, definitely if-else statement. Your switch statement is wrong btw.

如果您真的想使用开关,您可以将标记除以 10 并使 case 10 & case 9 = A 然后您可以将 if 条件放在 case 中,从 8 到 5 并默认为 f

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