mercredi 12 juillet 2017

I want to use a static random number in a non-static class. (Java)

I am making a Higher or lower game. I need a random number in my game, but the problem is each time that I press the button to guess (The app uses the number from the EditText to compare) the random number changes. So I used a Static, but the problem now is that the function findViewById doesn't work anymore since it is not static.

Here is my code:

public static void click (View view) {
    double number = Math.ceil(Math.random()* 20);

    EditText textIn = (EditText) findViewById(R.id.textIn);

    int givenN = Integer.parseInt(textIn.getText().toString());

    if (number > givenN) {
        System.out.println("Higher");
    } else if (number == givenN){
        System.out.println("Good!");
    } else if (number < givenN) {
        System.out.println("Lower");
    }
}

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




Aucun commentaire:

Enregistrer un commentaire