Reputation: 11
Okay, I have a stack sized 24. I need to fill it with random numbers between 1-6. But the condition is every numbers amount should be the same. Like four times 1, four times 2, four times 3. At the end of the day I need to have stack sized 24 that contains random numbers between 1-6 but every numbers amount should be 4. I need to use only stack, I can't use arrays or array list. How can I do it?
int counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0, counter5 = 0, counter6 = 0;
while(!(ottf.isFull())) {
int x = rnd.nextInt(6) + 1;
if (x == 1)
counter1++;
else if (x == 2)
counter2++;
else if (x == 3)
counter3++;
else if (x == 4)
counter4++;
else if (x == 5)
counter5++;
else
counter6++;
if (counter1 <= 4 && counter2 <= 4 && counter3 <= 4 && counter4 <= 4 && counter5 <= 4 && counter6 <= 4)
ottf.push(x);}
I've tried to do it with counters but i guess this won't fix my problem.
Upvotes: 0
Views: 138
Reputation: 79005
Such cases can be handled using switch...case
in a better way.
Do it as follows:
import java.util.Collections;
import java.util.Random;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Random rnd = new Random();
int counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0, counter5 = 0, counter6 = 0;
Stack ottf = new Stack();
do {
int x = rnd.nextInt(6) + 1;
switch (x) {
case 1:
if (counter1 < 4) {
ottf.push(x);
counter1++;
}
break;
case 2:
if (counter2 < 4) {
ottf.push(x);
counter2++;
}
break;
case 3:
if (counter3 < 4) {
ottf.push(x);
counter3++;
}
break;
case 4:
if (counter4 < 4) {
ottf.push(x);
counter4++;
}
break;
case 5:
if (counter5 < 4) {
ottf.push(x);
counter5++;
}
break;
case 6:
if (counter6 < 4) {
ottf.push(x);
counter6++;
}
}
} while (ottf.size() < 24);
Collections.sort(ottf);// Not required. Doing it just to validate the result quickly
System.out.println(ottf);
}
}
Output:
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6]
Upvotes: 1
Reputation: 40024
Try this. It only uses Stack. No arrays and no lists.
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < 24; i++) {
stack.push((i%6)+1);
}
Collections.shuffle(stack);
System.out.println(stack);
The output will always be different unless you seed shuffle. But here is one example.
[5, 2, 1, 6, 3, 4, 5, 1, 4, 3, 2, 6, 1, 4, 3, 6, 2, 5, 3, 2, 6, 5, 4, 1]
Upvotes: 1
Reputation: 1500
Can you try this? I gave you the piece of code regarding your idea. it's shorter and cleaner.
//this array is for counting only. The output is also a stack.
int[] count = new int[6];
while(!ottf.isFull()) {
int x = rnd.nextInt(6) + 1;
if(count[x-1]>4) {
continue;
}
count[x-1]++;
if (count[x-1]<=4) {
//push to stack
ottf.push(x);
}
}
Upvotes: 1
Reputation: 167
if (counter1 <= 4 && counter2 <= 4 && counter3 <= 4 && counter4 <= 4 && counter5 <= 4 && counter6 <= 4)
I guess this is wrong, because you use &&
which stops any number of being added if one of the counters are over 4.
Use this instead:
if (x == 1 && counter1 < 4)
ottf.push(x);
counter1++;
etc. Try debugging and post your result, if you have issues
Upvotes: 2