Would anyone please be able to help? I am just starting out with some very basic android development. I am developing an initial app to help my daughter learn her times tables. Idea being (in its first version) randomly generate 2 numbers between 1 and 12. Then click on a calculate button which will give the answer. I have it so that it randomly generates the 2 numbers, however, when I click calculate, it does not calculate, simply shows 0. I believe it is something to do with the value of the variables (digit1 & digit2) not being seen by the next method (calculate). If I hard code 2 numbers into the calculate method, it works. I just cannot the random generated numbers to calculate. Any help would be most appreciated. MainActivity.java:
package uk.co.myrayner.sophiestimestables;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int digit1;
int digit2;
int show_answer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void randomise_digits(View view) {
Random rand1 = new Random();
int digit1 = rand1.nextInt(12) + 1;
displaydigit1(digit1);
Random rand2 = new Random();
int digit2 = rand2.nextInt(12) + 1;
displaydigit2(digit2);
}
public void calculate(View view) {
show_answer = (digit1 * digit2);
displayanswer(show_answer);
}
private void displaydigit1(int number) {
TextView digit1TextView = (TextView) findViewById(R.id.digit1);
digit1TextView.setText("" + number);
}
private void displaydigit2(int number) {
TextView digit2TextView = (TextView) findViewById(R.id.digit2);
digit2TextView.setText("" + number);
}
private void displayanswer(int number) {
TextView answerTextView = (TextView) findViewById(R.id.show_answer);
answerTextView.setText("" + number);
}
}
Aucun commentaire:
Enregistrer un commentaire