日期:2014-05-20 浏览次数:20828 次
class Paixu {
public static void xu(int x[]) {
int i, j, temp;
// int x[]={a,b,c};
for (i = 0; i < 3; i++) // 循环3次;
{
for (j = 0; j < 3 - i; j++)
// 每次对比3-i次;
if (x[j] > x[j + 1]) {
temp = x[j];
x[j] = x[j + 1];
x[j + 1] = temp;
}
}
// System.out.println(m[0]+m[1]+m[2]);
}
}
class Daxiao {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int[] m = { a, b, c };
Paixu.xu(m);
System.out.println(m[0] + m[1] + m[2]);
//Paixu.paixu(a,b,c);
}
}
------解决方案--------------------
lz是本意是想冒泡排序吧?
在你的程序中,排序方法返回的是一个int类型的数,而在main主方法中,lz把返回的数赋值给int[]的数组,所以编译无法通过。
import java.util.Scanner;
class Paixu{
public static void xu (int[] x){
int i,j,temp;
for(i=0;i<x.length-1;i++) //循环x.length-1次
{
for(j=0;j<x.length-1-i;j++) //每次对比x.length-1-i次;
if (x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
}
public class Daxiao{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int[] m={a,b,c};
Paixu.xu(m);
int[] p=m; //这个步骤可以省略,直接用m[0], m[1], m[2]
System.out.println(m[0]+" " + m[1]+" "+ m[2]);
System.out.println(p[0]+" "+p[1]+" "+p[2]);//排序后的
System.out.println(p[0]+p[1]+p[2]);//和
//Paixu.paixu(a,b,c);
}
}