冒泡排序。我需要 double 还是 int ?
好的,所以我正在制作一个冒泡排序程序,以确保数组按顺序排列,我现在已经将第一个类编译得很好,但第二个类一直给我带来问题。现在它说它是错误的类型,所以我将变量 diff 更改为 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
}
}
OK So I am making a bubble sorting program to make sure arrays are in order and I have the first class compiled fine now but the second one keeps giving me problems. Right now it says it is the wrong type so I changed the variable dif to a int instead of a double but then it says possible loss of precision. Here is the code for the first file.
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;
}
}
and this is the second file that is giving me the problem
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的冒泡排序仅适用于 int[],因此 diff 必须是 int[]。
我认为“可能的精度损失”来自
因为:
这些数组被定义为 double[]。
您需要什么数据类型取决于您的输入是什么。
如果输入无非就是int,那就用int。如果可能出现浮点数,则应考虑double。
Your bubble sort only works on int[], thus the diff must be int[].
And I think the "possible loss of precision" comes from
Because:
Those arrays are defined as double[].
And What datatype you need depends on what your input will be.
If the input is nothing more than int, then use int. If floating-point number might appear, double should be considered.
您需要确保数据类型匹配,正如之前提到的,您的 BubbleSort 类仅对 int 数组进行操作,这是修改为使用双精度数组的代码(这是 diff 包含的内容)。
当您将 diff 更改为 int 并收到“精度丢失”消息时,这是因为您正在执行除法(GolfScores.java 中的第 43-47 行),然后将结果分配给 int(这会丢弃小数值)。
这是 BubbleSort 的一个版本,编译时没有错误或警告。我所做的更改(其中有 3 个)被注释为注释:
通过使用对象而不是基本类型(例如,对 Object 数组而不是 double 进行排序),并且可能使用用户指定的函数,可以使代码更加灵活比较元素,这样您就不需要更改类来处理不同的数据类型。也就是说,如果您要使用更多 Java 的工具和标准库,您不妨使用 Arrays.sort 方法。 此页面解释了内置排序方法的使用(以及为什么你不应该编写自己的排序例程除非它是某种学术作业)比我更好。
You need to make sure the data types match, as mentioned before your BubbleSort class operates only on an int array, this is the code as modified to work with double arrays (which is what diff contains).
When you changed dif to an int and received the "loss of precision" message, it's because you're doing division (lines 43-47 in GolfScores.java) and then assigning the result to an int (which discards fractional values).
Here is a version of BubbleSort that compiles without errors or warnings. Changes I made (there are 3 of them) are noted as comments:
The code could be made more flexible by using objects instead of primitive types (like, sort an array of Object instead of double), and perhaps using a user-specified function to compare elements, this way you wouldn't need to change the class to handle different data types. That said, if you're going to use many more of Java's facilities and standard libraries, you might as well go the Arrays.sort method. This page explains use of built-in sort methods (as well as why you shouldn't write your own sort routines unless it's some sort of academic assignments) better than I can.