更改数组值java

时间:2013-03-07 09:31:25

标签: java arrays list indexing

所以我在我的main调用的方法中设置了一个数组'generateUser'。但是,当我运行它时,它将索引的值替换为累积的i值。例如,不是每次例如dectile 7出现时,它只用i的当前值替换索引7。我看到了什么问题,但我不知道如何解决它。我认为问题在于

list [k] = ++ i; // HELP

public static int [] generateUser(int n)
{
    //pass number of students int n;
    int [] list = new int[10];
   int i=0;
    int total, counter, k;
    int score;
    String str3;
    total = counter =0;


   while (total < n)
   {

    str3 = JOptionPane.showInputDialog("Please enter the score: (1-100) ");
    score = Integer.parseInt(str3);
    System.out.print(str3+"\t");
        if (score <1 || score >100)
            {
                JOptionPane.showMessageDialog(null,"The data must be betwen 1 and      100.");
            }
      k = (score-1)/10;

      list[k]=++i;//HELP
      total = counter ++;
    }
      return list;

2 个答案:

答案 0 :(得分:2)

我不明白你在哪里设置i。你不是只想要:

 list[k]++;

增加在索引k找到的十分位数?

答案 1 :(得分:0)

我想要你喜欢的东西:

list[counter]=k;

因为你想在最后用 10 用户填写一个列表吗?

list[k]=++i;您没有设置索引。您可以在数组列表中将值设置为 k

相关问题