JAVA调用方法,从键盘输入一个float型数组,长度5;…………
调用方法,从键盘输入一个float型数组,长度5;调用方法,用冒泡法给其排序,从大到小排列(不要打印排序中间过程);调用方法,求数组平均值,并输出;调用方法,用forea...
调用方法,从键盘输入一个float型数组,长度5;调用方法,用冒泡法给其排序,从大到小排列(不要打印排序中间过程);调用方法,求数组平均值,并输出;调用方法,用foreach循环遍历输出排序前后的数组。
展开
2个回答
展开全部
package com.bank.cn;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
float[] x = new float[5];
float sum=0;
for(int i=0;i<x.length;i++) {
x[i] = scanner.nextInt();
sum+=x[i];
}
System.out.println("平均数是:"+(float)(sum)/5);
System.out.println("排序前:");
System.out.println(x.length);
for(float k:x) {
System.out.print(k+"\t");
}
float[] x1 = new float[5];
x1 = sort(x);
System.out.println("\n排序后:");
for(float i:x1) {
System.out.print(i+"\t");
}
}
//排序
private static float[] sort(float[] x) {
for(int i=0;i<x.length;i++) {
for(int j=i;j<x.length;j++) {
if(x[i]<x[j]) {
float temp = x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
return x;
}
}
结果:
展开全部
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
float[] f=new float[5];
double [] d=new double[5];
System.out.println("给float数组赋值");
Scanner sc=new Scanner(System.in);
f[0]= new Float(sc.nextFloat());
f[1]= new Float(sc.nextFloat());
f[2]= new Float(sc.nextFloat());
f[3]= new Float(sc.nextFloat());
f[4]= new Float(sc.nextFloat());
System.out.println("遍历float数组");
for(float ff:f){
System.out.print(ff+"+++++");
}
System.out.println();
System.out.println("--------------------------------------------------------------------");
System.out.println("给double数组赋值");
Scanner sc1=new Scanner(System.in);
d[0]=new Double(sc1.nextDouble());
d[1]=new Double(sc1.nextDouble());
d[2]=new Double(sc1.nextDouble());
d[3]=new Double(sc1.nextDouble());
d[4]=new Double(sc1.nextDouble());
System.out.println("遍历doub
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
float[] f=new float[5];
double [] d=new double[5];
System.out.println("给float数组赋值");
Scanner sc=new Scanner(System.in);
f[0]= new Float(sc.nextFloat());
f[1]= new Float(sc.nextFloat());
f[2]= new Float(sc.nextFloat());
f[3]= new Float(sc.nextFloat());
f[4]= new Float(sc.nextFloat());
System.out.println("遍历float数组");
for(float ff:f){
System.out.print(ff+"+++++");
}
System.out.println();
System.out.println("--------------------------------------------------------------------");
System.out.println("给double数组赋值");
Scanner sc1=new Scanner(System.in);
d[0]=new Double(sc1.nextDouble());
d[1]=new Double(sc1.nextDouble());
d[2]=new Double(sc1.nextDouble());
d[3]=new Double(sc1.nextDouble());
d[4]=new Double(sc1.nextDouble());
System.out.println("遍历doub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询