Java 关于 sort和swap的篇程

来源:百度知道 编辑:UC知道 时间:2024/05/10 08:17:38
我想写一个program
大概是输入四组号码

X1=1 1 1 1 1 1 1 1
X2=2 2 2 2 2 2 2 2
X3=3 3 3 3 6 6 6 6
X4=4 4 4 4 7 7 7 7
然后计算每组的总和再来排列大到小(我只能做到这里)
但我排列时只能显示每组的总和
我想要的是可以显示组名的

X4=??
X3=??
X2=??
X1=??
下面是我的篇程和output

import java.util.*;

public class sorting {

static Scanner console = new Scanner(System.in);

public static void main(String[]args)
{ int []x=new int[5];
int i,j;
int []v=new int[8];

for (j=1;j<5;j++)
{System.out.println("Enter the number for x"+ j );

for (i=0;i<8;i++)
v[i]=console.nextInt();

x[j]=(v[0]+v[1])-(v[2]+v[3])+(v[4]+v[5])+(v[6]+v[7]);

}
for (j=1;j<5;j++){
System.out.println("x"+j+"=" + x[j]);
}
Arrays.sort(x);
System.out.println("Sorted by descending");
for(int l = x.length-1; l >=1; l --) {
System.out.println(x

在你的程序基础上帮你做了点修改,运行成功,你自己可以调试看看:
import java.util.*;

public class Test {
@SuppressWarnings("unchecked")
public static void main(String[]args)
{
Scanner console = new Scanner(System.in);

int [][] x =new int[4][10];

for (int j=0; j<4; j++)
{
System.out.println("Enter the number for x"+ j+1 );
x[j][0] += j+1;

for (int i=0;i<8;i++)
{
x[j][i+2]=console.nextInt();
x[j][1] += x[j][i+2];
}
}

for (int j=0;j<4;j++)
{
System.out.println("x"+ (j+1) +"=" + x[j][1]);
}

Arrays.sort(x,new Compare());

System.out.println("Sorted by descending");

for (int j=0;j<4;j++)
{
System.out.print("x"+ x[j][0] +"=" + x[j][1] + "num:");
for(int i = 0; i &