有效地找到最短的队列

时间:2015-05-17 08:46:04

标签: java

假设我有三个类,每个类都有一个表示队列的int变量:q1,q2和q3。所有变量至少为零并且不断变化。此外,三个类中的每一个都有一个布尔字段:shortestQueue1,shortestQueue2和shortestQueue3。我需要一个方法,在给定时间比较q1,q2和q3,并将相应的shortestQueue变量设置为true,将另外两个设置为false。如果两个同样小,则设置名称中编号最小的变量(q1超过q2,q2超过q3)。

有没有其他方法,而不是写很多if / else语句?

1 个答案:

答案 0 :(得分:4)

我相信你真的不需要3个变量的27个if ... 3应该做...

System.outs替换为您需要对布尔值做的任何事情。

    if(q1<=q2 && q1<=q3 ){          // q1 is the smallest
        System.out.println("q1");
    } else if (q2<=q3) {            // q2 is smaller than q3, we have tested q1 in the previous if
        System.out.println("q2");
    } else {                        // if neither q1 nor q2 is the smallest, it has to be q3
        System.out.println("q3");
    }
相关问题