Java:排序/搜索数组

时间:2014-04-20 19:05:02

标签: java algorithm sorting insertion-sort

我在使用另一个程序时遇到了一些麻烦(我真的对模块和数组不太好)。谁能帮助我让它正常工作?整晚都在努力,我非常累...无论如何,这是迄今为止我所做的代码。问题是:

  

设计一个程序,要求用户输入10个高尔夫分数。该   分数应存储在整数数组中。对数组进行排序   升序并显示其内容。

import java.util.Scanner;

public class SortedGolfScoresIB
{
    public static void main(String[] args)
    {
        //local variables
        final int SIZE = 10;
        int[] scores = new int[SIZE];

        // get the scores
        getScores(scores, SIZE);

        // sore the scores in ascending order
        insertionSort(scores, SIZE);

        //display the results in ascending order
        displayScores(scores, SIZE);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.

    public static void getScores(int scores[], int size)
    {
        // local variable for loop index
        int index;


        // get the scores
        for (index = 0; index <=size; size--)
        {
            System.out.println("Enter golf score number " + index +1 + ":");
            Scanner keyboard = new Scanner(System.in);
            scores[index] = keyboard.nextInt();
        }

        }
        // the insertionSort module sorts the contents of the  array
        // in ascending order
        public static void insertionSort(int  array[], int size)
        {
            //local variables
            int index;
            int scan;
            int unsortedValue;

            for (index = 1; index <=size; size--)
            {
                unsortedValue = array[index];
                scan = index;
            }

            while (scan > 0 && array[scan-1] < array [scan])
            {
                swap (array[scan-1], array[scan]);
                scan = scan -1;
            }

        array[scan] = unsortedValue;
        }

        //the swap module swaps the contents of its two arguments
        public static void swap(int a, int b)
        {
            int temp;
            //swap a and b
            temp = a;
            a = b;
            b = temp;
        }

        // the display scores module displays the
        // golve scores in the scores array
        public static void displayScores(int scores[], int size)
        {

            // local variable for loop index
            int index;

            //display the scores
            System.out.println ("here are the scores: ");
            for (index=0; index <= size; size--)
            {
                System.out.println(scores[index]);
            }
        }
    }

任何人都可以帮我解决这个问题吗?

Edit1:我更新了代码,这就是我现在所拥有的

import java.util.Scanner;

public class SortedGolfScoresIB
{
    public static void main(String[] args)
    {
        //local variables
        final int SIZE = 10;
        int[] scores = new int[SIZE];

        // get the scores
        getScores(scores, SIZE);

        // sore the scores in ascending order
        insertionSort(scores, SIZE);

        //display the results in ascending order
        displayScores(scores, SIZE);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.

    public static void getScores(int scores[], int size)
    {
        // local variable for loop index
        int index;


        // get the scores
        for (index = 0; index <=size; index++)
        {
            System.out.println("Enter golf score number " + (index +1) + ":");
            Scanner keyboard = new Scanner(System.in);
            scores[index] = keyboard.nextInt();
        }

        }
        // the insertionSort module sorts the contents of the  array
        // in ascending order
        public static void insertionSort(int  array[], int size)
        {
            //local variables
            int index;
            int scan;
            int unsortedValue;

            for (index = 1; index <=size; index++)
            {
                unsortedValue = array[index];
                scan = index;
                array[scan] = unsortedValue;

                }

            while (scan > 0 && array[scan-1] < array [scan])
            {
                swap (array[scan-1], array[scan]);
                scan = scan -1;
            }

        }

        //the swap module swaps the contents of its two arguments
        public static void swap(int a, int b)
        {
            int temp;
            //swap a and b
            temp = a;
            a = b;
            b = temp;
        }

        // the display scores module displays the
        // golve scores in the scores array
        public static void displayScores(int scores[], int size)
        {

            // local variable for loop index
            int index;

            //display the scores
            System.out.println ("here are the scores: ");
            for (index=0; index <= size; index++)
            {
                System.out.print(scores[index]);
            }
        }
    }

我现在得到的错误是&#34;扫描&#34;在我的while循环中没有初始化。

编辑2:

使程序正常工作并正确编译,但它不会像升序那样以最终顺序输出所有分数。

import java.util.Scanner;

public class SortedGolfScoresIB
{
    public static void main(String[] args)
    {
        //local variables
        final int SIZE = 10;
        int[] scores = new int[SIZE];

        // get the scores
        getScores(scores, SIZE);

        // sore the scores in ascending order
        insertionSort(scores, SIZE);

        //display the results in ascending order
        displayScores(scores, SIZE);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.

    public static void getScores(int scores[], int size)
    {
        // local variable for loop index
        int index;


        // get the scores
        for (index = 0; index <=size; index++)
        {
            System.out.println("Enter golf score number " + (index +1) + ":");
            Scanner keyboard = new Scanner(System.in);
            scores[index] = keyboard.nextInt();
        }

        }
        // the insertionSort module sorts the contents of the  array
        // in ascending order
        public static void insertionSort(int  array[], int size)
        {
            //local variables
            int index;
            int scan;
            int unsortedValue;

            for (index = 1; index <=size; index++)
            {
                unsortedValue = array[index];
                scan = index;

                array[scan] = unsortedValue;



            while (scan > 0 && array[scan-1] < array [scan])

                swap (array[scan-1], array[scan]);
                scan = scan -1;
            }

        }

        //the swap module swaps the contents of its two arguments
        public static void swap(int a, int b)
        {
            int temp;
            //swap a and b
            temp = a;
            a = b;
            b = temp;
        }

        // the display scores module displays the
        // golve scores in the scores array
        public static void displayScores(int scores[], int size)
        {

            // local variable for loop index
            int index;

            //display the scores
            System.out.println ("here are the scores: ");
            for (index=0; index <= size; index++)
            {
                System.out.print(scores[index]);
            }
        }
    }

代码编译和工作,但它不输出数组的内容。即时通讯错误。

编辑3:通过更改&#34; index&lt; = size&#34;修复了越界错误to&#34; index&lt;大小&#34 ;.但我现在得到的错误是我的代码在输入所有10个分数后停止,而不是像我之前提到的那样显示数组的内容。

import java.util.Scanner;

public class SortedGolfScoresIB
{
    public static void main(String[] args)
    {
        //local variables
        final int SIZE = 10;
        int[] scores = new int[SIZE];

        // get the scores
        getScores(scores, SIZE);

        // sore the scores in ascending order
        insertionSort(scores, SIZE);

        //display the results in ascending order
        displayScores(scores, SIZE);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.

    public static void getScores(int scores[], int size)
    {
        // local variable for loop index
        int index;


        // get the scores
        for (index = 0; index <=size; index++)
        {
            System.out.println("Enter golf score number " + (index +1) + ":");
            Scanner keyboard = new Scanner(System.in);
            scores[index] = keyboard.nextInt();
        }

        }
        // the insertionSort module sorts the contents of the  array
        // in ascending order
        public static void insertionSort(int  array[], int size)
        {
            //local variables
            int index;
            int scan;
            int unsortedValue;

            for (index = 1; index <=size; index++)
            {
                unsortedValue = array[index];
                scan = index;

                array[scan] = unsortedValue;



            while (scan > 0 && array[scan-1] < array [scan])

                swap (array[scan-1], array[scan]);
                scan = scan -1;
            }

        }

        //the swap module swaps the contents of its two arguments
        public static void swap(int a, int b)
        {
            int temp;
            //swap a and b
            temp = a;
            a = b;
            b = temp;
        }

        // the display scores module displays the
        // golve scores in the scores array
        public static void displayScores(int scores[], int size)
        {

            // local variable for loop index
            int index;

            //display the scores
            System.out.println ("here are the scores: ");
            for (index=0; index <= size; index++)
            {
                System.out.print(scores[index]);
            }
        }
    }

3,希望最后编辑的代码。最后一点可以帮助我吗?

5 个答案:

答案 0 :(得分:0)

好吧,通常我会发布此评论,因为我还不确定,你到底想要什么;然而,有这么多代码,我不能。

但是你正在寻找一个正是这样做的程序:

