【问题标题】: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
  • 你身边有一本关于 Java 的书,对吧?

标签: 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]

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 2020-05-28
      • 2022-06-27
      • 2023-04-07
      • 1970-01-01
      • 2014-07-17
      相关资源
      最近更新 更多