I have error in arrayindexoutofbounds and I've looked up many places and not been able to spot the mistake

huangapple go评论80阅读模式
英文:

I have error in arrayindexoutofbounds and I've looked up many places and not been able to spot the mistake

问题

Main类:
public class Main {
 
  private static String [] Countries={"UK", "Italy",
            "Germany", "Thailand","Denmark"};
  static double average[] = new double[5];
  double[] results = new double[5];
 
  public static String getWinner(ArrayList<MissUniverse> alist){
    int winner = 0;
    for (int s = 0; s < alist.size(); s++){
        if (alist.get(s).average() > alist.get(winner).average() ) {
            winner = s;
        }
    }
    return alist.get(winner).getCountry();
  }
 
  public static String firstRunnerUp(ArrayList<MissUniverse> alist){
    int firstrunnerUp = 0;
    int winner = alist.indexOf(getWinner(alist)); // Retrieve winner index
    for (int s = 0; s < alist.size(); s++){
        if (alist.get(s).average() > alist.get(firstrunnerUp).average() &&
            s != winner ) {
            firstrunnerUp = s;
        }
    }
    return alist.get(firstrunnerUp).getCountry();
   
  }

 public static void main (String[] args){
   ArrayList<MissUniverse> alist = new ArrayList<MissUniverse>();
 MissUniverse missUniverse[] = new MissUniverse[Countries.length];
   
        for(int i=0;i<Countries.length;i++)
        {
            missUniverse[i] = new MissUniverse(Countries[i]);
            missUniverse[i].getScore();
            alist.add(missUniverse[i]);
        }
       
   
   System.out.printf("%-15s%-5s%-5s%-5s%-5s%-5s%-10s",
                "Countries","1","2","3","4","5","Average");
    System.out.println();
    for (int i = 0; i < Countries.length; i++){
    System.out.print(Countries[i]);
    System.out.print("    ");
    missUniverse[i].printInfo();
    System.out.printf("%5.1f", alist.get(i).average() );  ///this line 
    System.out.println();
    }
    System.out.println("The result is");
    System.out.println("Winner: Miss " + getWinner(alist) );
    System.out.println("1st Runner Up: Miss " + firstRunnerUp(alist) );
 
  }
}

MissUniverse类:

class MissUniverse { 
  public static int SIZE=5;
  private String country;
  private double[] score;  
 
  public MissUniverse(String country){
    this.country = country;
    score = new double [SIZE];
  }
 
  public String getCountry(){
    return country;
  }
 
  public void getScore(){
      for (int i=0;i<score.length;i++)
        {
            score[i] = Math.random()*10;
        }
  }
 
  public void setCountry(String newCountry) {
    this.country = newCountry;
  }
 
 private int highest(){
  int i;
        double highest;
        highest = score[0];
        int highestIndex = 0; // Store the index of the highest score

        for (i = 0; i <score.length; i++)
        {
            if (highest < score [i])
            {
                highest = score[i];
                highestIndex = i;
            }
        }
        return highestIndex;
  }
   
  private int lowest(){      
  int i;
        double lowest;
        lowest = score[0];
        int lowestIndex = 0; // Store the index of the lowest score
        for (i = 0; i <score.length; i++)
        {
            if (lowest > score [i])
            {
                lowest = score[i];
                lowestIndex = i;
            }
        }
        return lowestIndex;  
  }
 
  public double average(){
   
    double sum = 0.0;
     for (int i = 0; i < score.length; i++){
        sum +=  score[i];
     }
    int lowestIndex = lowest(); // Retrieve lowest index
    int highestIndex = highest(); // Retrieve highest index
    double average;
    average = (sum - score[lowestIndex] - score[highestIndex] ) / (score.length - 2);
    return average; // Return the calculated average
  }
 
