Я создаю метод, позволяющий «распечатать гистограмму», и я пытаюсь заставить его ссылаться на histogramData.length, чтобы иметь возможность цитировать и Стройте каждую строку, но она не распознает метод histogramData.length в методе.Ошибка компиляции: не удается найти символ array.length
У меня есть код в 2 отдельных файлах, один для основного и один для создания методов.
Main выглядит следующим образом:
import becker.robots.*;
public class DrawHistogram extends Object
{ public static void main(String[] args)
{ City Edmonds = new City(12, 12);
HistogramRobot drawingBot = new HistogramRobot(Edmonds, 1, 1, Direction.EAST, 1000);
HistogramPrinter histPrinter = new HistogramPrinter();
int [] histogramData = new int[7];
histogramData[0] = 3; // The first element holds 3
histogramData[1] = 5; // The second element holds 5
histogramData[2] = 1; // The third element holds 1
histogramData[3] = 0; // The fourth element holds 0
histogramData[4] = 4; // The fifth element holds 4
histogramData[5] = 2; // The sixth element holds 2
histogramData[6] = 1; // The seventh element holds 1
drawingBot.drawRow();
}
}
И мой файл метод выглядит
import becker.robots.*;
class HistogramRobot extends Robot
{
HistogramRobot(City c, int st, int ave, Direction dir, int num)
{
super(c, st, ave, dir, num);
}
public void drawRow()
{
for(int counter = 0; counter < histogramData.length; counter++)
{
if(histogramData[counter] == 0)
{
this.turnRight();
this.move();
this.turnLeft();
}
for(int histoDrop = 0; histoDrop < histogramData[counter]; histoDrop++)
{
this.putThing();
this.move();
}
this.turnAround();
for (int moves = 0; moves < histogramData[counter]; moves++)
{
this.move();
}
this.turnLeft();
this.move();
this.turnLeft();
}
}
public void turnRight()
{
this.turnLeft();
this.turnLeft();
this.turnLeft();
}
public void turnAround()
{
this.turnLeft();
this.turnLeft();
}
}
И ошибки я получаю для каждого упоминания "histogramData", включая "histogramData.length"
HistogramRobot.java:24: error: cannot find symbol
for(int histoDrop = 0; histoDrop < histogramData[counter]; histoDrop++)
^
symbol: variable histogramData
location: class HistogramRobot
В чем причина ошибки и как ее исправить?
// Извините! Я не мог заставить интервал во второй части работать в мою пользу, сообщите мне, если вам нужно разъяснение, чтобы ответить.
Ваша проблема не определить массив с именем histogramData в любом месте вашего класса HistogramRobot. Попробуйте передать его методу drawRow – JamesENL