如何根据游戏级别用户点击更改数学测验难度? Android

How to change math quiz difficulty based on game level user clicks? Android

我正在创建一个简单的 android 数学游戏,其中向用户显示 4 个游戏级别。

我正在尝试弄清楚如何根据用户选择的级别更改数学问题的长度。我该怎么做?它会是一个 if 语句吗?所以如果用户选择硬,例如,它会显示 5 * 10 / 3.

我在游戏中实现了这个class(游戏class):

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;

这是困难的主要活动class

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>

我该怎么做 if 语句,或者根据用户点击的内容切换到更改问题的难度。每个游戏级别都会有不同长度的问题,但我不确定如何根据用户点击的级别来做

感谢任何帮助。谢谢

如果您使用文件存储问题:

使用 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);
}

如果您决定将所有问题存储为问题对象:

像这样创建一个问题class:

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、else 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 方法来更进一步。