找不到符号变量k

时间:2015-02-19 19:58:13

标签: java

当我编译并运行它时,我得到"找不到符号变量k"但是当我定义符号变量时,它表示已经定义了它。为什么是这样? {

private int numBars;    // number of bars to be displayed
int barHeight[];        // array of bar heights
int swapDelay;          // The delay between the original display and the second display.
int bar1, bar2;         // Indexes of 2 bars to be swapped.

public void init()
{
    numBars   = enterIntGUI("How many bars will be displayed?  {1-1000}");
    bar1      = enterIntGUI("What is the index of the 1st bar to be swapped?  {0-"+(numBars-1)+"}");
    bar2      = enterIntGUI("What is the index of the 2nd bar to be swapped?  {0-"+(numBars-1)+"}");
    swapDelay = 3000;
    barHeight = new int[numBars];
    getBarValues();     
}

public void getBarValues()
{     
  int list[] = new int[20];
    list[k] = Expo.random(10,640);
    for (int k = 1; k < 20; k++)
    System.out.println("list[" + k + "] = " + list[k]);


}

2 个答案:

答案 0 :(得分:3)

在定义之前使用

k。也许你想要

for (int k = 1; k < 20; k++) {
    list[k] = Expo.random(10,640);
    ...
}

答案 1 :(得分:0)

这种情况正在发生,因为您在定义k之前尝试访问public void getBarValues() { int list[] = new int[20]; for (int k = 1; k < 20; k++) { list[k] = Expo.random(10,640); System.out.println("list[" + k + "] = " + list[k]); } } 或具有值。

你需要这样修改它:

{{1}}