如何以特定顺序打印数字

时间:2013-09-19 06:20:14

标签: java scriptlet

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

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代码。

1 个答案:

答案 0 :(得分: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);
    }
}