【发布时间】:2013-09-09 09:47:49
【问题描述】:
在 main 方法中读取一个包含 5 个整数值的数组。创建一个单独的函数,该函数将对数组进行排序并将数组返回给 main 方法并在那里打印。
到目前为止我已经完成了:
import java.util.*;
public class arraysort {
public static void main(String[]args) {
Scanner in = new Scanner (System.in);
System.out.println("Enter 5 integers: ");
int [] x = new int [5];
for (int i=0; i<5; i++) {
x[i] = in.nextInt();
}
}
public static int sortarray(int [] value) {
int max = value[0];
for (int i = 1; i < value.length; i++) {
//I am not sure after this point
//I just did the rest
int [] y = new int [5];
Scanner in = new Scanner (System.in);
value[i] = in.nextInt();
Arrays.sort(y);
System.out.println(y);
}
}
}
【问题讨论】:
-
你是因为这是家庭作业而自己写排序吗?
-
你知道方法是如何工作的吗?和返回类型?
-
我现在看到 Arrays.sort 实际上在代码中。我不明白你在做什么,你在 for 循环的每次迭代中创建一个新数组,并试图对一个只有 1 个初始化值的数组进行排序。
-
我认为你应该尝试阅读these tutorials。他们会在很多问题上为您提供帮助。
标签: java arrays sorting methods return