输入10个整数,放入一个数组中。并找出数组中最大值和最小值,以及他们的下标列表,输出要求:最大值 最大值下标列表;最小值 最小值下标列表。
package Main
;
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner in
= new Scanner(System
.in
);
while(in
.hasNextInt()){
int arr
[]=new int[10];
int temp
;
int b
[]=new int [10];
for(int i
=0;i
<10;i
++)
{
arr
[i
]=in
.nextInt();
}
for(int i
=0;i
<10;i
++)
b
[i
]=arr
[i
];
for(int i
=0;i
<10;i
++)
{
for(int j
=0;j
<9-i
;j
++)
{
if(arr
[j
]>arr
[j
+1])
{
temp
=arr
[j
];
arr
[j
]=arr
[j
+1];
arr
[j
+1]=temp
;
}
}
}
int x
[]=new int[10];
int u
=0;
for(int i
=0;i
<10;i
++)
{
if(arr
[9]==b
[i
])
{
x
[u
]=i
;
u
++;
}
}
System
.out
.printf("Max:"+arr
[9]+" at position:");
for(int i
=0;i
<u
;i
++)
System
.out
.printf("%d ",x
[i
]);
System
.out
.printf("\n");
int y
[]=new int[10];
int z
=0;
for(int i
=0;i
<10;i
++)
{
if(arr
[0]==b
[i
])
{
y
[z
]=i
;
z
++;
}
}
System
.out
.printf("Min:"+arr
[0]+" at position:");
for(int i
=0;i
<z
;i
++)
System
.out
.printf("%d ",y
[i
]);
System
.out
.printf("\n");
}
in
.close();
}
}
固定给你10个数,请你设计3个方法分别来计算10个值得最小值,最大值,均值 方法如下 1.public static double min(double[] array) 2.public static double max(double[] array) 3.public static double average(double[] array) 输出要求:先后输出最小值,最大值,平均值,之间用换行隔开,最后有换行! 每个结果统一保留两位小数!
import java
.util
.Scanner
;
public class array {
public static void main(String
[] args
) {
Scanner in
=new Scanner(System
.in
);
while(in
.hasNext()) {
double[] arr
=new double [10];
for(int i
=0;i
<arr
.length
;i
++)
arr
[i
]=in
.nextDouble();
System
.out
.printf("%.2f",min(arr
) );
System
.out
.printf("\n");
System
.out
.printf("%.2f",max(arr
));
System
.out
.printf("\n");
System
.out
.printf("%.2f",average(arr
));
System
.out
.printf("\n");
}
}
public static double max(double[] arr
){
double max
=arr
[0];
for(int i
=0;i
<arr
.length
;i
++){
if(arr
[i
]>max
){
max
=arr
[i
];
}
}
return max
;
}
public static double average(double[] arr
) {
double b
=0,average
=0;
for(double i
:arr
)
{b
+=i
;}
average
=b
/arr
.length
;
return average
;
}
public static double min(double[] arr
){
double min
=arr
[0];
for(int i
=0;i
<arr
.length
;i
++){
if(arr
[i
]<min
){
min
=arr
[i
];
}
}
return min
;
}
}
给你一组整数型数字,让你来统计里面每个数字出现的次数,输出要求:每一行输出每个数字的出现次数
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
java
.util
.Scanner input
=new java.util.Scanner (System
.in
);
while(input
.hasNextLine())
{
int [] a
= new int[122];
int [] b
= new int[122];
for(int i
=0;;i
++)
{
a
[i
] = input
.nextInt();
if(a
[i
] == 0)
break;
b
[a
[i
]]++;
}
for(int i
=0;i
<122;i
++)
{
if(b
[i
] == 1)
System
.out
.println(i
+ " occurs "+ b
[i
] +" time");
else if(b
[i
] > 1)
System
.out
.println(i
+ " occurs "+ b
[i
] +" times");
}
}
}
}
将数组的元素除第后一个元素外,所有元素后移一位,最后一位元素移动到第一位。 按格式输出源数组(输入的数据)和后移后的数组 参考方法: /**
最后一名到第一位,其他的都后退一位@param a@return */ public static int[] shiftBackward(int[] a)
/**
按格式输出一个数组@param a 输出的数组@param numberOfRow 每行元素个数@param spliter 分隔字符 */ public static void printArray(int[] a , int numberOfRow,String spliter )
输出要求:原数组每行输出5个元素,并且以字符#分隔 后一位的数组每行输出4个元素,并且以*分隔
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner sc
= new Scanner(System
.in
);
while(sc
.hasNextInt())
{
int a
[]=new int[10];
int b
[]=new int[10];
for(int i
=0;i
<10;i
++)
{
a
[i
]=sc
.nextInt();
if(i
==4)
System
.out
.println(a
[i
]);
else if(i
==9)
System
.out
.println(a
[i
]);
else
System
.out
.print(a
[i
]+"#");
}
int m
=0;
m
=a
[9];
a
[9]=a
[0];
for(int j
=0;j
<8;j
++)
{
a
[j
]=a
[j
+1];
}
a
[8]=m
;
System
.out
.print(a
[8]+"*");
System
.out
.print(a
[9]+"*");
for(int n
=0;n
<8;n
++)
{
if(n
==1)
System
.out
.println(a
[n
]);
else if(n
==5)
System
.out
.println(a
[n
]);
else if(n
==7)
System
.out
.println(a
[n
]);
else
System
.out
.print(a
[n
]+"*");
}
}
}
}