Yannik Pieper
Yannik Pieper

Reputation: 63

If statement does not work? Text does not change

I've written this code which should show a different Text at each integer (0-4). The If statements 0-3 are fine but when the integer turns into 4 the TextView does not change to "Example 5". If you press the button nothing happens and I've no Idea why! Need Help ;)

package com.eastereggdevelopment.entwederoder;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;


public class Start extends AppCompatActivity {

private int question;
private TextView Question;
private TextView quNumber;

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

}

public void back(View view)
{
    if(question > 1)
    {
        question--;
        setQuestion();
    }

}

public void next(View view)
{
    if(question < 5)
    {
        question++;
        setQuestion();
    }

}

public void rumble(View view)
{
    Random rand = new Random();
    question = (rand.nextInt(4));
    setQuestion();
}

public void setQuestion()
{
    TextView Question = (TextView) findViewById(R.id.question);
    if(question == 0)
    {
        Question.setText("Example 1");
    }
    if(question == 1)
    {
        Question.setText("Example 2");
    }
    if(question == 2)
    {
        Question.setText("Example 3");
    }
    if(question == 3)
    {
        Question.setText("Example 4");
    }
    if(question == 4)
    {
        Question.setText("Example 5");
    }
}
}

Thank you :)

Upvotes: 1

Views: 52

Answers (1)

Ramiz Wachtler
Ramiz Wachtler

Reputation: 5683

This line - question = (rand.nextInt(4)); , change the value to 5

The nextInt(int n) method is used to get a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

Java Documentation

Upvotes: 2

Related Questions