简体   繁体   中英

Android App Keeping Random Number the Same

I have posted with this code before but this is a different question.

When the 'guess' button is pressed, a random number is generated. The only problem with the code as it is is that it generates a new number every time regardless of whether the user guesses the right number or not. Ideally I want to give the user a 3 guess limit which would require the app to keep the random number generated the same and then reset after 3 incorrect attempts. I've come to a standstill as I've not done any java for a long time and it's perplexing me a bit in terms of incorporating it into this app.

Thanks in advance

package lab.mad.cct.c3375331task1;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import  android.support.v7.app.AlertDialog;
import     android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;

public class Task1Activity extends     AppCompatActivity {

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

        final TextView textResponse = (TextView)               findViewById(R.id.txtResponse);
        final TextView guessText = (TextView)  findViewById(R.id.txtAnswer);
        final EditText userGuess = (EditText) findViewById(R.id.etNumber);

        Button pressMe = (Button) findViewById(R.id.btnGuess);



    // When the button is clicked, it shows the text assigned to the txtResponse TextView box
        pressMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String randText = "";
                Random randGen = new   Random();
                int ranNum = randGen.nextInt(5);
                int userNumber =       Integer.parseInt(userGuess.getText().to String());
                int attempts = 0;


                if (userNumber >19 ) {
                guessText.setText("Please guess between 0 and 20");
                } else if (userNumber == ranNum) {
                    guessText.setText("You got it!");
                } else if (userNumber < ranNum) {
                    guessText.setText("Your answer is too low. Guess     again!");
                  guessText.setBackgroundColor(Color.RED);
                } else if (userNumber > ranNum) {
                    guessText.setText("Your answer is too high.  Guess again!");
                }

                randText = Integer.toString(ranNum);
                textResponse.setText("");

                userGuess.setText("");

            }
        });

    } 



}

remove int userNumber = Integer.parseInt(userGuess.getText().to String()); from onClick(View v) because every time guessme button will be pressed, it will generate the new number.

declare some variables in your class

public static final int ATTEMPTS = 3;
public int attempt = 0;
public int currentRandomNumber = new Random().nextInt(5);

inside onClick();

if(attempt >= ATTEMPTS){
  attempt = 0;
  currentRandomNumber = new Random().nextInt(5);
} else {
  attempt++;
}

// the rest of your code..

also, you are using rand..nextInt(5) but by the looks of your code you should be using ..nextInt(20)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM