为什么我的代码没有正确打印出最高分和第一名亚军?

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

why isnt my code printing out the highest and 1st runner up properly?

问题

Main:

public class Main {

  // ... (other code remains the same)

  public static String getWinner(ArrayList<MissUniverse> alist) {
    int winner = 0;
    double highestAverage = alist.get(0).average(); // Store the highest average
    for (int s = 1; s < alist.size(); s++) { // Start from index 1
      if (alist.get(s).average() > highestAverage) {
        winner = s;
        highestAverage = alist.get(s).average(); // Update the highest average
      }
    }
    return alist.get(winner).getCountry();
  }

  public static String firstRunnerUp(ArrayList<MissUniverse> alist) {
    int firstrunnerUp = 0;
    double highestAverage = alist.get(0).average(); // Store the highest average
    for (int s = 1; s < alist.size(); s++) { // Start from index 1
      if (alist.get(s).average() > highestAverage && alist.get(s).getCountry() != getWinner(alist)) {
        firstrunnerUp = s;
        highestAverage = alist.get(s).average(); // Update the highest average
      }
    }
    return alist.get(firstrunnerUp).getCountry();
  }

  public static void main(String[] args) {
    // ... (other code remains the same)
  }
}

The issue in the original code is that you are comparing the average of each contestant to the average of the first contestant in both getWinner and firstRunnerUp methods. This is why the results are incorrect. To fix this, you need to keep track of the highest average and compare the averages of all contestants to that highest average. The code above makes this correction in both methods.

英文:

Main:

public class Main {
private static String [] Countries={&quot;UK&quot;, &quot;Italy&quot;,
&quot;Germany&quot;, &quot;Thailand&quot;,&quot;Denmark&quot;,&quot;Japan&quot;,&quot;Spain&quot;,&quot;Argentina&quot;};
static double average[] = new double[8];
double[] results = new double[8];
public static String getWinner(ArrayList&lt;MissUniverse&gt;alist){
int winner = 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%-5s%-5s%-5s%-10s&quot;,
&quot;Countries&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;,&quot;Average&quot;);
System.out.println();
for (int i = 0; i &lt; Countries.length; i++){
System.out.printf(&quot;%-13s&quot;,Countries[i]);
missUniverse[i].printInfo();
System.out.printf(&quot;%5.1f&quot;, alist.get(i).average() );
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 MissUniverse { 
public static int SIZE=8;
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()-1] - score[highest()-1] )/ (score.length - 2);
return average;
}
public void printInfo(){
for(int i=0;i&lt; score.length;i++){  
System.out.printf(&quot;%-5.1f&quot;,score[i]);
}
}
}

Why is the output wrong?

output 1:

Countries      1    2    3    4    5    6    7    8    Average   
UK           2.0  0.5  3.7  2.0  5.2  7.0  9.2  5.8    4.0
Italy        3.9  3.8  2.4  5.1  5.7  1.0  6.7  6.9    3.6
Germany      7.0  0.5  8.4  2.4  6.3  4.4  8.8  7.1    5.1
Thailand     6.9  8.7  3.9  4.7  4.9  3.5  4.7  9.4    4.7
Denmark      0.5  0.9  3.4  3.3  6.3  10.0 7.0  6.6    4.1
Japan        4.2  2.1  5.9  9.4  9.8  2.3  4.2  9.7    4.7
Spain        6.5  4.0  6.5  3.6  8.5  8.5  9.2  9.4    6.2
Argentina    0.4  6.6  6.5  2.3  2.6  4.5  5.6  3.7    4.1
The result is
Winner: Miss Argentina
1st Runner Up: Miss Spain

its supposed to be winner: miss spain, runner up: miss germany.
but isnt the logic in getWinner(alist) and firstrunnerup(alist) correct?

output 2:

Countries      1    2    3    4    5    6    7    8    Average   
UK           0.5  9.2  2.8  1.3  7.0  9.6  6.9  6.9    5.1
Italy        4.3  7.6  2.3  3.3  8.6  9.3  7.0  3.1    6.5
Germany      2.2  0.4  6.1  4.4  5.8  3.1  5.5  9.1    3.1
Thailand     1.7  7.1  6.2  9.5  7.9  4.9  0.8  8.3    5.0
Denmark      1.6  1.6  2.5  6.5  8.8  5.8  3.7  1.2    4.9
Japan        7.4  1.5  5.6  1.6  3.1  5.8  3.2  3.4    4.1
Spain        0.2  0.9  0.3  7.2  1.5  7.8  7.3  8.6    2.7
Argentina    9.1  8.9  0.3  5.1  2.6  8.8  4.3  5.1    5.7
The result is
Winner: Miss Argentina
1st Runner Up: Miss Italy

