JAVA-数组

public class Average {
    private int[] data;
    private double mean;
public Average(){

}
    public Average(int[]data,double mean,int j) {
      this.mean=mean;

      System.out.println("you should input  score number"+(j+1));


      this.data=data;


    }
    public void selectionSort(int[] data) {
        int startScan, index, minIndex;
       int minValue;
        int n=data.length-1;
        for (startScan = 0; startScan < n; startScan++)
        {
            minIndex = startScan;
            minValue = data[startScan];
            for(index = startScan + 1; index < n+1; index++)
            {
                if (data[index] < minValue)
                {
                    minValue = data[index];
                    minIndex = index;
                }
            }
            data[minIndex] = data[startScan];
            data[startScan] = minValue;
        }
    }


    public void calculateMean(int[] data,double aver) {
        int sum = 0;
        int n=data.length;
        for (int i = 0; i < n; i++) {
            sum += data[i];
        }

         aver = sum / 5.0;
        System.out.println("the average number is:"+aver);

    }

    public String toString(int[] data) {
        selectionSort(data);
        double aver=0;
        calculateMean(data,aver);
      for(int i=0;i<5;i++){
          System.out.println(data[i]+" ");
      }
      return " ";
    }


}
public class Song {
    private String title;   // The song's title
    private String artist;  // The song's artist

    
public Song(){

}
    public Song(String title, String artist)
    {
        this.title = title;
        this.artist = artist;
    }
    public void set(String title,String artist){
    this.title=title;
    this.artist=artist;
    }
public String title(){
        return title;
}
public String artist(){
        return artist;
}
    /**
     The toString method
     @return A String object containing the name
     of the song and the artist.
     */

    public String toString(Song[]arr,int i){

        System.out.println(arr[i].title + " by " +arr[i].artist() + "\n");

        return " ";
    }
}
import java.io.*;


public class CompactDisc {
    public static void main(String[] args)throws IOException
    {
        FileReader file = new FileReader("Classics.txt");
        BufferedReader input = new BufferedReader(file);
        String title;
        String artist;
        final int size=6;
        Song[] cd=new Song[size];
        // ADD LINES FOR TASK #3 HERE
        // Declare an array of Song objects, called cd,
        // with a size of 6
        System.out.println("Contents of Classics:");
        for (int i = 0; i < size; i++) {
            title = input.readLine();
            artist = input.readLine();
            cd[i]= new Song();
             cd[i].set(title,artist);
            // ADD LINES FOR TASK #3 HERE
            // Fill the array by creating a new song with
            // the title and artist and storing it in the
            // appropriate position in the array



                cd[i].toString(cd,i);
                // ADD LINES FOR TASK #3 HERE
                // Print the contents of the array to the console

        }
    }
}
import java.util.Scanner;
public class  AverageDriver{
    public static void main(String[] args) {
        int[] aver = new int[5];
        double mean=0;
        for(int j=0;j<5;j++) {
            Average av1 = new Average(aver, mean, j);
            Scanner sc = new Scanner(System.in);
            aver[j] = sc.nextInt();
        }
        Average av1 = new Average();
          av1.toString(aver);

    }
//when I input 3,8,1,2,0.I algorith average is 2.8.
    //and sorted from max to min is 8,3,2,1,0

}
  • 13
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值