-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practice35.java
54 lines (48 loc) · 1.2 KB
/
Practice35.java
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
package fiftypratice;
import java.util.Arrays;
import java.util.Scanner;
/*
* 题目:输入数组,最大的与第一个元素交换,
* 最小的与最后一个元素交换,输出数组。
* */
/*此题解法有误,若最大值交换的第一个元素是最小值,则条件二无法满足;
* */
public class Practice35 {
public static void main(String[] args) {
System.out.println("请输入一个元素为5的数组:");
int a[]=new int[5];
Scanner s=new Scanner(System.in);
for(int i=0;i<a.length;i++){
a[i]=s.nextInt();
}
if(s!=null){
s.close();
}
int min=a[0];
int max=a[0];
int index1=0;
int index2=0;
for(int i=0;i<a.length;i++){
if(a[i]>=max){
max=a[i];
index1=i;
}
if(a[i]<=min){
min=a[i];
index2=i;
}
}
if(index1 != 0) {
int temp = a[0];
a[0] = a[index1];
a[index1] = temp;
}
if(index2 != a.length-1) {
int temp = a[a.length-1];
a[a.length-1] = a[index2];
a[index2] = temp;
}
String t= Arrays.toString(a);
System.out.println(t);
}
}