开发者

Multiple cases - Testing based on ranges of values rather than single values in Java?

As part of learning to write Java I found on the web the switch statement for multiple cases.

The problem for me with this statement is that it compares the argument to the single numbers that I use as cases, but what if I want to differentiate the flow based on the range of values the argument belongs to?

Is there a more elegant way than using a lot of ifs? I am looking for something like the cond statement in Scheme.

public class Assignment02Q03 {
   public static void main(String[] args){
      int grade = Integer.parseInt(args[0]);
      if (grade >= 90) {
     开发者_高级运维    System.out.println("A");
      } else {
         if (grade >= 80 ){
            System.out.println("B");
         } else {
            if (grade >= 70){
               System.out.println("C");
            }else {
               if (grade >= 60){
                  System.out.println("D");
               }else { 
                  System.out.println("F");
               }
            }
         }  
      } 
   }
}

There must be something more elegant :)

Thank you!


Generally written as:

if (grade >= 90) {
    System.out.println("A");
} else if (grade>=80 ) {
    System.out.println("B");
} else if (grade>=70) {
    System.out.println("C");
} else if (grade>=60) {
    System.out.println("D");
} else { 
    System.out.println("F");
}

Not that there is anything special about else if. The braces can be replaced by a single statement, in this an if-else statement. The indentation is like nothing else in the language, but it's an idiom that should be easy to follow.

For an expression, there is also the ternary operator:

System.out.println(
    grade>=90 ? "A" :
    grade>=80 ? "B" :
    grade>=70 ? "C" :
    grade>=60 ? "D" :
    "F"
);


In Java there are the ifs, switch case construct and ternary operator(which is a shortened if). Nothing elegant I guess:)


There's nothing built into the language. For terse and quick code, you can create a Map that maps your test values to Runnable or Callable actions. However, that tends to be a little opaque because you have to look elsewhere in your code for info on what's in the map.


Like many other languages you may use a ternary if-test.

String text = (1 < 10) ? "One is not greater than ten!" : "One is greater than ten";


There is OOP and polymorphism in Java) You can write smt like

 ActionFactory.create(grade).execute()

 //returns some instance
 public static AbstractAction create(int grade){
      //can be based on Maps as @Ted Hopp mentioned
      if(grade >= 0){
           return new PositiveAction();
      } else {
           return new NegativeAction();
      }
 }
 //can be interface
 class AbstractAction{
     public abstract void execute();
 }
 class PositiveAction extends AbstractAction {
     public void execute(){Sout("positive");}
 }
 class NegativeAction extends AbstractAction {
     public void execute(){Sout("negative");}
 }

It seems more verbose, but it works in real tasks. Java isn't for elegant solutions. Java for work. Feel Java)


if (a > b) 
{
  max = a;
}
else 
{
  max = b;
}

can be written like this....

max = (a > b) ? a : b;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