its supposed to be:
winner:italy
runner up:argentina

can someone point me in the right direction?i think the mistake is supposed to be in the getwinner and firstrunnerup methods but i can't spot the mistake.

答案1

得分: 1

getWinner() 函数中将:

if (alist.get(s).average() > alist.get(0).average() ) {

修改为:

if (alist.get(s).average() > alist.get(winner).average() ) {

同样地,在 getFirstRunnerUp() 函数中将:

if (alist.get(s).average() > alist.get(0).average() && alist.get(s).getCountry() != getWinner(alist) ) {

修改为:

if (alist.get(s).average() > alist.get(firstRunnerUp).average() && alist.get(s).getCountry() != getWinner(alist) ) {
英文:

In getWinner() change

if (alist.get(s).average() &gt; alist.get(0).average() ) {

to

if (alist.get(s).average() &gt; alist.get(winner).average() ) {

Similarly in getFirstRunnerUp() change

if (alist.get(s).average() &gt; alist.get(0).average() &amp;&amp; alist.get(s).getCountry() != getWinner(alist) ) {

to

if (alist.get(s).average() &gt; alist.get(firstRunnerUp).average() &amp;&amp; alist.get(s).getCountry() != getWinner(alist) ) {

答案2

得分: 1

你在 getWinnerfirstRunnerUp 函数中比较了错误的变量。请将这些函数替换为以下代码:


public static String getWinner(ArrayList<MissUniverse> alist){
    int winner = 0;
    double max = 0;
    for (int s = 0; s < alist.size(); s++){
        if (alist.get(s).average() > max) {
            max = alist.get(s).average();
            winner = s;
        }
    }
    return alist.get(winner).getCountry();
}

public static String firstRunnerUp(ArrayList<MissUniverse> alist){
    int firstrunnerUp = 0;
    int winner = 0;
    double max = 0;
    for (int s = 0; s < alist.size(); s++){
        if (alist.get(s).average() > max) {
            firstrunnerUp = winner;
            winner = s;
            max = alist.get(s).average();
        }
    }
    return alist.get(firstrunnerUp).getCountry();
}
英文:

You are comparing wrong variables in getWinner and firstRunnerUp. Replace the functions with the following ones:


public static String getWinner(ArrayList&lt;MissUniverse&gt; alist){
    int winner = 0;
    double max = 0;
    for (int s = 0; s &lt; alist.size(); s++){
        if (alist.get(s).average() &gt; max) {
            max = alist.get(s).average();
            winner = s;
        }
    }
    return alist.get(winner).getCountry();
}

public static String firstRunnerUp(ArrayList&lt;MissUniverse&gt; alist){
    int firstrunnerUp = 0;
    double winner = 0;
    double max = 0;
    for (int s = 0; s &lt; alist.size(); s++){
        if (alist.get(s).average() &gt; max) {
            firstrunnerUp = winner;
            winner = s;
            max = alist.get(s).average();
        }
    }
    return alist.get(firstrunnerUp).getCountry();
}

答案3

得分: 0

问题出在比较条件。

对于获胜者的比较条件是这样的:alist.get(s).average() &gt; alist.get(0).average(),但应该是这样的:alist.get(s).average() &gt; alist.get(winner).average()。你应该与当前的获胜者进行比较。

对于第二名函数也是同样的,应该是这样的:alist.get(s).average() &gt; alist.get(firstrunnerUp).average() &amp;&amp; alist.get(s).getCountry() != getWinner(alist)

英文:

The problem is in the compare condition.

Your compare condition for the winner is this alist.get(s).average() &gt; alist.get(0).average() but it should be this alist.get(s).average() &gt; alist.get(winner).average(). You should compare with the current winner.

Same goes for runnerup function, it should be this alist.get(s).average() &gt; alist.get(firstrunnerUp).average() &amp;&amp; alist.get(s).getCountry() != getWinner(alist)

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

发表评论

匿名网友

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

确定