 import java.util.Scanner;
 import java.util.LinkedList;
 import java.util.Comparator;

 public class SortedGolfScoresIB {

     public static void main(String[] args) {
         //local variables
         final int SIZE = 10;
         LinkedList<Integer> scores;
         // get the scores
         scores = getScores(SIZE);
         // sore the scores in ascending order
         insertionSort(scores);
         //display the results in ascending order
         displayScores(scores);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.
    public static LinkedList<Integer> getScores(int size) {
        LinkedList<Integer> result = new LinkedList<Integer>();
        // get the scores
        for (int index = 0; index < size; index++) {
            System.out.println("Enter golf score number " + (index + 1) + ":");
            Scanner keyboard = new Scanner(System.in);
            result.add(keyboard.nextInt());
        }
        return result;
    }
    public static void insertionSort(LinkedList<Integer> array) {
         //local variables
         java.util.Collections.sort(array, new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }

        });
    }

    public static void displayScores(LinkedList<Integer> scores) {
        //display the scores
        System.out.println("here are the scores: ");
        for (Integer score: scores) {
            System.out.println(score);
        }
    }
}

答案 1 :(得分:0)

你没有在getScores()方法的for循环中增加索引,而是减小了大小,所以它不会正确地将元素读入分数数组,总是得分[0]只得到值,因此你的读数值不是顺便加入阵列。

最好将其从for (index = 0; index <=size; size--)更改为for (index = 0; index <size; index++) 请参阅以下工作计划并告诉我任何帮助

public class SortedGolfScoresIB
{
     public static void main(String[] args) {
         final int SIZE = 10;
            int[] scores = new int[SIZE];
            int[] input =getScores(scores, SIZE);
            insertionSort(input);
        }
     public static int[]  getScores(int scores[], int size)
        {
            // local variable for loop index
            int index;


            // get the scores
            for (index = 0; index <size; index++)
            {
                System.out.println("Enter golf score number " + index +1 + ":");
                Scanner keyboard = new Scanner(System.in);
                scores[index] = keyboard.nextInt();
            }
            return scores;

            }
        private static void printNumbers(int[] input) {

            for (int i = 0; i < input.length; i++) {
                System.out.print(input[i] + ", ");
            }
            System.out.println("\n");
        }

