因此,我在findSmallest
和findLargest
方法中收到错误,但是我理解该错误,只是不知道为什么会发生。我正在比较2D数组中的2个双打,但仍然告诉我不能将它们与'>'进行比较。任何帮助将不胜感激。您可以跳过大多数主要方法,直到出现问题的地方。
import java.util.Scanner;
public class DivingScores
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String[] judge = new String[7]; //
System.out.println("Enter judges' names:"); //
for(int x = 0; x<judge.length; x++) //
judge[x] = input.nextLine(); //
//
String[] diver = new String[4]; //
System.out.println("Enter divers' names:"); //
for(int x = 0; x<diver.length; x++) //
diver[x] = input.nextLine(); // Creating and Instantiating
//
double[] diveDifficulty = new double[7]; //
for(int x = 0; x<diveDifficulty.length; x++) //
diveDifficulty[x] = input.nextDouble(); //
//
double[][] diverScore = new double[4][7]; //
for(int x = 0; x<diverScore.length; x++) //
for(int y = 0; y<diverScore[x].length; y++) //
diverScore[x][y] = input.nextDouble(); //
System.out.println("Judges"); //
for(int x = 0; x<judge.length; x++) //
System.out.printf("%15s, ", judge[x]); //
System.out.println(); //
//
System.out.println("Divers"); //
for(int x = 0; x<diver.length; x++) //
System.out.printf("%13s, ",diver[x]); //
System.out.println(); //
//
System.out.println("Dive Difficulty"); // Printing
for(int x = 0; x<diveDifficulty.length; x++) //
System.out.printf("%2.2f, ", diveDifficulty[x]); //
System.out.println(); //
//
System.out.println("Diver Scores"); //
for(int x = 0; x<diverScore.length; x++) //
{ //
System.out.println(diver[x]); //
for(int y = 0; y<diverScore[x].length; y++) //
System.out.printf("%2.2f ", diverScore[x][y]); //
System.out.println(); //
}
findSmallest(diverScore);
findLargest(diverScore);
awardMedal(calcScore(diverScore, diveDifficulty, diver.length), diver);
}
public static void findSmallest(double[][] diverScore)
{
for(int x = 0; x<diverScore.length; x++)
{
int smallest = 0;
for(int y = 0; y<diverScore[x].length; y++)
{
if(y > 0 && diverScore[smallest] > diverScore[y]) //Error Here
smallest=y;
}
diverScore[x][smallest] = 0;
}
}
public static void findLargest(double[][] diverScore)
{
for(int x = 0; x<diverScore.length; x++)
{
int largest = 0;
for(int y = 0; y<diverScore[x].length; y++)
{
if(y > 0 && diverScore[largest] < diverScore[y]) //Error Here
largest=y;
}
diverScore[x][largest] = 0;
}
}
public static double[] calcScore(double[][] diverScore, double[] diveDifficulty, int divers)
{
double[] scores = new double[divers];
for(int x=0; x<divers; x++)
{
double total = 0;
for(int y=0;y<diverScore[x].length;y++)
total += diverScore[x][y]*diveDifficulty[y];
scores[x]=total;
}
return scores;
}
public static void awardMedal(double[] scores, String[] diver)
{
int first = 0;
int second = 0;
int third = 0;
for(int x = 0; x<scores.length; x++)
{
if(x!=0 && scores[first]<scores[x])
first = x;
}
System.out.println(diver[first] + scores[first] + " Gold");
scores[first]=0;
for(int x = 0; x<scores.length; x++)
{
if(x!=0 && scores[second]<scores[x])
second = x;
}
System.out.println(diver[second] + scores[second] + " Silver");
scores[second]=0;
for(int x = 0; x<scores.length; x++)
{
if(x!=0 && scores[third]<scores[x])
third = x;
}
System.out.println(diver[third] + scores[third] + " Bronze");
}
}
我不知道为什么在这种情况下会发生错误,但是还有其他方法可以完成我的工作吗?
答案 0 :(得分:2)
它是一个2D数组,所以在做的时候
diverScore[smallest] > diverScore[y]
您正在比较数组不加倍。
double[][] diverScore
根据您的输入
for(int x = 0; x<diverScore.length; x++) //
for(int y = 0; y<diverScore[x].length; y++) //
// here
您需要进行内循环