Results 1 to 16 of 16
Thread: Making a count..
- 02-17-2009, 04:39 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
Making a count..
Gah its hard to explain..
but lets say...
There is:
case 1:
break;
case 2:
break;
case 3:
break;
And they are called at random.
I want to try and see wich is called the most.
Ok ok sorry i dont know what term to use say i say "call"
but i mean like
case(random(3));
void case(int n) {
switch (n) {
case 1:
break;
Lets say the random comes up with 1.
Thats what i mean by calling on 1 lol sorry.
Like a simple way i had some ideas but dont know how to do them..
And i dont want some long solution like if (2 > 1)
because there will be over 100 cases not just 3 ;)
Thanks and if i need to explain something more just tell me.
And also theres a second part..
I want to be able to do something like
void case(int num) {
switch (num) {
case 0:
return;
case 1:
return;
case 2:
return;
because if you have break; it doesnt stop the void so it checks through all the rest of them
wich is like a waste i guess. can i do that or can you not use return with cases?
And a part 3 sorry =)
lets say you had:
switch (n) {
case 1:
i = a;
continue;
case 2:
i = b;
continue;
}
if (i == a) {
//blah blah
}
If case 1 were true would it skip case 2 and go straight to the if statement or check if case 2 is true?
Thanks everyone!Last edited by Samgetsmoney; 02-17-2009 at 05:09 AM.
- 02-17-2009, 04:43 AM #2
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
what you mean saying that? what method calls them or what realizes the effect? post some code )And they are called at random.
- 02-17-2009, 04:46 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
There updated hows that?
- 02-17-2009, 01:18 PM #4
Heads spinning ...
Sam... it's very difficult to understand what you want, but I'll try...
part 1:
You what to have a counter for many time each case is randomly picked?
Just add a counter varialble to each case section...
Part 2:Java Code:int countCase1 = 0; . . . switch... case 1: countCase1++; break;
Don't call your method "Case". Call it something that has meaning. I don't know what the problem is here. The break command breaks out of the switch statement (it doesn't go through the rest of the case statements). The void in the method declaration indicates that the method doesn't return any values. I don't any relation between the break statement and the void.
Part 3:
Case1 is an assigment. THere is no true or false in your case statement. Try it out. Use continue and break and see what happens.
Luck,
CJLSChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-17-2009, 01:50 PM #5
T get a random int, try this:
a: How high you want it to go, if its 40, then it'll be a number between 0 and 40.Java Code:int rand = (int)(Math.random() * a) + b;
b: To shift it, If you want it between 1 and 40, then a would be 39 and b would be 1.
For counting, you could have an array, or a separate counter for each.
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-17-2009, 06:55 PM #6
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
and where is the random limit? or it is non-limited one?
Math.random(n);
- 02-17-2009, 09:28 PM #7
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
CJSLMAN gets what im saying for part 1
i need a count like that but something more complex like an arraylist or something is what i heard...
Like 100's of cases so adding 100 ints and 100 case++; isnt going to be very organized..
So i was thinking all the case numbers get added to the list then each time a particular case is picked with random it adds one count to the case number..
- 02-17-2009, 09:47 PM #8
Do you know how many random numbers you will be generating? 200? 500? I definitely agree that using switch-case would not be wise. I have another idea....
Instead of switch-case just use an ArrayList:
- start loop
- Generate a random number
- assign a number to that array element (i++)
- stop loop
- read data form ArrayList
Something like (pseudocode)...
random niumber = 57
ArrayList [57] = add.Array[57]++
Make sense?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-17-2009, 09:53 PM #9
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
Im looking for something like (sorry if i write this wrong im new)
ArrayList cases = new ArrayList;
random(100);
lets say 50 gets picked
case 50:
if (cases.contains(50)) {
cases(50)++;
} else {
cases.add(50, 1);
}
- 02-17-2009, 11:26 PM #10
Would that work? I think but I'm not positive whether the counter and the number will be in the same locations in their respective arrays (wouldn't work if they weren't) try it out and see.Java Code:ArrayList<Integer> list = new ArrayList<Integer>(); ArrayList<Integer> counter = new ArrayList<Integer>(); for(int i = 0; i < NUM; i++) { // replace NUM with how many times to do it Integer rand = new Integer((int)(Math.random() * 9) + 1); // 1 -10 if(list.contains(rand)) counter.set(list.indexOf(rand), list.indexOf(rand) + 1) else { list.add(rand); } } list.trimToSize(); counter.trimToSize(); for(int i = 0; i < list.size(); i++) { System.out.println(Occurrences of " + list.get(i) + ": " + counter.get(i)); }
-MK12Last edited by MK12; 02-17-2009 at 11:35 PM. Reason: Forgot Generics
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-17-2009, 11:44 PM #11
Close , but I don't think it would work...
This would return true if the element (rand) existed, but you need to know if the index is populated or not... right?Java Code:if(list.contains(rand))
There can be various solutions... what I did was:
- Create an arraylist and fill it with zeros (with for loop)
- start loop
- generate random number (randNum)
- randArray.set(randNum,randArray.get(randNum)+1);
- end loop
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-17-2009, 11:47 PM #12
Your talking about whether there's space in the counter arraylist when i set the value, right? I could just add it also when the else is executed, or ensureCapacity, couldn't I (or Him if he uses that).
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-17-2009, 11:59 PM #13
hhhmmm... the thing is that you don't need two arraylists. You can do it with one... for example
- generate arraylist with size = 200 and fill with zeros
- start loop
- generated random number = 57 (i use the Random class ... I don't like the math one)
- set the arraylist index (randNum) with the number it has (0) plus one -> randArray.set(randNum,randArray.get(randNum)+1);
- end loop
- print the arraylist: a sample output of this using an array size = 10:
Luck,randArray[0]: 1
randArray[1]: 1
randArray[2]: 0
randArray[3]: 0
randArray[4]: 1
randArray[5]: 0
randArray[6]: 3
randArray[7]: 1
randArray[8]: 1
randArray[9]: 2
randArray[10]: 1
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-18-2009, 01:04 AM #14
Oh ya that makes more sense.. but why the + 1? then random num 50 would be in index 51 in the zero-starting list right?
EDIT: And what's wrong with Math.random()? I know a generator is easier when making many numbers but I'm only doing it once in this situation (which loops).
-MK12Last edited by MK12; 02-18-2009 at 03:47 AM.
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-18-2009, 01:09 AM #15
Because your not saving the random number... the random number is the index in the ArrayList. Your saving the times that the random number is generated, hence the "+1" (I tried it get to accept the ++, but didn't have any luck).
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-18-2009, 01:44 PM #16
Similar Threads
-
to get count value as a variable
By arunkumarinfo in forum JDBCReplies: 2Last Post: 03-30-2009, 01:32 AM -
how we can get the count value in a variable
By arunkumarinfo in forum JDBCReplies: 4Last Post: 01-23-2009, 09:39 PM -
Getting row count
By Java Tip in forum Java TipReplies: 0Last Post: 02-11-2008, 08:49 AM -
how to count 2 inserts together?
By kim85 in forum New To JavaReplies: 0Last Post: 01-20-2008, 11:25 AM -
making a count down timer using java
By saytri in forum New To JavaReplies: 3Last Post: 12-29-2007, 09:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks