所以,我试图自己学习JAVA并且遇到臭名昭着的非静态变量,这不能从静态上下文中引用#34;错误。
我知道这对你们重复,但我不明白为什么会这样......
public class JavaApplication1 {
public static void main(String[] args) {
/** Error comes from line below */
Anagram Pay = new Anagram(3,"PAYPAL");
System.out.println(Pay.horzArray);
}
public class Anagram{
private int vertArray;
private int horzArray;
private String starterWord;
private int wordLength;
private int diagonalMax;
public Anagram(int verticalArray, String inputWord){
vertArray = verticalArray;
starterWord = inputWord;
wordLength = starterWord.length();
if (vertArray > 2)
diagonalMax = vertArray - 2;
else
diagonalMax = 0;
horzArray = wordReducer(wordLength);
}
public int wordReducer(int word){
int step = 1;
int horzValue = 0;
while (word > 0)
if (step == 1) {
word = word - vertArray;
step = step + 1;
horzValue = horzValue + 1;
} else {
word = word - diagonalMax;
step = 1;
horzValue = horzValue + diagonalMax;
}
return horzValue;
}
}
};
(P.S如果有人好奇 - 我正在回答这个问题:https://leetcode.com/problems/zigzag-conversion/description/)