Back | Reverse |

variable's variable? Edited: Arrays (in Java)
Link | by ve_krin on 2008-03-08 20:09:13 (edited 2008-03-09 08:46:58)
Well, the problem is that I need many of variable.
For example, int point0, point1, point2, ... point500)
Therefore I got to initiate them.

What I thought was, "This is such ineffective way of programming.
Then I tried to make the program initiate variable by itself.

To produce massive work, we use looping.
for(int amount=1; amount<=500; amount++){int xxx}

The question, what should we fill xxx?
Is there any other way to do massive initiation of same data type?



P.S.: I know it may sounds ridiculous, but an idea is an idea.




Edited:



I use an array (int win[]) to store the massive amount of of variable.

Thanks for reading.


P.S.: It was ridiculous!

-Problem 1 Solved-




Problem 2



Now that I've stored some numbers on an array (win[])

I'd like to display the integer in percentage

Therefore I typed:


int round = 500

for (int b = 0; b < 100; b++)
{
percentage[b] = (win[b] / round);

System.out.println(+b +" = " +percentage[b] +"%");
}



What happened on the output is:
All the percentage (percentage[b]) are zero
Even that there are not zero amount on win[b]

Why is it?
How to solved it? (have the percentage of number stored at win[b])

Dream ends when we wake up and life ends when we die.
What's the different?

Re: variable's variable? Edited: Arrays (in Java)
Link | by zehippo on 2008-03-09 10:22:43 (edited 2008-03-09 10:23:34)
Multiply win[b] by 100 before you multiply, or divide it by 5 rather than 500, or use floating point numbers.

i.e.

percentage[b] = (win[b] * 100 / round);

or

float percentage[]; // or something

percentage[b] = ((float)win[b] / round);
System.out.println(+b +" = " +percentage[b]*100.0 +"%");


Or.. um.. something along those lines. I'm not proficient in Java, but I think these solutions would probably solve your issue.

Re: variable's variable? Edited: Arrays (in Java)
Link | by ve_krin on 2008-03-09 11:31:09 (edited 2008-03-09 11:33:12)
Float or even Double! That's it, int would be inappropriate variable data type, how come I didn't notice...
Moreover, If it is not multiplied by 100 how could I call that "percentage".
Ah...

Thanks zehippo.

-Problem 2 Solved-

Dream ends when we wake up and life ends when we die.
What's the different?

Back | Reverse |

Copyright 2000-2024 Gendou | Terms of Use | Page loaded in 0.0024 seconds at 2024-12-28 19:21:39