        public static void insertionSort(int array[]) {
            int n = array.length;
            for (int j = 1; j < n; j++) {
                int key = array[j];
                int i = j-1;
                while ( (i > -1) && ( array [i] > key ) ) {
                    array [i+1] = array [i];
                    i--;
                }
                array[i+1] = key;

            }
            printNumbers(array);
        }
}

答案 2 :(得分:0)

阅读问题,它自然分为四个部分:

  • 要求用户输入十个高尔夫分数。

  • 将十个分数存储在整数数组中。

  • 按升序对数组进行排序。

  • 显示已排序数组的内容。

您已经编写了一小段代码来执行这四个单独的任务。接下来需要做的是通过在每次转换时打印出数据的当前状态来检查您是否在四个代码片段中的每一个之间获得了预期。查看中间数据,并将其与您期望看到的数据进行比较。这将告诉你代码失败的地方。

首先尝试修复任何失败的部分。如果您仍然卡住,请在此处发布失败的代码并告诉我们您已尝试过的修复代码。否则,你会得到一些,&#34;你试过......&#34;你回复的建议,&#34;是的,我已经尝试过了。&#34;

您表现出的工作越多,我们就越有可能帮助您解决问题。

答案 3 :(得分:0)

程序中loops的所有内容都是从score数组中访问相同的索引。

将for循环:size--更改为index++,如下所示

for (index = 0; index <=size; index++) <br>
{
 //Your code
}

答案 4 :(得分:0)

我得到了我想要的代码!这是最终的代码

import java.util.Scanner;

public class SortedGolfScoresIB
{
    public static void main(String[] args)
    {
        //local variables
        final int SIZE = 10;
        int[] scores = new int[SIZE];

        // get the scores
        getScores(scores, SIZE);

        // sore the scores in ascending order
        insertionSort(scores, SIZE);

        //display the results in ascending order
        displayScores(scores, SIZE);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.

    public static void getScores(int scores[], int size)
    {
        // local variable for loop index
        int index;


        // get the scores
        for (index = 0; index < size; index++)
        {
            System.out.println("Enter golf score number " + (index +1) + ":");
            Scanner keyboard = new Scanner(System.in);
            scores[index] = keyboard.nextInt();
        }

        }
        // the insertionSort module sorts the contents of the  array
        // in ascending order
        public static void insertionSort(int  array[], int size)
        {
            //local variables
            int index;
            int scan;
            int unsortedValue;

            for (index = 1; index < size; index++)
            {
                unsortedValue = array[index];
                scan = index;

                array[scan] = unsortedValue;



            while (scan > 0 && array[scan-1] < array [scan])
                {
                swap(array[scan-1], array[scan]);
                scan = scan -1;
            }
        }

        }

        //the swap module swaps the contents of its two arguments
        public static void swap(int a, int b)
        {
            int temp;
            //swap a and b
            temp = a;
            a = b;
            b = temp;
        }

        // the display scores module displays the
        // golve scores in the scores array

        public static void displayScores(int scores[], int size)
        {

            // local variable for loop index
            int index;

            //display the scores
            System.out.println ("here are the scores: ");

            for (index=0; index < size; index++)
            {
                System.out.println(scores[index]);
            }
        }
    }

感谢大家的帮助。