【问题标题】:How do I solve each compiling error in this program code(java)? [closed]如何解决此程序代码(java)中的每个编译错误? [关闭]
【发布时间】:2014-04-14 21:33:27
【问题描述】:

我一直在尝试解决这个问题,我对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]);
    }
}
}

【问题讨论】:

  • 编译器会告诉你准确哪一行哪一列是错的。请阅读它们并检查您做错了什么。
  • 我仍在尝试找出如何解决每个错误。我不知道从哪个方向调试编译器告诉我的错误。
  • 我给了你你想要的信息。你所要做的就是检查编译器的输出,它会告诉你哪一行有错误,在哪一列。阅读编译器输出,转到该行并检查您是否做错了什么
  • 发布您从编译器获得的一些错误甚至是一个好主意。
  • 其中一个错误显示“错误:不兼容的类型返回分数[索引]”,箭头指向索引左侧的第一个括号。第二个错误指​​出“找不到符号”关于“unsortedValue = scores[index]; 我认为第二个错误的问题之一是因为我没有完成第一个错误。但我尝试了很多方法并且还是不知道怎么解决第一个错误。

标签: java


【解决方案1】:

我注意到的前三件事是:

  • 在 getScores 方法中,您将返回一个由索引变量的位置设置的 int 值,而不是一个数组(顺便说一下,如果有效,它将抛出一个 ArrayIndexOutOfBoundsException)

  • 您在 insertSort 方法中调用的 swapModule() 方法没有所需的参数

  • displayScores 方法必须返回一个 int[],但您没有返回任何内容。

同样,正如其他人所说,编译器会告诉你哪里出了问题。这些只是我通过查看代码看到的一些错误。希望对您有所帮助!

【讨论】:

  • 你认为你能帮助我更多关于如何修复这些错误吗?我对 java 还是很陌生,我不明白应该如何编写 java 的结构。这也是我正在学习的第一语言。当您的意思是第一点时,您的意思是我不应该在数组“分数”中包含索引吗?我取消了索引并在请求 .class 的确切行上给出了错误,我认为这是我要去的错误方向。
猜你喜欢
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2014-06-11
  • 1970-01-01
  • 2021-10-01
相关资源
最近更新 更多