Home > Software engineering >  For loop not stopping and random generated number not being saved (number guessing game)
For loop not stopping and random generated number not being saved (number guessing game)

Time:05-12

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 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 that 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. Because 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" as if its not saving the random number.

Any information will be of help!

CodePudding user response:

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

CodePudding user response:

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

  • Related