【问题标题】:The if statement in JAVAJAVA中的if语句
【发布时间】:2012-06-24 12:15:17
【问题描述】:

我的代码中有两个“if”语句,如果一个“if”语句为真,我该怎么做才能自动忽略/禁用另一个?

【问题讨论】:

  • 阅读“else”,如果条件为假,则执行“if”语句的一部分。
  • 我知道问题是,在某些条件下,两个“if”语句都可以成为真的..因此我有两个 if 条件同时在那里工作......做什么你觉得呢?
  • @Matthew 您可能需要先使用return,然后才能转到下一个if
  • 不确定这是否应该关闭。这家伙正在“到达那里”。
  • 不知道为什么这个问题被关闭了:p

标签: java if-statement


【解决方案1】:
    if(maxValY < 3)
    {
        p.setColor(Color.black);
        //display the value of graph width and graph height
        aw = String.valueOf(x1);
        p.drawString("Graph Width = ", 740,490);
        p.drawString(aw,840,490);
        p.drawString("Graph Height = ", 740,510);
        String ah = String.valueOf(y1);
        p.drawString(ah,846,510);
    } 
    else
    {   if (minValx == -1 || minValx == - 2 || minValx == - 3){
        p.setColor(Color.black);
        //display the value of graph width and graph height
        aw = String.valueOf(x1);
        p.drawString("Graph Width = ", 740,90);
        p.drawString(aw,840,90);
        p.drawString("Graph Height = ", 740,110);
        String ah = String.valueOf(y1);
        p.drawString(ah,846,110);
        }
        else{
        p.setColor(Color.black);
        //display the value of graph width and graph height
        aw = String.valueOf(x1);
        p.drawString("Graph Width = ", 50,90);
        p.drawString(aw,150,90);
        p.drawString("Graph Height = ", 50,110);
        String ah = String.valueOf(y1);
        p.drawString(ah,156,110);
        }
   }

【讨论】:

    【解决方案2】:

    所以你有以下内容:

    if(condition1){
       block1;
    }
    if(condition2){
       block2;
    }else{
       block3;
    }
    

    要禁用/忽略第二个if,您可以使用else if 语句:

    if(condition1){
       block1;
    } else if(condition2){
       block2;
    }else{
       block3;
    }
    

    或者block1之后的return语句

    if(condition1){
       block1;
       return;
    }
    if(condition2){
       block2;
    }else{
       block3;
    }
    

    【讨论】:

    • “返回”的目的是什么;一般来说?我无法理解:S
    • 返回的基本意思是:我已经完成了,不要再费心了 :)
    • 此解决方案解决了问题。
    • @TonyEnnis 我不同意。 OP 的问题是如何忽略第二个 if 声明而不是重构他的代码
    【解决方案3】:

    至少有两个答案。一种是在第一个if 语句的底部放置一个return 语句。我不喜欢这样,因为它可能不是一个好的通用解决方案。

    另一个答案是稍微重构你的代码......

    首先,由于 maxValX >= 3 的情况似乎不是问题所在,我会通过反转第一个 if 条件来解决这个问题:

        if (maxValY >= 3) {
            p.setColor(Color.black);
            //display the value of graph width and graph height
            aw = String.valueOf(x1);
            p.drawString("Graph Width = ", 50, 90);
            p.drawString(aw, 150, 90);
            p.drawString("Graph Height = ", 50, 110);
            String ah = String.valueOf(y1);
            p.drawString(ah, 156, 110);
        } else if (maxValY < 3) {
            p.setColor(Color.black);
            //display the value of graph width and graph height
            aw = String.valueOf(x1);
            p.drawString("Graph Width = ", 740, 490);
            p.drawString(aw, 840, 490);
            p.drawString("Graph Height = ", 740, 510);
            String ah = String.valueOf(y1);
            p.drawString(ah, 846, 510);
        }
        if (minValx == -1 || minValx == -2 || minValx == -3) {
            p.setColor(Color.black);
            //display the value of graph width and graph height
            aw = String.valueOf(x1);
            p.drawString("Graph Width = ", 740, 90);
            p.drawString(aw, 840, 90);
            p.drawString("Graph Height = ", 740, 110);
            String ah = String.valueOf(y1);
            p.drawString(ah, 846, 110);
        }
    

    所以发生的只是我把“不重复的 if 语句问题”放在了顶部。

    现在,我们有两个条件,一个处理minValX,一个处理maxValY。但是我们现在注意到每个的内容几乎相同。我们通过稍微重构代码来解决这个问题(并潜入 else...):

        if (maxValY >= 3) {
            p.setColor(Color.black);
            //display the value of graph width and graph height
            aw = String.valueOf(x1);
            p.drawString("Graph Width = ", 50, 90);
            p.drawString(aw, 150, 90);
            p.drawString("Graph Height = ", 50, 110);
            String ah = String.valueOf(y1);
            p.drawString(ah, 156, 110);
        } else if (maxValY < 3) {
            q(491, 510);
        } else if (minValx == -1 || minValx == -2 || minValx == -3) {
            q(90, 110);
        } else {
            throw new RuntimeException("This cannot possibly happen ;-)");
        }
    }
    
    private void q(int loc1, int loc2) {
        p.setColor(Color.black);
        //display the value of graph width and graph height
        aw = String.valueOf(x1);
        p.drawString("Graph Width = ", 740, loc1);
        p.drawString(aw, 840, loc1);
        p.drawString("Graph Height = ", 740, loc2);
        String ah = String.valueOf(y1);
        p.drawString(ah, 846, loc2);
    }
    

    然后我们注意到代码块仍然几乎相同。重构...

    private void someName() {
        // ...
        if (maxValY >= 3) {
            q(50, 90, 510);
        } else if (maxValY < 3) {
            q(740, 490, 510);
        } else if (minValx == -1 || minValx == -2 || minValx == -3) {
            q(740, 90, 110);
        } else {
            throw new RuntimeException("This cannot possibly happen ;-)");
        }
        // ...
    }
    
    private void q(int base, int graphWidth, int graphHeight) {
        p.setColor(Color.black);
        //display the value of graph width and graph height
        aw = String.valueOf(x1);
        p.drawString("Graph Width = ", base, graphWidth);
        p.drawString(aw, base+100, graphWidth);
        p.drawString("Graph Height = ", base, graphHeight);
        String ah = String.valueOf(y1);
        p.drawString(ah, base+100+6, graphHeight);
    }
    

    所以我们看到除了一些常量之外,所有三个部分的代码基本相同。通过重构它们,我们减少了必须调试的代码量。另外,if语句的结构我们看得一清二楚,逻辑与实际工作分离,经常产生额外的红利。

    【讨论】:

    • OP 的问题是如何忽略第二个 if 语句而不是重构他的代码
    • @GETah 是的。有了这个解决方案,OP 可以使用首选的结构得到他想要的东西,并希望能学到更多东西。初学者经常问错误的问题,这里就是这种情况。
    猜你喜欢
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多