英文:
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={"UK", "Italy",
"Germany", "Thailand","Denmark","Japan","Spain","Argentina"};
static double average[] = new double[8];
double[] results = new double[8];
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(0).average() ) {
winner= s;
}
}
return alist.get(winner).getCountry();
}
public static String firstRunnerUp(ArrayList<MissUniverse>alist){
int firstrunnerUp =0;
for (int s = 0; s < alist.size(); s++){
if (alist.get(s).average() > alist.get(0).average() && alist.get(s).getCountry() != getWinner(alist) ) {
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%-5s%-5s%-5s%-10s",
"Countries","1","2","3","4","5","6","7","8","Average");
System.out.println();
for (int i = 0; i < Countries.length; i++){
System.out.printf("%-13s",Countries[i]);
missUniverse[i].printInfo();
System.out.printf("%5.1f", alist.get(i).average() );
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=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<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 <score.length; i++)
{
if (highest < score [i])
{
highest = score[i] ;
}
}
return i;
}
private int lowest(){
int i;
double lowest;
lowest = score[0];
for (i = 0; i <score.length; i++)
{
if (lowest > score [i])
{
lowest = score[i];
}
}
return i;
}
public double average(){
double sum = 0.0;
for (int i = 0; i < 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< score.length;i++){
System.out.printf("%-5.1f",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() > alist.get(0).average() ) {
to
if (alist.get(s).average() > alist.get(winner).average() ) {
Similarly in getFirstRunnerUp() change
if (alist.get(s).average() > alist.get(0).average() && alist.get(s).getCountry() != getWinner(alist) ) {
to
if (alist.get(s).average() > alist.get(firstRunnerUp).average() && alist.get(s).getCountry() != getWinner(alist) ) {
答案2
得分: 1
你在 getWinner
和 firstRunnerUp
函数中比较了错误的变量。请将这些函数替换为以下代码:
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<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;
double 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();
}
答案3
得分: 0
问题出在比较条件。
对于获胜者的比较条件是这样的:alist.get(s).average() > alist.get(0).average()
,但应该是这样的:alist.get(s).average() > alist.get(winner).average()
。你应该与当前的获胜者进行比较。
对于第二名函数也是同样的,应该是这样的:alist.get(s).average() > alist.get(firstrunnerUp).average() && 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() > alist.get(0).average()
but it should be this alist.get(s).average() > alist.get(winner).average()
. You should compare with the current winner.
Same goes for runnerup function, it should be this alist.get(s).average() > alist.get(firstrunnerUp).average() && alist.get(s).getCountry() != getWinner(alist)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论