0

i am trying to work with arrays where each element is a random number. it has 3 elements and i need to remove the lowest value, and come up with 2 elements. after this i add the two elements together. Here is the code i am trying to compile.

int D6;
Random rnum = new Random();
int[] abilityScore = {rnum.nextInt(6) + 1, rnum.nextInt(6) + 1, rnum.nextInt(6) + 1};
int roll = 0;
int min = abilityScore[0];

for(int u = 0; u < abilityScore.length ; ++u){
  if(min < abilityScore[u]){
    min = abilityScore[u];
  }                                   
}
for(int o = 0 ; o < abilityScore.length ; ++o){
  if(min == abilityScore[o]){
    for(int i = 0 ; i < abilityScore.length ; ++i){
      abilityScore[i] = abilityScore[i + 1];
    }
  } 
}
for(int x = 0 ; x < abilityScore.length ; ++x){
  roll += abilityScore[x]; 

}

I am getting an out of bounds exception, which does make sense but i'm not sure how else to make this change. information on how else to do this or how i can tweak this to make it work would be great. Thanks!

Dilly
  • 1
  • 2
    Please read: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Turing85 Feb 21 '20 at 23:39
  • I second @Turing85's link recommendation above. No, it won't give you the direct solution, but it will give you the key steps that you should follow to help you find the solution to this problem, and more importantly, to *tomorrow's* problem. – Hovercraft Full Of Eels Feb 21 '20 at 23:40
  • `abilityScore[i] = abilityScore[i + 1];` `i+1` is your problem. You need to adjust your for-loop. – PM 77-1 Feb 21 '20 at 23:40
  • abilityScore[i] = abilityScore[i + 1]; you use this at a time when I is at the highest legal value. – NomadMaker Feb 21 '20 at 23:41

0 Answers0