我有一个值要在两个类之间传递,但是我收到以下错误:
令牌“;”上的语法错误,在初始化被调用值时需要 来自另一个班级
我有初始值的类
public class Game extends Activity implements OnClickListener{
RandomMathQuestionGenerator question = new RandomMathQuestionGenerator();
private static final String TAG= "Sudoku";
public static final String KEY_DIFFICULTY =
"org.example.sudoku.difficulty";
public static final int DIFFICULTY_NOVICE = 1;
public static final int DIFFICULTY_EASY = 2;
public static final int DIFFICULTY_MEDIUM = 3;
public static final int DIFFICULTY_GURU = 4;
public int value = 0;
private GameView gameView;
public Game()
{
if (KEY_DIFFICULTY == String.valueOf(1))
{
value = 2;
}
else if (KEY_DIFFICULTY == String.valueOf(2))
{
value = 3;
}
else if (KEY_DIFFICULTY == String.valueOf(3))
{
value = 4;
}
else if (KEY_DIFFICULTY == String.valueOf(4))
{
value = 6;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
}
@Override
public void onClick(View v)
{
//code here
}
}
我称之为值的类
public class RandomMathQuestionGenerator {
Game num = new Game();
int number = 0;
number = num.Game(value);
//public int number = 0;
//number = num.Game(number);
private static final int NUMBER_OF_QUESTIONS = 1;
private static final int MIN_QUESTION_ELEMENTS = 2;
private final int MAX_QUESTION_ELEMENTS = number;
private static final int MIN_QUESTION_ELEMENT_VALUE = 1;
private static final int MAX_QUESTION_ELEMENT_VALUE = 100;
private final Random randomGenerator = new Random();
//rest of irrelevant code below
}
答案 0 :(得分:2)
从长远来看,语法错误无关紧要。
在Android中,您的必须使用Intent启动活动。 (见this Developer's Guide article。)当你想开始使用游戏时:
Intent intent = new Intent(this, Game.class);
startActivity(intent);
另外,您将KEY_DIFFICULTY
声明为final
(不可更改):
public static final String KEY_DIFFICULTY = "org.example.sudoku.difficulty";
因此,在任何这些情况下,你的if-else块永远不会成立:
if (KEY_DIFFICULTY == String.valueOf(1))
和比较Java中的字符串必须使用equals()
,==
会产生不准确的结果。 (How do I compare strings in Java?)
如果您想将难度级别从一个Activity传递到另一个Activity,请使用Intent的附加内容,public
类变量或其他方法:How do I pass data between Activities in Android application?
<强>加成强>
您刚刚在问题中添加了更多代码,并且您具有循环逻辑。
- When you create RandomMathQuestionGenerator object, you will create a Game object
- When you create Game object, you will create a RandomMathQuestionGenerator object
- When you create RandomMathQuestionGenerator object, you will create a Game object
- When you create Game object, you will create a RandomMathQuestionGenerator object
- When you create RandomMathQuestionGenerator object, you will create a Game object
- When you create Game object, you will create a RandomMathQuestionGenerator object
- When you create RandomMathQuestionGenerator object, you will create a Game object
- When you create Game object, you will create a RandomMathQuestionGenerator object
- ...
只有在您的应用程序抛出StackOverflowException时才会停止。
答案 1 :(得分:0)
number = num.Game(value);
我在Game(int value)
课程中没有看到方法Game
。您需要在班级中创建此方法:
public int Game(int value){
//code
}
我也不确定你可以命名一个与你的同名同名的方法。我假设它在签名中有一个返回值,它是有效的,但无论如何让方法名与你的类相同是不好的做法。
答案 2 :(得分:0)
<init>
以下是我修改后的RandomMathQuestionGenerator
代码:
public class RandomMathQuestionGenerator {
private Game num;
private int number;
public RandomMathQuestionGenerator() {
num = new Game();
number = num.value;
// Existing code here
}
// Existing code here
}
Game num = new Game();
int number = num.value;
这是错的:
public Game
- public
是字段修饰符,而不是变量修饰符,在方法代码中间无效。请注意,final
是一个有效的修饰符 - 这意味着重新分配变量是编译错误。0
永远不会被使用gameInst
可行。(进行中)