I can't seem to figure out why it's not printing correctly:
public class test3 {
public static void main(String[] args) {
double[] gens = {100, 200.1, 9.3, 10};
double d0 = 0;
double d1 = 0;
double d2 = 0;
double d3 = 0;
double d4 = 0;
double d5 = 0;
double d6 = 0;
double d7 = 0;
double d8 = 0;
double d9 = 0;
for (int i = 0; i < gens.length; i++) {
double percs = gens[i];
while (percs < -9 || 9 < percs) percs /= 10;
percs = Math.abs(percs);
if (percs == 0) {
d0 += 1;
}
else if (percs == 1) {
d1 += 1;
}
else if (percs == 2) {
d2 += 1;
}
else if (percs == 3) {
d3 += 1;
}
else if (percs == 4) {
d4 += 1;
}
else if (percs == 5) {
d5 += 1;
}
else if (percs == 6) {
d6 += 1;
}
else if (percs == 7) {
d7 += 1;
}
else if (percs == 8) {
d8 += 1;
}
else if (percs == 9) {
d9 += 1;
}
}
double[] pack = {
d0 /= gens.length,
d1 /= gens.length,
d2 /= gens.length,
d3 /= gens.length,
d4 /= gens.length,
d5 /= gens.length,
d6 /= gens.length,
d7 /= gens.length,
d8 /= gens.length,
d9 /= gens.length
};
System.out.println(pack[0]);
System.out.println(pack[1]);
System.out.println(pack[2]);
System.out.println(pack[3]);
System.out.println(pack[4]);
System.out.println(pack[5]);
System.out.println(pack[6]);
System.out.println(pack[7]);
System.out.println(pack[8]);
System.out.println(pack[9]);
}
}
I know it's pretty repetitive, but that's just how I'm dealing with it for now.
Anyway, essentially, it should take those four numbers in the gens array, cut them off to the first digit of each number (i.e. 1, 2, 9 & 1 respectively), then count how many times each digit from 0-9 shows up, so '1' should give 0.5 (50%), 2 should give .25 (25%) and 9 should give .25 (25%) where every other digit from 0-9 should give 0. I get 0.5 for the digit '1', but 0 for every other one, and I haven't been able to find my mistake. The results I get vary depending on which numbers I input (hardcode), but they're pretty much always wrong.
Any ideas?
Thank you!