我一直试图解决这个问题,而且我对java的模块不太熟悉并且调用ref。有人可以帮助我使这个程序工作吗?这很令人困惑。它在java中通过引用传递有很多问题,以及我在那里的任何额外错误。我不断收到类似不兼容的错误,但找不到符号。
//This program is designed to have the user enter 10 golf scores, then display them in ascending order.
import java.util.Scanner;
public class SortedGolfScoresMT
{
public static void main(String[] args)
{
//Declares the size for the variable, which is 10
int SIZE = 10;
//Declares the array and utilize it with SIZE variable
int[] scores = new int[SIZE];
getScores(scores, SIZE);
insertionSort(scores, SIZE);
displayScores(scores, SIZE);
}
public static int[] getScores(int scores[], int SIZE)
{
// Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Declares the index as a counter
int index;
//Get the scores for each golf player
for ( index = 0; index <= SIZE - 1; index++)
{
System.out.print("Enter the golf scores for player " + (index + 1) + ": ");
scores[index] = keyboard.nextInt();
}
return scores[index];
}
public static int[] insertionSort(int array[], int SIZE)
{
int unsortedValue;
int scan;
int index;
for ( index = 1; index <= SIZE -1; index++)
{
unsortedValue = scores[index];
scan = index;
while (scan > 0 && array[scan - 1] < array[scan])
{
swapModule();
scan = scan - 1;
}
array[scan] = unsortedValue;
}
return array[scan];
}
public static int[] swapModule(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
return b;
}
public static int[] displayScores(int scores[], int SIZE)
{
int index;
System.out.print("The scores are now displayed: ");
for (index = 0; index <= SIZE - 1; index++)
{
System.out.print(scores[index]);
}
}
}
答案 0 :(得分:2)
我注意到的前三件事是:
在getScores方法中,您返回一个由索引变量的位置而不是数组设置的int值(顺便说一下,如果它起作用,它将抛出一个ArrayIndexOutOfBoundsException)
您在insertedSort方法中调用的swapModule()方法没有所需的参数
displayScores方法必须返回一个int [],但你没有返回任何内容。
同样,正如其他人所说,编译器会告诉你错误的原因。这些只是我通过查看代码看到的一些错误。希望它有所帮助!