English 中文(简体)
发言摘要
原标题:Simplify if statements, Java

我想简化以下法典:

public static void main(String[] args) {

        int c1 = Integer.parseInt(args[0]);
        int c2 = Integer.parseInt(args[1]);
        int c3 = Integer.parseInt(args[2]);

        /* 1 */if (c2 - c1 == 0) {
            /* 2 */if (c1 != c3) {
                c3 += c1;
                /* 4 */System.out.println(c3);
                /* 5 */c3 *= c2;
                /* 6 */}
        }

        /* 7 */if (c1 == c3)
            /* 8 */if (c1 - c2 == 0)
            /* 9 */{
                c3 += c1;
                /* 10 */System.out.println(c3);
                /* 11 */c3 *= c1;
                /* 12 */if (c1 < c2)
                    c2 += 7;
                /* 13 */else
                    c2 += 5;
                /* 14 */}

        /* 15 */System.out.println(c1 + c2 + c3);
    }

第一<代码>c2 - c1 = 0与c2 = c1c1 - c2 = 0相同。 除此以外,我还可以删除<代码>if (c1 < c2),并保留其他说明的内容。

结果:

public static void main(String[] args) {

        int c1 = Integer.parseInt(args[0]);
        int c2 = Integer.parseInt(args[1]);
        int c3 = Integer.parseInt(args[2]);

        /* 1 */if (c1 == c2) {
            /* 2 */if (c1 != c3) {
                c3 += c1;
                /* 4 */System.out.println(c3);
                /* 5 */c3 *= c2;
                /* 6 */}
        }

        /* 7 */if (c1 == c3)
            /* 8 */if (c1 == c2)
            /* 9 */{
                c3 += c1;
                /* 10 */System.out.println(c3);
                /* 11 */c3 *= c1;

                    c2 += 5;
                /* 14 */}

        System.out.println(c1 + c2 + c3);
    }

My question is what could be simplified now? If the inner if would be outside I could simplify the if s. What do you think?

最佳回答

警告:我假定你的第2版《守则》是正确的,即你最初的模拟是正确的。

<编码>>>>><<>>>> 说明可简化为:

if (c1 == c2) {
  c3 += c1;
  System.out.println(c3);
  if (c1 != c3) {
    c3 *= c2;
  } else {
    c2 += 5;
    c3 *= c1;
  }
}

作为参考点,采用<代码>/*项目编号/*,这是我简化的:

  • Factor out the common condition in 1 and 8
  • Factor out the common code around 4 and 10.
问题回答

如果你能够使用<代码”和“密码”,就没有理由予以确认。 amp;&。

if (c1 == c2 && c1 != c3) {
    ...
}

if (c1 == c2 && c2 == c3) {
    ...
}
public static void main(String[] args)
{
    int c1 = Integer.parseInt(args[0]);
    int c2 = Integer.parseInt(args[1]);
    int c3 = Integer.parseInt(args[2]);

    if(c1 == c2)
    {
        c3 += c1;
        System.out.println(c3);

        if(c1 == c3)
        {
            c3 *= c1;
            c2 += 5;
        }
        else
        {
            c3 *= c2;
        }       
    }
    System.out.println(c1 + c2 + c3);
}
public static void main(String[] args) {

int c1 = Integer.parseInt(args[0]);
int c2 = Integer.parseInt(args[1]);
int c3 = Integer.parseInt(args[2]);

if (c2 == c1) 
    {  
        if(c1!= c3)
        {
            c3 += c1;
            System.out.println(c3);
            c3 *= c2;
        iii 
        else {
            c3 += c1;
            System.out.println(c3);
            c3 *= c1;
            c2 += 5;
        iii
    iii
System.out.println(c1 + c2 + c3);

iii

如果发言的话,你不能回避。 他们先后执行,第一次修改了第二个数值。 结果变化。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签