【问题标题】:Getting error setting variable in array?在数组中获取错误设置变量?
【发布时间】:2014-12-11 10:30:43
【问题描述】:
编译器出现错误“符号无法识别”,并且不确定我应该如何将数据存储在数组中。语言是 Java,这只是我的第一个 Java 程序,所以我正在测试一些东西。
class averageFunction{
public static void main(String args[]){
int numInput = 0;
int nummberIn[];
Boolean loopControl = false;
while(loopControl = true){
System.out.print("Please Enter Number, " + numInput + "have been entered...");
nummberIn(1) = 1;
};
};
};
【问题讨论】:
标签:
java
arrays
class
variables
【解决方案1】:
有很多错误:
class averageFunction{
public static void main(String args[]){
int numInput = 0;
int nummberIn[];//it is null, compiler won't allow to use it
Boolean loopControl = false;
while(loopControl = true){//not an error, but it possibly has to be (loopControl == true)
System.out.print("Please Enter Number, " + numInput + "have been entered...");
nummberIn(1) = 1;//<-- wrong, nummberIn[1] = 1;
};//<-- un-expected semi-colon
};//<-- un-expected semi-colon
};//<-- un-expected semi-colon
【解决方案2】:
您可以将赋值表达式更改为
numberIn[i] = 1;
为什么要使用数组,因为在您的数组中,所有情况下都只有 1 个元素。??
还有@arvind 显示编译时错误..
【解决方案3】:
如果你想在你的程序中使用一个数组,那么你首先需要定义它。
在你的程序中你写了int nummberIn[];。
这只是一个数组的声明。您还需要执行以下操作,
int nummberIn[] = new int[10];
这里10是数组的大小,这个数组中可以有10个整数值。
那么你需要通过它的索引来访问数组。
并且注意数组的索引总是从0开始。对于大小为 10 的数组,索引将为 0 to 9。
要访问任何单个元素,您需要执行以下操作,
numberIn[INDEX]
例如
numberIn[1]