  public void printInfo(){
   
      for(int i=0;i< score.length;i++){  
      System.out.printf("%-5.1f",score[i]);
      }
   
  }
}
英文:
Main Class:
public class Main {
private static String [] Countries={&quot;UK&quot;, &quot;Italy&quot;,
&quot;Germany&quot;, &quot;Thailand&quot;,&quot;Denmark&quot;};
static double average[] = new double[5];
double[] results = new double[5];
public static String getWinner(ArrayList&lt;MissUniverse&gt;alist){
int champion = 0;
for (int s = 0; s &lt; alist.size(); s++){
if (alist.get(s).average() &gt; alist.get(0).average() ) {
winner= s;
}
}
return alist.get(winner).getCountry();
}
public static String firstRunnerUp(ArrayList&lt;MissUniverse&gt;alist){
int firstrunnerUp =0;
for (int s = 0; s &lt; alist.size(); s++){
if (alist.get(s).average() &gt; alist.get(0).average() &amp;&amp; alist.get(s).getCountry() != getWinner(alist) ) {
firstrunnerUp = s;
}
}
return alist.get(firstrunnerUp).getCountry();
}
public static void main (String[] args){
ArrayList&lt;MissUniverse&gt; alist = new ArrayList&lt;MissUniverse&gt;();
MissUniverse missUniverse[] = new MissUniverse[Countries.length];
for(int i=0;i&lt;Countries.length;i++)
{
missUniverse[i] = new MissUniverse(Countries[i]);
missUniverse[i].getScore();
alist.add(missUniverse[i]);
}
System.out.printf(&quot;%-15s%-5s%-5s%-5s%-5s%-5s%-10s&quot;,
&quot;Countries&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;Average&quot;);
System.out.println();
for (int i = 0; i &lt; Countries.length; i++){
System.out.print(Countries[i]);
System.out.print(&quot;    &quot;);
missUniverse[i].printInfo();
System.out.printf(&quot;%5.1f&quot;, alist.get(i).average() );  ///this line 
System.out.println();
}
System.out.println(&quot;The result is&quot;);
System.out.println(&quot;Winner: Miss &quot; + getWinner(alist) );
System.out.println(&quot;1st Runner Up: Miss &quot; + firstRunnerUp(alist) );
}
}

MissUniverse class:

class MissUniverse { 
public static int SIZE=5;
private String country;
private double[] score;  
public MissUniverse(String country){
this.country = country;
score = new double [SIZE];
}
public String getCountry(){
return country;
}
public void getScore(){
for (int i=0;i&lt;score.length;i++)
{
score[i] = Math.random()*10;
}
}
public void setCountry(String newCountry) {
this.country = newCountry;
}
private int highest(){
int i;
double highest;
highest = score[0];
for (i = 0; i &lt;score.length; i++)
{
if (highest &lt; score [i])
{
highest = score[i] ;
}
}
return i;
}
private int lowest(){      
int i;
double lowest;
lowest = score[0];
for (i = 0; i &lt;score.length; i++)
{
if (lowest &gt; score [i])
{
lowest = score[i];
}
}
return i;  
}
public double average(){
double sum = 0.0;
for (int i = 0; i &lt; score.length; i++){
sum +=  score[i];
}
double average;
average = (sum - score[lowest()] - score[highest()] )/ score.length -2;
}
public void printInfo(){
for(int i=0;i&lt; score.length;i++){  
System.out.printf(&quot;%-5.1f&quot;,score[i]);
}
}
}

The error:

Countries      1    2    3    4    5    Average   
UK    3.3  5.8  6.6  4.8  0.2  Exception in thread &quot;main&quot; java.lang.ArrayIndexOutOfBoundsException: 5
at MissUniverse.average(Main.java:121)
at Main.main(Main.java:50)

I think the problem is in the average() or lowest() or highest() methods but I cant find the bug. I dont think there is anything wrong in those methods that can lead to counting outside the array length (length is 8) and index is 0-7. I've looked at other links in stackoverflow but I can't find whats wrong. I hope someone can help me?

The output I am trying to achieve is:

Countries       1    2    3    4    5   Average   
UK             6.2  2.5  9.7  4.3  0.1  3.4
Italy          7.1  1.2  1.6  5.5  1.9  3.7
Germany        8.5  3.3  6.7  4.0  6.0  5.9
Thailand       5.3  8.5  1.5 10.0  2.7  4.1
Denmark        3.1  4.9  5.4  0.8  3.2  3.4
The result is:
Winner: Miss Germany
1st runner up: Miss Thailand

答案1

得分: 1

问题实际上出现在你的 average 方法中。这一行:

average = (sum - score[lowest()] - score[highest()] )/ score.length -2;

需要更改为:

average = (sum - lowest() - highest() )/ score.length -2;

请记住,在数组中所有索引都从 0 开始。

你还忘记了返回一个值,所以你需要在 average 方法的末尾加上 return average;。同时,在你的 getWinner 方法中,你需要通过在 getWinner 方法的开头加上 int winner = 0; 来声明变量 winner

highest() 也需要更改为 return highest;,而 lowest() 需要更改为 return lowest;。你需要将这些返回类型更改为 double 才能使其正常工作。

英文:

The problem is actually in your average method. This line:

average = (sum - score[lowest()] - score[highest()] )/ score.length -2;

Needs to be changed to:

average = (sum - lowest() - highest() )/ score.length -2;

Remember that all indices start at 0 in arrays.

You also forgot to return a value, so you need to put return average; at the end of the average method. Also, in your getWinner method, you need to declare the variable winner by putting int winner = 0; at the beginning of the getWinner method.

highest() also needs to be edited to return highest;, and lowest() needs to be edited to return lowest;. You will need to change those return types to double to make this work.

huangapple
  • 本文由 发表于 2020年5月29日 11:59:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/62078481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定