好的,所以我正在制作一个冒泡排序程序来确保数组是有序的,我现在已经很好地编译了第一个类,但是第二个一直给我带来问题。现在它说它是错误的类型,所以我将变量 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
}
}