我必须创建一个程序,要求用户输入一系列数字,并将输入保存在数组中。然后稍后允许用户搜索数组中的数字并打印数组。我被卡住了,因为它不让我将输入存储在数组中。我也需要帮助打印阵列。存储在阵列和打印阵列
我正在学习一个Java类,我们使用的教科书根本没有任何帮助,有没有人对Java教科书有什么建议?
这是到目前为止我的代码:
import java.util.Scanner;
public class MyContainer {
private int [] values;
private int size;
public MyContainer() {
values = new int[50];
size=0;
}
public static void main(String [] args)
{
//load()
Scanner input = new Scanner(System.in);
double sum = 0;
System.out.print("Enter a positive number, negative number will end entry.");
double number = input.nextDouble();
int count = 0;
double average = 0;
int numofinput = 0;
while (number >= 0)
{
count++;
numofinput ++;
sum += number;
System.out.print("Enter a positive number, negative number will end entry.");
number = input.nextDouble();
}
sum = sum + number;
average = sum/numofinput;
System.out.print("The average of the numbers as of right now is "
+ average);
}
//search()
像'值[数] =数目;'在'while'回路应页码存储在数组中。如果没有,你可以扩展“不让我把输入存储在数组中”吗?为了打印数组,你可以考虑采用类似的方法来读取数字。由于您事先知道打印出多少个数字,因此您可以在教科书中查找“for”循环。 – Simon 2013-02-19 00:40:46
啊,我被缩进困惑了,并且错过了将数字读入'main()'这一点,这是静态的,因此'values'不能从那里访问。 – Simon 2013-02-19 02:07:41