【问题标题】:Smaller than x and bigger than y [closed]小于 x 且大于 y [关闭]
【发布时间】:2014-12-03 11:08:05
【问题描述】:

我只是想问一下这个例子我怎么用java写。

case 1: 
    if (mark == 1) {
        System.out.println("You can't pass test");
    }    
    break;
case 2:
    if (mark > 1) {
        System.out.println("You can pass test");
    }

但最大分数是 5,所以如果有人把 6 应用程序假设说“错误”。我该如何解决?

【问题讨论】:

  • 你在找&&
  • 这个问题似乎是题外话,因为它是关于语言的基础知识

标签: java case


【解决方案1】:

测试i 小于 x 且大于 y。类似的东西

if (i > y && i < x)

或使用De Morgan's laws

if (!(i <= y || i >= x))

【讨论】:

    【解决方案2】:

    在某些情况下,如果测试看起来像是在检查同一件事,那会令人困惑,因为没有显示整个 switch 语句。如果 switch 语句也使用了标记,那么用 case 和 if 语句进行测试似乎是多余的。

    如果您正在寻找如何使用 switch 测试范围,这里有一个示例。在 switch 中,如果您希望它们具有相同的效果,您可以让它们通过(通过不添加中断):

    switch (mark) {
        case 1: {
            System.out.println("you can't pass test");
            break;
        }
        case 2:
        case 3:
        case 4:
        case 5: {
            System.out.println("You can pass test");
            break;
        }
        default: {
            System.out.println(mark > 5 ? "too high" : " too low");
            break;
        }
    }
    

    虽然 if 语句在这里可能会更好:

    if (mark >= 2 && mark <= 5) {
        System.out.println("You can pass test"); 
    } else if (mark < 2){
        System.out.println("You can't pass test");
    } else {
        System.out.println("ERROR");
    }
    

    【讨论】:

      【解决方案3】:

      您需要在 if 语句中使用 &&。

      if(mark ==1) {
      
      }
      

      if(mark > 1 && mark <=5) {
      }
      

      这是假设最大分数包括五个。

      【讨论】:

      • @Tom 你知道汤姆,有时正是我想念的那些愚蠢的小事让我觉得自己像个大白痴。我责备匆匆忙忙想要快速回答。
      • 不用担心。有时会发生这种情况:)。
      • @Tom 你完全错过了One of Spades
      【解决方案4】:
      if (mark >= 0 && mark<= 5) {
          if(mark<=1){
              System.out.println("You can't pass test");
          }else{
              System.out.println("You can pass test");
          }
      }else{
          System.out.println("Error");
      }
      

      【讨论】:

      • 如果mark2,你的第一个代码会显示什么?
      • 谢谢汤姆 :) 更少/更大 :)
      【解决方案5】:

      重构:

      void testPass()
      {
       System.out.println("You can pass test");
      }
      
      void testFail()
      {
       System.out.println("You can't pass test");
      }
      
      void testError()
      {
       //some code to do error processing
      }
      
      void someMethod(int marks)
      {
       //somecode
         check_block:
         {
              if (marks > 5)
              {
                  testError();
                  break check_block;
              }
              if(marks > 1)
              {
                  testPass():
                  break check_block;
              }
              testFail();
          }   
          //some code
      
      }
      

      尽量避免 switch 。 希望这会有所帮助

      【讨论】:

        【解决方案6】:

        考虑使用业务对象而不是底层类型(在本例中为 int):

        class Mark {
           private final int value;
           public Mark(int value) {
               if (value < 1) {
                    throw new IllegalArgumentException("Value " + value + " too small");
               } else if (value > 5) {
                    throw new IllegalArgumentException("Value " + value + " too large");
               }
               this.value = value;
           }
           public boolean isPassing() {
               return value > 1;
           }
           public int getValue() {
               return value;
           }
        }
        

        在应用程序的主体中

        try {
            Mark mark = new Mark(value);
            System.out.println("You "+(mark.isPassing()?"can":"can't")+" pass test"); 
        } catch (IlleagalArgumentException e) {
            System.out.println("Error: " + e.getMessage()); 
        }
        

        真正的优势在于,随着应用程序的增长,您可以传递 Mark 的实例,而您只需担心将 int 转换为 Marks 时的错误情况。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-01-04
          • 1970-01-01
          • 1970-01-01
          • 2012-09-28
          • 2017-12-02
          • 2023-03-03
          • 1970-01-01
          相关资源
          最近更新 更多