Q:

How do I create a score counter in Android Studio?

I'm a complete newbie to Android Studio and have basic experience in Java. I tried to create an android app where the user has to input a number, once the button is clicked a random number is generated from 0-6, if the input number and the generated number is the same then the user gains 1 point. I've tried to implement a score counter but after 1 correct guess the score stays at 1 and never increases any further.
public class MainActivity extends AppCompatActivity {
String matchingnumbers = "Congratulations!";

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

public void on_button_click(View view) {
    TextView numberW = this.findViewById(R.id.textView);
    EditText tvW = this.findViewById(R.id.editText);
    TextView scoreW =this.findViewById(R.id.textView3);
    Random r = new Random();
    int dicenumber = r.nextInt(6);
    numberW.setText(Integer.toString(dicenumber));

    try {
        int number = Integer.parseInt(numberW.getText().toString());
        int tv = Integer.parseInt(tvW.getText().toString());
            if(number==tv){
                int score = 0;
                score++;
                Toast.makeText(getApplicationContext(), matchingnumbers, Toast.LENGTH_LONG).show();
                scoreW.setText("Your score is = " + score);
        }

    }
    catch (Exception ex) {
        Log.e("Button Errors", ex.toString());
    }
}
}

A:

Don't declare score in the method, because it does not remain. Declare it in the class instead:
public class MainActivity extends AppCompatActivity {
String matchingnumbers = "Congratulations!"; 
//here
int score = 0;
// ...
}

