0

好的,所以我正在制作一个冒泡排序程序来确保数组是有序的,我现在已经很好地编译了第一个类,但是第二个一直给我带来问题。现在它说它是错误的类型,所以我将变量 dif 更改为 int 而不是 double 但是它说可能会丢失精度。这是第一个文件的代码。

public class BubbleSort
{
    public static void sort(int[] a, int numberUsed)
    {
        int index;
        for ( int i =0; i<numberUsed; i++)
        {
            for (index = 0; index<a.length - 1; index++)
            {
                if (a[index]> a[index + 1])
                {
                    interchange(index, index + 1, a);
                } //end of if ()
            } //end of for ()
        } //end of for ()
    }
        private static void interchange(int i, int j, int[] a)
        {
            int temp1;
            temp1 = a[i];
            a[i] = a[j];
            a[j] = temp1;
        }
    }

这是给我问题的第二个文件

import java.util.Scanner;
public class GolfScores
{
    public static double[] diff = new double[5];
    public static void printOutArray(double[] a)
    {
        System.out.println("Sorted array values:");
        for (int i=0; i<a.length; i++)
        {
            System.out.printf("%2.2f",a[i]);
        } //end of for loop
        System.out.println();
        double Handicap= (diff[0])/1*0.96;
        System.out.printf("Handicap: %2.2f",Handicap);
        System.out.println();
    }
    public static void main(String[]args)
    {
        //construct and declare three arrays
        Scanner keyboard = new Scanner(System.in);
        double[] rating = new double[5];
        double[] slope = new double[5];
        double[] score = new double[5];
        int numberUsed = 0;
        int index = 0;

        //Print out directions for the user
        System.out.println("Calculate handicap for 5 games of golf.  This program takes Scores, \nCourse Rating and Slope rating for 5 games. \n\nUsing those figures, the program calculates the differential\nfor each round entered using this formula:(Score - Course Rating) x113 / Slope Rating.  \n\nThen uses the lowest differential to calculate handicap");
        System.out.println();
        //A do while loop that runs until index is great than 5
        do
        {
            System.out.print("Enter golf scores for game " +(index+1)+ ": ");
            score[index]= keyboard.nextDouble();
            System.out.print("Course rating for game "+(index+1)+ ": ");
            rating[index] = keyboard.nextDouble();
            System.out.print("Course slope for game "+(index+1)+": ");
            slope[index] = keyboard.nextDouble();
            index++;
            //index is the number of array indexed variables used so far
        } while((index<5));
        //this formula for all 5 arrays (Score- Course Rating) x 113 / Slope Rating
        diff[0]=((score[0]- rating[0]) * 113 / slope [0]);
        diff[1]=((score[1]-rating[1])*113/slope[1]);
        diff[2]=((score[2]-rating[2])*113/slope[2]);
        diff[3]=((score[3]-rating[3])*113/slope[3]);
        diff[4]=((score[4]-rating[4])*113/slope[4]);

        BubbleSort.sort(diff, diff.length);//passes value of diff array to BubbleSort.sort
        printOutArray(diff);//prints out diff array
    }
}
4

2 回答 2

0

您的冒泡排序仅适用于 int[],因此 diff 必须为 int[]。
我认为“可能的精度损失”来自

    diff[0]=((score[0]- rating[0]) * 113 / slope [0]);
    diff[1]=((score[1]-rating[1])*113/slope[1]);
    diff[2]=((score[2]-rating[2])*113/slope[2]);
    diff[3]=((score[3]-rating[3])*113/slope[3]);
    diff[4]=((score[4]-rating[4])*113/slope[4]);

因为:

    double[] rating = new double[5];
    double[] slope = new double[5];
    double[] score = new double[5];

这些数组被定义为 double[]。


你需要什么数据类型取决于你的输入是什么。
如果输入只不过是 int,则使用 int。如果可能出现浮点数,则应考虑使用双精度数。

于 2011-03-30T02:08:10.193 回答
0

您需要确保数据类型匹配,如前所述,您的 BubbleSort 类仅在 int 数组上运行,这是修改为使用双数组(这是 diff 包含的内容)的代码。

当您将 dif 更改为 int 并收到“精度损失”消息时,这是因为您正在执行除法(GolfScores.java 中的第 43-47 行),然后将结果分配给 int(丢弃小数值)。

这是 BubbleSort 的一个版本,它编译时没有错误或警告。我所做的更改(其中有 3 个)在注释中注明:

public class BubbleSort
{
    public static void sort(double[] a, int numberUsed) //change here
    {
        int index;
        for ( int i =0; i<numberUsed; i++)
        {
            for (index = 0; index<a.length - 1; index++)
            {
                if (a[index]> a[index + 1])
                {
                    interchange(index, index + 1, a);
                } //end of if ()
            } //end of for ()
        } //end of for ()
    }
        private static void interchange(int i, int j, double[] a) //change here
        {
            double temp1; //change here, important
            temp1 = a[i];
            a[i] = a[j];
            a[j] = temp1;
        }
    }

通过使用对象而不是原始类型(例如,对 Object 数组而不是 double 进行排序),并且可能使用用户指定的函数来比较元素,可以使代码更加灵活,这样您就不需要更改类处理不同的数据类型。也就是说,如果您要使用更多 Java 工具和标准库,您不妨使用 Arrays.sort 方法。这个页面比我更好地解释了内置排序方法的使用(以及为什么你不应该编写自己的排序例程,除非它是某种学术作业)。

于 2011-03-30T02:12:22.997 回答