I have managed to create a simple guessing game (this works fine) the only problem is I cant seem to refresh the random number that I generate at the start.
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private TextView hint;
private Button enterGuess;
private EditText chosenNumber;
Random rand = new Random();
int secretNumber = rand.nextInt(99) + 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enterGuess = findViewById(R.id.buttonEnter);
chosenNumber = findViewById(R.id.etEnterNumber);
hint = findViewById(R.id.tvHint);
final String hintHigh = "Guess Higher";
final String hintLow = "Guess Lower";
final String correctGuess = "Correct! Guess the new number!";
enterGuess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int number = Integer.parseInt(chosenNumber.getText().toString());
if (number < secretNumber){
hint.setText(hintHigh);
}
else if (number > secretNumber) {
hint.setText(hintLow);
}
else {
hint.setText(correctGuess);
Random rand = new Random();
int secretNumber = rand.nextInt(99) + 1;
}
}
});
}
}
Aucun commentaire:
Enregistrer un commentaire