简体   繁体   中英

java assigning values to an array

so i'm creating a program in java where you enter scores and it classifies them into deciles (0-9, 10-19, 20-29, ... 80-89, 90-100) and I've got a grasp on how the program is supposed to work but I'm missing one key element. I create an array with 10 elements (one for each decile range). Once the user inputs their score it divides by 10 and then needs to put the score into it's appropriate spot in the array, but that's where I'm lost. It has to do a bunch of things after that, that I understand but how am I supposed to tell the code when someone enters 55 to increase the 50-59 part of the array by 1 and so on?

Um, it sounds like you just want:

bucketedScores[score / 10]++;

Don't you? Or possibly clearer:

bucketedScores[score / 10] = roughScores[score / 10] + 1;
int index = value / 10;
myArray[index] += 1;

FYI, given what you said, you will get an IndexOutOfBoundsException with a score of 100. May need to deal with that.

As the range is has 101 values from 0 to 101 you need to do more than divide by 10.

Do you mean like?

int[] scores = new int[10];

int decile = ...
int score = ....

scores[Math.min(9, decile/10)] += score;

This ensures that 100 is mapped to 9. Another solutions is to use (int)(decile/10.1) which would maps 0 to 10 to the first decile.

这样尝试。

bucketedScores[score / 10] = roughScores[score / 10] + 1;

Sounds like

int[] myarray = new int[10];
 // Divide and increase the number returned (like 84/10 = 8 in integer division)
myarray[num/10]++

Though 100 would throw it off, you would need a special case for that one.

According to the Question get into loops say for example i hope you're dividing 100 values 10-10 each. Use for loops and check and categorize it by assigning the input to a temporary variable for each iteration of inputs.

for(int i=0; i<=100; i++)
{
    if(a[i] >= 0 && a[i] < 10)
                your desired o/p(execution)
     else if(a[i] > 10 && a[i] < 20)
                your desired o/p(execution)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM