-3

我想要特定条件的动态输出,我的条件是:

size = 3;
countersize = 8; //can be anything based on the user input.

如果我将计数器尺寸设为 4 或 5 或 8,那么对于特定计数器尺寸,我的输出应该是这些 0 或 1 或 2 中的任何一个。输出应该 < 3。

示例 1:

     **user Input:** countersize=7 then   output= 0 or 1 or 2 (only one from these) 
     **user Input:** countersize=5 then   output= 0 or 1 or 2 (only one from these)
     **user Input:** countersize=3 then   output= 0 or 1 or 2 (only one from these)
     **user Input:** countersize=0 then   output=0
     **user Input:** countersize=1 then   output=1
     **user Input:** countersize=2 then   output=2
     **user Input:** countersize=4 then   output= 0 or 1 or 2 (only one from these)
     **user Input:** countersize=9 then   output= 0 or 1 or 2 (only one from these)

示例 2: 假设 size = 2; 和 countersize = 8;// 可以是任何基于用户输入的东西。

     **user Input:** countersize=7 then   output= 0 or 1 or 2 (only one from these) 
     **user Input:** countersize=5 then   output= 0 or 1 or 2 (only one from these)
     **user Input:** countersize=3 then   output= 0 or 1 or 2 (only one from these)
     **user Input:** countersize=0 then   output=0
     **user Input:** countersize=1 then   output=1
     **user Input:** countersize=2 then   output=0 or 1 or 2 (only one from these)
     **user Input:** countersize=4 then   output= 0 or 1 or 2 (only one from these)
     **user Input:** countersize=9 then   output= 0 or 1 or 2 (only one from these)

请帮助我处理我的 Java 代码。

4

1 回答 1

1

试试这个,它适用于你的例子:

public static int getOutput(int size, int countersize) {
    if(countersize < size) {
        // return the countersize value if it is inferior to size
        return countersize;
    } else {
        // return a value from {0, 1, 2}
        Random generator = new Random(); 
        return generator.nextInt(3);
    }
}
于 2013-09-19T07:13:49.890 回答