|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
public static void main(String[] args) {
Person jiejie=new Person();
jiejie.shunxun();
}
public static void shunxun(){
System.out.println("请输入4个数:");
Scanner in_t1 = new Scanner(System.in);//循环输入数组
for(int i=0;i<number;i++){
t1[i]=in_t1.nextInt();}
for (int i = 0; i < t1.length; i++) {
int pos = i;
for (int j = i + 1; j < t1.length; j++) {
if (t1[pos] > t1[j])
pos = j;
}
if (pos != i) {
t1[i] = t1[i] + t1[pos];
t1[pos] = t1[i] - t1[pos];
t1[i] = t1[i] - t1[pos];
}
}
for (int i = t1.length - 1; i >= 0; i--)
System.out.print(t1[i] + "\t");
}
}
[b]存放4个元素, 然后颠倒我没看懂这里的代码::[/b
if (pos != i) {
t1[i] = t1[i] + t1[pos];
t1[pos] = t1[i] - t1[pos];
t1[i] = t1[i] - t1[pos]; // 各位强大的鱼油,,助助力啊!
// 方法二: 这是我自己编译的 但是 不可以的到想要的功能。。。鱼油们,靠你们了 自学有点难啊 感觉!
import java.util.Scanner;
public class Leiyu {
static int number = 4;
static int [] array= new int[number];
public static void main(String[] args) {
// TODO Auto-generated method stub
Leiyu.order(array);
}
public static void order(int[] array) {
System.out.println("请输入4个数:");
Scanner sc = new Scanner(System.in);
for(int i = 0; i < array.length; i++) {
array[i] = sc.nextInt();
for(i = 0; i < array.length/2; i++) {
if(array[i] < array[i+1]) {
int temp = array[i];
array[i] = array[array.length-i-1];
array[array.length-i-1] = temp;
}
}
}
System.out.println(array);
}
public static void printorder(int[] array) {
for (int i = array.length - 1; i >= 0; i--)
System.out.print(array + "\t");
}
}
|
|