사용자에게 20 개의 double 값을 입력하도록 요청하는 프로그램을 작성하려고합니다. 프로그램은 모든 입력의 합계를 더하고 평균을 계산할 것입니다. 그렇다면 합계, 평균, 5.00 미만의 값 및 계산 된 평균보다 큰 값을 표시해야합니다. 지금까지, 나는 그것을 할 수 있었다. 그러나 모든 요소를 내림차순으로 표시하라는 요청을 받았습니다. 우리는 버블 정렬을하기 위해 도입되었지만 다른 결과에 영향을 미치기 때문에 현재 코드에 배치 할 위치와 방법을 파악할 수 없습니다. 우리는 BufferedReader를 위해서만 사용할 수 있습니다. Java에서 버블 정렬 알고리즘을 어디에서 어떻게 배치 할 수 있습니까?
이
내가 지금까지 가지고 그 코드 :import java.io.*;
public class Prices {
public static void main(String[]args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
double [] price = new double [5];
double sum = 0, ave = 0, temp;
System.out.print("Enter a value that has a decimal point: ");
for(int i=0; i<price.length;i++)
//loop for accepting values for the array, adding values and getting the ave of the values
{
price[i] = Double.parseDouble(br.readLine());//holds the values of the input
sum += price[i];//adds all the input
ave = sum/5;//divides the computed sum by array size
}
// for printing the total sum & ave
System.out.println("The sum is = "+sum);
System.out.println("The average is = "+ave);
for(int i=0; i<price.length;i++){//loop for displaying values<5.00 and values>computed ave.
if (price[i]<5.00)
{
System.out.println("Values less than 5.00: "+ price[i]);
}
if (price[i]>ave)
{
System.out.println("Values greater than average: "+ price[i]);
}
}
}
}
배열의 모든 유효한 값을 입력 한 후에 정렬하십시오. 전의. system.out.println (sum is = ...) 코드 줄 바로 위 –