'pulling cards at random until getting one of each
1 of 10 different cards, randomly distributed, in each box Any one collecting all 10 cards at the end of the first 21 days will receive 2 weeks paid vacation every day until Johnny collects all 10 cards. How many boxes of cereal must Johnny’s mother purchase to collect at least one of each card? At the end of the program display a sentence indicating whether or not Johnny won the trip. Run the program a few times and compare the results.
it prints out only 01 and 0 for sum of cards dont know why the loop isn't doing the right values.
import java.util.Random;
public class Cereal{
public static void main(String []args) {
String [] wCards = {"eevee","jirachi","pichu","Taillow","flying pikachu","Togekiss","Blastoise","raichu","flareon","lanturn"};
int e=0,j=0,p=0,t=0,fp=0,toge=0,b=0,r=0,f=0,l=0,sum=0,pull;
int[] pokeCards= {e,j,p,t,fp,toge,b,r,f,l};
Random rand = new Random(10);
boolean card = false;
while (card == false) {
pull = rand.nextInt();
if(pull ==0)
e++;
if(pull == 1)
j++;
if(pull== 2)
p++;
if(pull == 3)
t++;
if(pull == 4)
fp++;
if(pull == 5)
toge++;
if(pull == 6)
b++;
if(pull == 7)
r++;
if(pull == 8)
f++;
if(pull== 9)
l++;
for(int i =0; i < pokeCards.length; i++) {
if(pokeCards[i] >= 0) {
card = true;}
sum+= pokeCards[i];
}
System.out.println("Acme Cereal Inc. Disney World Contest Simulation\n");
System.out.println("Total number of boxes of cereal purchased : " + sum);
System.out.println("Card#\tCArd\tNumber of cards collected");
for(int k=0; k < wCards.length;k++) {
System.out.println(k+1 + "\t" + wCards[k] + "\t\t" + pokeCards[k]+1);
}
if(sum<21){
System.out.println("You won a Vacation");
}else {
System.out.println(" sorry no Vacation");
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
