Reputation: 1
I'm supposed to generate two random numbers, then find the perimeter of a rectangle with them. Then it allows the User to input their answer in a EditText, when the User inputs their answer in the EditText, There is a submit button so that when the user clicks it, it lets the person know if their answer is correct or incorrect. This is displayed as a toast message. The problem i have, is when i click the submit button, it always show says "The answer is incorrect even if i put in the right value in the EditText. Would appreciate some help. Thank You
public class Perimeter extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_perimeter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void PerimeterGame(View view) {
Random rand = new Random();
int number = rand.nextInt(12) + 1;
TextView myText = (TextView) findViewById(R.id.rand1);
String myString = String.valueOf(number);
myText.setText(myString);
Random rand2 = new Random();
int number2 = rand2.nextInt(12) + 1;
TextView myText2 = (TextView) findViewById(R.id.rand2);
String myString2 = String.valueOf(number2);
myText2.setText(myString2);
((TextView) findViewById(R.id.question)).setText
("Find the perimeter of a rectange with a width of " + myString + "cm" + " and " + "length of " + myString2 + "cm" + ".");
}
public void AnswerCheck(View view){
int perimeter;
Random randOne = new Random();
int number = randOne.nextInt(12) + 1;
TextView myText = (TextView) findViewById(R.id.rand1);
String myString = String.valueOf(number);
myText.setText(myString);
Random randTwo = new Random();
int number2 = randTwo.nextInt(12) + 1;
TextView myText2 = (TextView) findViewById(R.id.rand2);
String myString2 = String.valueOf(number2);
myText2.setText(myString2);
EditText num = (EditText)findViewById(R.id.answertext);
int val = Integer.parseInt(num.getText().toString() );
perimeter=(number+number2+number+number2);
if(val==perimeter){
Toast.makeText(this, "The answer is correct", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "The answer is incorrect ", Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 0
Views: 337
Reputation: 191738
Your problem is that you are generating completely different random numbers when you go to check the answer. You need to save the numbers in instance variables like so.
public class Perimeter extends AppCompatActivity {
private int number1, number2;
private Random rand;
onCreate() {
rand = new Random();
}
PerimeterGame(View view) {
number1 = rand.nextInt(12) + 1;
number2 = rand.nextInt(12) + 1;
}
AnswerCheck(View view) {
// Don't make new random variables here, just check your inputs.
EditText num = (EditText)findViewById(R.id.answertext);
int val = Integer.parseInt(num.getText().toString() );
int perimeter= 2*(number + number2);
if (val == perimeter) { }
}
}
Upvotes: 1
Reputation: 2509
In AnswerCheck
you dont need to generate new numbers again.. you need to get old generated numbers in first method PerimeterGame()
and do your formula with them.
Upvotes: 0