在数学游戏中显示难度

时间:2018-02-21 16:37:34

标签: java android

我必须显示难度级别,用户可以从中选择要播放的级别。这是一个Android工作室编程和我到目前为止,当用户按下新游戏时它只显示标题:难度但不显示级别。任何帮助将不胜感激。

public class Game extends Activity {
    public static final String KEY_DIFFICULTY ="com.example.arteaarbenmalo.braintraininggame.difficulty" ;
        if (Brainactivity.difficulty == 0){ // this calls difficulty level NOVICE I HAVE DECLARED IT AS SWITICH 0 this is getting it from brainactivity.java

                operationsrandom = operator[new Random().nextInt(4)]; //selects random operators from 4 expressions
                switch(operationsrandom)  //switch statement

                {

                    case '+': // do addition
                        Total = number1 + number2; //does the total for random number 1 and random number 2 the both numbers are random takes away
                        question.setText(String.valueOf(number1+" " + "+" +" "+number2+" =")); //puts the answer in here and displays the questions with the both random numbers
                        break;  //break

                    case '-': // does minus
                        Total = number1 - number2; //does the total for random number 1 and random number 2 the both numbers are random takes away
                        question.setText(String.valueOf(number1+" " + "-" +" "+number2+" =")); //puts the answer in here and displays the questions with the both random numbers
                        break;  //break

                    case '/': //does division
                        if(number1 == 0||number2==0){ //this is a if statement for when 2 numbers divide so the dont equal 0 adds 1 to random number
                            number1=number1+(1); //add 1 to this random number
                            number2=number2+(1);} // add1 to random number 2
                        Total = number1 / number2; //does the division
                        question.setText(String.valueOf(number1+" " + "/" +" "+number2+" =")); //puts the answer in here and displays the questions with the both random number
                        break; //beak


                    case '*': //this does multiplication
                        Total = number1 * number2; //multiply random number 1 by random number 2
                        question.setText(String.valueOf(number1+" " + "*" +" "+number2+" =")); //puts the answer in here and displays the questions with the both random number
                        break; //break

                }
            }

            if (Brainactivity.difficulty == 1) { // this calls difficulty level EASY I HAVE DECLARED IT AS SWITICH 1 this is getting it from brainactivity.java

                switch(operationsrandom ){ //Switch statement

                    case '+': // do addition
                        Total = number1 + number2 - number4 ; //plus random number 1 by random number 2 and minus random number 4
                        question.setText(String.valueOf(number1 + "+" + number2 + "-" + number4 + "=")); //displays all random numbers with required expressions random
                        break; //break

                    case '-': //do take away
                        Total = number1 - number2 * number3 ; //plus random number 1 by random number 2 and minus random number 3
                        question.setText(String.valueOf(number1 + "-" + number2 + "-"  + number3 + "=")); //displays all random numbers with required expressions random
                        break; //break

                    case '/': // do revision
                        if(number1 == 0||number2==0){ //if statement for random number 1 and number 2 if equals to 0 plus 1
                            number1=number1+(1);  //adds 1 to number1 if 0
                            number2=number2+(1);}   // adds 1 to number2
                        Total = number1 / number2; // two random numbers dividing
                        question.setText(String.valueOf(number1 + "/" +number2 + "=")); //displays question with 2 random numbers
                        break; //break

                    case '*': // does multiplication
                        Total = number1 * number2; //multiply the 2 random numbers
                        question.setText(String.valueOf(number1 + "*" + number2 + "=")); // displays the question with the 2 random numbers randomly
                        break; //break
                }
            }



            if (Brainactivity.difficulty == 2) { //this is calling difficulty level medium from brainactivity.java this is Switch case 2

                switch(operationsrandom){ // switch statement

                    case '+': // do addition
                        Total = number1 + number2 / number3; // gets random number1 adds it to random number2 and then divides random number3
                        question.setText(String.valueOf(number1 + "+" + number2 + "/" + number3 + "=")); //displays the question with the random numbers
                        break; //break

                    case '-': // do minus
                        if(number1 == 0||number2==0){ //if statement for random number 1 and number 2 if equals to 0 plus 1
                            number1=number1+(1);    //adds 1 to number1 if 0
                            number2=number2+(1);}   //adds 1 to number2 if 0
                        Total = number1 / number2 * number3 + number4; // doing a expression to all the random numbers declared
                        question.setText(String.valueOf(number1+"/"+number2+ "*" + number3+ "+" + number4+ "=")); //displays the question with the random numbers
                        break; //break

                    case '/': // do divide
                        if(number1 == 0||number2==0){ //if statement for random number 1 and number 2 if equals to 0 plus 1
                            number1=number1+(1); //adds 1 to number1 if 0
                            number2=number2+(1);} //adds 1 to number2 if 0
                        Total = number1 / number2; //Doing division for random number1 and random number2
                        question.setText(String.valueOf(number1+"/"+number2+" =")); //displays the questions
                        break; //break

                    case '*': // do multiplication
                        Total = number3 * number2 - number1 ; // getting number3 number2 number1 randomly doing required expression
                        question.setText(String.valueOf( number3 + "*" +number2 + "-" + number1 + "=")); //displays the question
                        break;  //break

                }
            }

