Issue
I have created a small android application based on random number guessing. When I will click on the button "generate random number" it will generate one random number between 1-100 and the number will be shown in the TextView. But at the initial time the TextView will be invisible because I don't want to show that the random number to the user before guessing. After generating the random number, The user have to guess the correct number generated by the system and have to write on the EditText portion. Now when I will click on the check button, it will check whether the user guessed it correct or not. If the guess number is lower than the generated number, it will show "too low try again". If user guessed it higher than the generated number, it will show "Too high try again". Now if the user guessed it correct, it will show that the user guessed it correctly and also it will show that in how many guesses they got the correct answer. Also that time the TextView where the random number generated will get visible.(I have used do-while loop for the checking part)
Now, when I'm running the app, when I was clicking on the button "generate the random number", it was generating the number. But when I was mentioning the guessed number and clicking on the check button, neither the app was responding nor it was showing any kind of exceptions in the logcat. And each and every time the avd is restarting. I will be thankful, if anybody can assist me to solve this problem. Thank you.
The code is given below:
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btn_random, btn_check;
TextView txt_random_num;
EditText edt_guess;
int guess;
int random;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
declareView();
randomNumberGenerate();
guessingTheValue();
}
private void guessingTheValue() {
btn_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
while (guess != random){
guess = Integer.valueOf(edt_guess.getText().toString().trim());
count++;
validation();
}
}
});
}
private void randomNumberGenerate() {
btn_random.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txt_random_num.setVisibility(View.GONE);
random = (int) (Math.random() * 100) + 1;
txt_random_num.setText(String.valueOf(random));
Toast.makeText(MainActivity.this,"Random number Generated!! Now guess Below",Toast.LENGTH_LONG).show();
}
});
}
private void declareView() {
txt_random_num = findViewById(R.id.btn_random);
btn_random = findViewById(R.id.btn_random);
btn_check = findViewById(R.id.btn_check);
edt_guess = findViewById(R.id.edt_guess);
}
public boolean validation(){
if (guess > random)
{
Toast.makeText(MainActivity.this,"Too high! Try Again",Toast.LENGTH_LONG).show();
return false;
}
else if (guess < random)
{
Toast.makeText(MainActivity.this,"Too low! Try Again",Toast.LENGTH_LONG).show();
return false;
}
else {
btn_random.setVisibility(View.GONE);
txt_random_num.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this,"Correct! You got it in" + count + "guesses!",Toast.LENGTH_LONG).show();
return true;
}
}
}
Images are given below
Solution
The issue is declaring the while loop inside the Button OnClickListener, there is no need for it. Instead you can restructure your code as follows:
btn_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
guess = Integer.valueOf(edt_guess.getText().toString().trim());
validation();
if (guess != random){
count++;
}
}
});
Answered By - Learnaholic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.