【发布时间】:2016-03-03 22:01:23
【问题描述】:
有人可以帮助我使用数组继续此代码吗?这是示例输出 输入元素数(2-10):5 元素[1]:-3 元素[2]:0 元素[3]:10 元素[4]:2 元素[5]:7
Max Value is: 10
Min Value is: -3
Ascending order: -3 0 2 7 10
Descending order: 10 7 2 0 -3
我只有这个代码。
import java.util.Scanner;
public class Array1 {
public static void main(String[] args) {
Scanner n = new Scanner(System. in );
int num[] = new int[];
int ne = 0;
int e = 0;
System.out.print("Enter Number of Elements(2-10): ");
ne = n.nextInt();
for (int x = 1; x <= ne; x++) {
System.out.print("Element[" + x + "]: ");
e = n.nextInt();
}
}
}
这个的输出是:
Enter number of element(2-10): 5
Element[1]: -3
Element[2]: 0
Element[3]: 10
Element[4]: 2
Element[5]: 7
那我不知道怎么弄到其他人了。
【问题讨论】:
-
您的代码存在一些缺陷。第一的。在您知道它将有多少项目之前,您不应该声明一个新的 int 数组。 (所以在
ne = n.nextInt();之后移动int num[] = new int[];并使用new int[ne]声明一个包含“ne”项的数组。然后在你的 for 循环中你应该填充该数组。