gpt4 book ai didi

java - 初始化从另一个类调用的值时,预计 token ";"、 、 出现语法错误

转载 作者:行者123 更新时间:2023-12-02 07:13:46 27 4
gpt4 key购买 nike

我想在两个类之间传递一个值,但出现以下错误:

Syntax error on token ";", , expected when initializing called value from another class

我有初始值的类

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
}

最佳答案

从长远来看,语法错误是无关紧要的。

在 Android 中,您必须启动带有 Intent 的 Activity。 (参见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 block 都不会为 true:

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 时,这才会停止。

关于java - 初始化从另一个类调用的值时,预计 token ";"、 、 出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15210991/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com