多种情况 - 基于值范围而不是 Java 中的单个值进行测试?

发布于 2024-10-21 18:32:18 字数 1192 浏览 3 评论 0原文

作为学习编写 Java 的一部分,我在网上发现了 switch 语句用于多种情况。

对我来说,这个语句的问题在于,它将参数与我用作 case 的单个数字进行比较,但是如果我想根据参数所属的值范围来区分流,该怎么办?

有没有比使用大量 if 更优雅的方法?我正在寻找类似 cond 的内容方案中的语句。

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");
               }
            }
         }  
      } 
   }
}

一定有更优雅的东西:)

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萌能量女王 2024-10-28 18:32:18

一般写为:

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");
}

并不是说​​else if有什么特别的。大括号可以用单个语句替换,在此为 if-else 语句。缩进与该语言中的其他内容不同,但它是一个应该很容易理解的习惯用法。

对于表达式,还有三元运算符:

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

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"
);
慕巷 2024-10-28 18:32:18

Java中有ifsswitch case结构和三元运算符(这是一个缩短的if )。我想没什么优雅的:)

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

魂归处 2024-10-28 18:32:18

该语言中没有内置任何内容。对于简洁快速的代码,您可以创建一个映射,将测试值映射到 RunnableCallable 操作。但是,这往往有点不透明,因为您必须在代码中的其他位置查找有关地图内容的信息。

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.

尝蛊 2024-10-28 18:32:18

与许多其他语言一样,您可以使用三元 if 测试。

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

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";
悲凉≈ 2024-10-28 18:32:18

Java中有OOP和多态性)
你可以这样写smt,

 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");}
 }

它看起来比较冗长,但它在实际任务中有效。 Java 不适合优雅的解决方案。 Java 用于工作。感受Java)

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)

青衫儰鉨ミ守葔 2024-10-28 18:32:18
if (a > b) 
{
  max = a;
}
else 
{
  max = b;
}

可以这样写......

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

can be written like this....

max = (a > b) ? a : b;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文