'For loop not stopping and random generated number not being saved (number guessing game)

I'm doing a number guessing game in an Android app. You have three tries to guess the number. If you run out of trials, then you loose.

I added a for loop so that each time the user inputs then it adds one more. But, when I run the app and I input the numbers more than three times, it doesn't show the "You don't have more tries" message, like it's not counting the tries.

I've tried changing the for to a restart button, and then every time I click the restart button it adds one more. But, that didn't work either.

My current code is:

public class MainActivity extends AppCompatActivity {
    private int trials;
    private int maxTrials = 3;
    private TextView s_title, t_rules, strt_title, t_introgss;
    private Button btn_start;
    private EditText num_input;


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

    num_input = findViewById(R.id.num_input);
    btn_start = findViewById(R.id.btn_start);
    s_title = findViewById(R.id.s_title);
    t_rules = findViewById(R.id.t_rules);
    strt_title = findViewById(R.id.strt_title);
    t_introgss = findViewById(R.id.t_introgss);

    btn_start.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View arg0){

            s_title.setVisibility(View.INVISIBLE);
            t_rules.setVisibility(View.INVISIBLE);
            btn_start.setVisibility(View.INVISIBLE);

            strt_title.setVisibility(View.VISIBLE);
            t_introgss.setVisibility(View.VISIBLE);
            num_input.setVisibility(View.VISIBLE);
        }
    });



    num_input.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int guessNum = Integer.parseInt(num_input.getText().toString());
            int number = 1 + (int) (30 * Math.random());

            for (trials = 0; trials < maxTrials; trials++) {
                if (guessNum == number) {
                    Toast.makeText(MainActivity.this, "You won!", Toast.LENGTH_SHORT).show();

                } else if (guessNum < number && trials != maxTrials - 1) {
                    Toast.makeText(MainActivity.this, "The number is bigger", Toast.LENGTH_SHORT).show();

                } else if (guessNum > number && trials != maxTrials - 1) {
                    Toast.makeText(MainActivity.this, "The number is smaller", Toast.LENGTH_SHORT).show();
                }

                if (trials == maxTrials) {
                    Toast.makeText(MainActivity.this, "You dont have more tries", Toast.LENGTH_SHORT).show();
                    Toast.makeText(MainActivity.this, "The number was " + number, Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }
    });
}

}

I think it's not counting the trials, because it's generating a random number every time the user inputs a guess. I've tried entering 17 and it shows a message that "the value is bigger" and I enter 18 and shows a message that "the value is smaller". It's as if it's not saving the random number.



Solution 1:[1]

You do not have to break as that should be handled by the for loop -- @mkjh

This is already the reason that the for-loop is ending, so placing it afterward instead of as a test/break condition will be more efficient. Also,

Try changing it to trials <= maxTrials – @viv3k

// start at trial 1 & end when trial == the max
for (trials = 1; trials <= maxTrials; trials++) 
{
    ...
}
// then after that loop
Toast.makeText(MainActivity.this, "You dont have more tries", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "The number was " + number, Toast.LENGTH_SHORT).show();

The break statement is provided by the test condition trials < maxTrials in the for-loop

EDIT for additional info

once I input a number, the "the number is ..." message repeats three times, and shows that the user ran out of trials, but only one number has been input.

I can see that a value is being entered out of the loop

public void onClick(View view) 
{
    int guessNum = Integer.parseInt(num_input.getText().toString());
    int number = 1 + (int) (30 * Math.random());

Then inside of the loop, there is not another place to take a user's guess so it tests those conditions with the same number each time. Reading in the user's guess within the loop will allow you to reassign a value to test on the next loop around (edited the format into a while-loop)

    trials = 0;
    boolean found = false;
    // under 3 guesses and not found
    while(++trials <= maxTrials && !found) 
    {
        // they guessed it
        if (guessNum == number) 
        {
            they win;
            found = true;
        } // they didn't guess it
        else 
        {
            if (guessNum < number)
                it is bigger;
            else
                it is smaller;
            // take the next guess when it is > or <
            guessNum = Integer.parseInt(num_input.getText().toString());
        }
    }
    ...
}    

NOTE This is not full code

Solution 2:[2]

trials will never be == to maxTrials. In your for loop, you are starting with trials = 0 and stops the loop when trials < maxTrials. The criteria should be changed to trials == maxTrials-1 instead. Also, you do not have to break as that should be handled by the for loop

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 mkjh