-3

I want the dynamic output for a particular condition, and my condition is :

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

If I got the countersize as 4 or 5 or 8, then my output should be any one of these 0 or 1 or 2 for a particular countersize. The output should be < 3.

Example1:

     **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)

Example2 : suppose size = 2; and countersize = 8;// can be anything based on the user input.

     **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)

Please help me with my Java code.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Sthita
  • 1,750
  • 2
  • 19
  • 38

1 Answers1

1

Try this, it works for your examples:

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);
    }
}
aUserHimself
  • 1,589
  • 2
  • 17
  • 26
  • Thanks,I think i figured out a different way that is int outputInt=countersize % size ; this output always will be less than size. – Sthita Sep 19 '13 at 07:38
  • Yes, this works too, but in your Example2 above you will never get 2 as output. I thought you want one of the values {0, 1, 2} to be chosen somehow randomly: you expressed it as '0 or 1 or 2', not as 'always 0 or always 1 or always 2'. But if it suits your application logic, then it is a good solution. – aUserHimself Sep 20 '13 at 06:42