            if (Brainactivity.difficulty == 3) { //this is calling difficulty level Guru from brainactivity.java this is Switch case 3

                switch(operationsrandom){ // Switch statement

                    case '+': // do addition
                        Total = number1 + number2 * number5 - number4; // getting number1 number2 number5 number4 randomly doing required expression
                        question.setText(String.valueOf(number1 + "+" +number2 + "*" + number5 + "-" + number4 + "=")); //displays the question
                        break; //break

                    case '-': // do minus
                        if(number1 == 0||number2==0){ //if statement for random number 1 and number 2 if equals to 0 plus 1
                            number1=number1+(1); //adds 1 to random number 1
                            number2=number2+(1);} //adds 1 to random number2
                        Total = number1 / number2 * number6 + number5 * number4; // getting number1 number2 number6 number5 number4 randomly and doing required expression
                        question.setText(String.valueOf(number1 + "/" + number2 + "*" + number6 + "+" + number5 + "*" + number4 + "=")); //displays question
                        break; //break

                    case '/': // do divide
                        if(number1 == 0||number2==0){ //if statement for random number 1 and number 2 if equals to 0 plus 1
                            number1=number1+(1); //adds 1 to random number 1
                            number2=number2+(1);} //adds 1 to random number 2
                        Total = number1 / number2 * number4 * number5 + number3 - number6; // getting number1 number2 number4 number5 number3 number6 randomly and doing required expression
                        question.setText(String.valueOf(number1 + "/" + number2 + "*" + number4 + "*" + number5 + "+" + number3 + "-" + number6 + "=")); //displays the question
                        break; //break

                    case '*': // do multiply
                        Total = number6 * number5 - number4 + number3 ; // getting number6 number5 number4 number3 randomly and doing required expression
                        question.setText(String.valueOf( number6 + "*" + number5 + "-" + number4 + "+" + number3 + "=")); //displays question
                        break; //break
                }
            }
        }
    });
}

这是我的大脑活动课

public class Brainactivity extends Activity implements OnClickListener {
    static int difficulty;  //static variable for difficulty

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brainactivity);

        View continueButton = findViewById(R.id.continue_button);   //displays continue
        continueButton.setOnClickListener(this);        //on click for the continue button
        View newButton = findViewById(R.id.new_button); //new button
        newButton.setOnClickListener(this);             // on click listener for this button
        View aboutButton = findViewById(R.id.about_button); // displays about button
        aboutButton.setOnClickListener(this);           // on click listener for this
        View exitButton = findViewById(R.id.exit_button); // exit button
        exitButton.setOnClickListener(this);        // exits application
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) { //implements menu
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.brainactivity, menu); // displays menu
        return true; //returns
    }


    public void onClick(View v) {                       //This is a method to get the about button on click listener
        switch (v.getId()) {                            //switch statement
            case R.id.about_button:                         // case id about button
                Intent i = new Intent(this, About.class);   // calls it from about class
                startActivity(i);                           //starts activity
                break;                                      //break
            case R.id.exit_button:                          //exit  button
                finish();                                   // finishes the app
                break;                                      //break
            case R.id.new_button:                           //new game button
                openNewGameDialog();                        //opens the game dialog
                break;                                      // break


        }
    }

    private static final String TAG = "BrainGame" ;         //the game name
    private void openNewGameDialog() {                      //opens new game
        new AlertDialog.Builder(this)                       // alert dialog builder
                .setTitle(R.string.new_game_title)                  // gets string from new game
                .setItems(R.array.difficulty,                       // sets items array difficulty
                        new DialogInterface.OnClickListener() {     // onclick listener
                            public void onClick(DialogInterface             //onclick listener
                                                        dialoginterface, int i) {               // dialog with the variable name
                                startGame(i);                               // starts game with i varible
                            }
                        })
                .show();                                        // shows starts
    }
    private void startGame(int i) {                 // method for to start game

        Log.d(TAG, "clicked on " + i);              // when clicked plus I

        switch (i){                             // switch statement for I

            case 0:                                                             // case 0 for novice
                Intent intent = new Intent(Brainactivity.this, Game.class);     //gets novice from game class
                startActivity(intent);                                          //starts activity novice
                difficulty =0;                                                  // novice class case 0
                break;                                                          //break

            case 1:                                                             // case 1 for easy
                Intent easy = new Intent(Brainactivity.this, Game.class);       //gets easy from game class
                startActivity(easy);                                            //starts activity easy
                difficulty =1;                                                  //easy class case 1
                break;                                                          //break

            case 2:                                                             // case 2 for medium
                Intent medium = new Intent(Brainactivity.this, Game.class);     //gets medium from game class
                startActivity(medium);                                          //starts activity medium
                difficulty =2;                                                  //medium class case 2
                break;                                                          //Break

            case 3:                                                             // case 3 for novice
                Intent guru = new Intent(Brainactivity.this, Game.class);       //gets guru from game class
                startActivity(guru);                                            // starts activity guru
                difficulty =3;                                                  // guru class case 3
                break;                                                          // break
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}

和xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="difficulty" />
</resources>

2 个答案:

答案 0 :(得分:1)

如Pavneet_Singh的评论中所述;如果你想将变量“难度”发送到你的活动,我建议你使用一个可以被你的活动检索的意图,并根据需要使用它来显示关卡。以下答案提供了一个很好的例子:

https://stackoverflow.com/a/5265952/9388338

对不起,如果这不是你的问题(据我所知)我暂时无法发表评论。 :)

答案 1 :(得分:0)

您的根问题是:为什么警报对话框中没有项目。

因为String.xml中的字符串数组中没有项目

 <string-array name="difficulty">
    <item>easy</item>
    <item>medium</item>
    <item>hard</item>
  </string-array>