我正在创建一个简单的Android数学游戏,其中向用户呈现4个游戏关卡。
我正在试图弄清楚如何根据用户选择的级别更改数学问题的长度。我该怎么做?这是一个if声明吗?因此,如果用户选择硬盘,它将显示5 * 10/3。
我已经在游戏类(游戏类)中实现了这个:
private static final String TAG = "math Game" ;
public static final String KEY_LEVEL = "org.example.math.level" ;
public static final int LEVEL_EASY = 0;
public static final int LEVEL_MEDIUM = 1;
public static final int LEVEL_HARD = 2;
这是困难的主要活动类
private static final String TAG = "Math Game" ;
private void openNewGameDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.new_game_title)
.setItems(R.array.level,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface
dialoginterface, int i) {
startGame(i);
}
})
.show();
}
private void startGame(int i) {
Log.d(TAG, "clicked on " + i);
Intent intent = new Intent(MainActivity.this, Game.class);
intent.putExtra(Game.KEY_LEVEL, i);
startActivity(intent);
}
这是array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="level">
<item>@string/easy_label</item>
<item>@string/medium_label</item>
<item>@string/hard_label</item>
</array>
</resources>
如果语句或者可能会根据用户点击的内容切换到更改问题的难度,我该怎么办?每个游戏关卡都会有不同的问题,但我不知道如何根据用户点击的级别进行操作
感谢任何帮助。感谢
答案 0 :(得分:0)
使用if..else if..else语句在Game.class中选择适当的文件
Intent intent = getIntent();
String level = intent.getStringExtra(Game.KEY_LEVEL);
File myFile;
if (Game.KEY.equals(getResources().getText(R.string.easy_label))) {
myFile = new File(insert here the path of the file that contains the easy questions);
} else if (Game.KEY.equals(getResources().getText(R.string.medium_label))) {
myFile = new File(insert here the path of the file that contains the medium questions);
} else {
myFile = new File(insert here the path of the file that contains the hard questions);
}
像这样创建一个Question类:
class Question {
private String questionText;
private String category;
// more vars
// Setters and Getters
public getARandomQuestion(String category) {
// write code here to return a random questions that has belongs to the Category category.
}
然后执行相同的if..else if..else循环,但这次是if的主体,否则if和else应该是不同的:
if (Game.KEY.equals(getResources().getText(R.string.easy_label))) {
Question theQuestion = new Question();
theQuestion.getARandomQuestion("easy");
} else if (Game.KEY.equals(getResources().getText(R.string.medium_label))) {
Question theQuestion = new Question();
theQuestion.getARandomQuestion("medium");
} else {
Question theQuestion = new Question();
theQuestion.getARandomQuestion("hard");
}
当然,通过创建类别枚举以及将类别而不是字符串传递给getARandomQuestion方法,可以更进一步。