-6

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

4

3 回答 3

3
    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);
        }
   }
于 2012-06-24T13:00:28.357 回答
0

所以你有以下内容:

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

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

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

或之后的退货声明block1

if(condition1){
   block1;
   return;
}
if(condition2){
   block2;
}else{
   block3;
}
于 2012-06-24T12:50:29.263 回答
0

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

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

首先,由于 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清楚地看到了语句的结构,并将逻辑与实际工作分开,这经常产生额外的红利。

于 2012-06-24T13:29:30.267 回答