数组/排序,插入,查找,平均值,最值

来源:互联网 发布:知几是什么意思 编辑:程序博客网 时间:2024/06/10 20:56
package practice;import java.util.*;import static java.lang.System.*;/** * * @author Zhu Zhengyan */public class practice1 {    public static void main(String[] args)    {        out.println("The length of the array");        shuzu m = new shuzu();        out.print("They are");        m.show();        out.print("The average is:");        m.average();        m.zuizhi();        out.println("sort them");        m.paixu(2.0);        out.println("输入他们中的一个数求其位置");        m.search();        out.println("插入一个数到哪个位置");        out.println("插入哪个数");        m.charu();    }}class shuzu{    Scanner in = new Scanner(System.in);    private double []shu = new double[in.nextInt()];    private double a ;    private double s = 0;   public shuzu()    {        out.println("Please enter them");        for(int i = 0;i<shu.length;i++)        {            a = in.nextDouble();            shu[i] = a;        }    }    public void average()    {        for(int i = 0;i<shu.length;i++)        {           s += shu[i];        }        out.println(s/shu.length);    }    public void show()    {        String o = Arrays.toString(shu);        out.println(o);    }    public void zuizhi()    {        Arrays.sort(shu);        out.print("The max is :");        out.println(shu[shu.length-1]);        out.print("The min is :");        out.println(shu[0]);    }    public void paixu(double u)    {        double a[] = this.shu;        Arrays.sort(a);        out.println(Arrays.toString(a));    }    public void paixu(int u)    {        double a[] = this.shu;        Arrays.sort(a);        out.println(Arrays.toString(a));    }    public void search()    {        double u = in.nextDouble();        for(int i = 0;i<shu.length;i++)        {            if(shu[i] == u)                out.println("位置:"+i);        }    }    public void charu()    {        double t = 0;        int i = in.nextInt();        double x = in.nextDouble();        double newshu[] = Arrays.copyOf(this.shu,this.shu.length+1);        for(int j = newshu.length-1;j>=i;j--)        {            newshu[j] = newshu[j-1];        }        newshu[i] = x;        out.println("They are:");        out.println(Arrays.toString(newshu));    }}

阅读全文
0 0
原创粉丝点击