您好,我正在尝试以下语句的正确逻辑。


如果“带宽”比“用户数”(NumberOfUsers)低250或更多,则VPN状态为“ A”(带宽> NumberOfUsers + 250)
如果“带宽”介于“ NumberOfUsers”和“ NumberOfUsers”之间的250之间,则状态为“ B”(NumberOfUsers + 1到NumberOfUsers + 249之间的带宽)
如果“带宽”等于或大于“ NumberOfUsers”,则状态为“ C”(带宽

这是我尝试过的代码

            if (bandwidthValue > numberofusersValue + 250) {
               setVpnStatus(A);
            } else if (bandwidthValue >= numberofusersValue+1    && bandwidthValue <= numberofusersValue + 249) {
                setVpnStatus(B);
            } else if (bandwidthValue <= numberofusersValue) {
                setVpnStatus(C);
            }


测验

        User Entered -
                    BandwidthValue      =   1234
                    NumberofUsersValue  =   900

        Vpn Status Expected By the user - C
        PASSED

    User Entered -
                BandwidthValue      =   1234
                NumberofUsersValue  =   1234

    Vpn Status Expected By the user - C
    PASSED

    User Entered -
                BandwidthValue      =   1236
                NumberofUsersValue  =   1234

    Vpn Status Expected By the user - B
    Unable to set any status - FAILING



对于第三个测试用例无效。


对于上面提到的这些语句,上面的代码是否适合BandwidthValue = x和NumberofUsersValue = y的值?

最佳答案

以下逻辑满足您问题的要求:

if (bandwidthValue <= (numberofusersValue - 250)) {
    setVpnStatus(A);
} else if (bandwidthValue != numberofusersValue && (bandwidthValue - numberofusersValue <= 250)
                || (bandwidthValue > (numberofusersValue - 250) && bandwidthValue < numberofusersValue)) {
    setVpnStatus(B);
} else {
    setVpnStatus(C);
}


演示:

public class Main {
    public static void main(String args[]) {
        // Tests
        System.out.println(getVpnStatus(1234, 900));
        System.out.println(getVpnStatus(1234, 1234));
        System.out.println(getVpnStatus(1236, 1234));
    }

    static char getVpnStatus(int bandwidthValue, int numberofusersValue) {
        char vpnStatus;
        if (bandwidthValue <= (numberofusersValue - 250)) {
            vpnStatus = 'A';
        } else if (bandwidthValue != numberofusersValue && (bandwidthValue - numberofusersValue <= 250)
                || (bandwidthValue > (numberofusersValue - 250) && bandwidthValue < numberofusersValue)) {
            vpnStatus = 'B';
        } else {
            vpnStatus = 'C';
        }
        return vpnStatus;
    }
}


输出:

C
C
B


如有任何问题/疑问,请随时发表评论。

关于java - Java数学逻辑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60747644/

10-09 09:36