0

Hello Guys im new on this Website so if I make any mistakes please forgive me. For the School I need to code an App and I want to code an app like the old Egg app where you needed to click on for like 1.000.000 times.

My Counter works but I wanted to let the Egg break. For this I wanted to use an if statement so I can make it break after like 200 times and so on.

My Problem is that if I'm pressing the button the Image switch in the first time. Not as I expected in the third time

Do anyone knows what my problem is?

PS: I'm new in java so maybe the code isn't sexy

package com.example.die_vierte;

import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.webkit.SafeBrowsingResponse;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private Object TextView;

    int eggcounter;
    ImageButton ImgButton;

    android.widget.TextView textClicks;
    private Object SafeBrowsingResponse;

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

        eggcounter = 10000000;
        final ImageButton ImgButton = findViewById(R.id.eggBtn);

        ImgButton.setOnClickListener(
                new View.OnClickListener() {

                    public void onClick(View view) {
                        eggcounter = eggcounter - 1;
                        updateEgg();

                        if (eggcounter > 999998) {

                            ImgButton.setImageResource(R.drawable.egg_2);
                        }

                    }
                }
        );

    public void updateEgg() {
        textClicks = (TextView) findViewById(R.id.textScore);
        textClicks.setText(eggcounter + " ");

    }
2
  • Your counter is always greater than 999998 that is why it always works the first time, and there is no else statement after the if Commented Apr 28, 2019 at 0:32
  • could you please check my answer and evaluate it Commented Apr 28, 2019 at 1:00

2 Answers 2

1

The first time you click on the button, the 'eggcounter' is at 999999.
That is more than 999998, so the if statement is true, thus the image is changed.

I'm guessing that you should change the '>' to a '<' so the image isn't changed until the 3rd click.

Sign up to request clarification or add additional context in comments.

3 Comments

If im doing this the image never change.
Check the numbers. You're using '10000000' and '999998' First number is 10 million, the second is just under a million. So you'd need to click the button just over 9 million times. I'm guessing you meant to input '1000000' instead.
oh yes youre right didnt notice how much numbers there are i just pressed the 0. Thank you <3 much love
0

First of all , you .setText() method isn't correct ,you should use it like that :

textClicks.setText(String.valueOf(eggcounter)+" ");

and for your issue you should use your if statement like this :

 if (eggcounter < 9999998) {
      ImgButton.setImageResource(R.drawable.egg_2);
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.