0

I want to make an application to find the ideal type of tournament. The code below is a function that starts when you press the Start button. 'choice1' and 'choice2' are boolean that becomes true when each image button is pressed. When the Start button is clicked, only the button is pressed and not executed. I'm a beginner, so I don't know what's wrong. Help me.

public class BoyWorldCup extends Activity {
    boolean choice1;
    boolean choice2;
    Button start;
    ImageButton image1, image2;
    TextView txt1, txt2;
    ArrayList<Integer> photoList;
    ArrayList<String> nameList;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.boy);
        int[] photos= {R.drawable.cha_enwoo, R.drawable.park_bogum,R.drawable.jung_heain,R.drawable.seo_gangjoon,
                R.drawable.gang_dongwon,R.drawable.gong_you,R.drawable.won_bin, R.drawable.ahn_hyosub};
        photoList = new ArrayList<>();
        for(int i : photos) {
            photoList.add(i);
        }
        String[] names ={"차은우","박보검","정해인","서강준","강동원","공유","원빈", "안효섭"};
        nameList = new ArrayList<>(Arrays.asList(names));

        start = (Button) findViewById(R.id.start);
        image1 = (ImageButton) findViewById(R.id.image1);
        image2 = (ImageButton) findViewById(R.id.image2);
        txt1 = (TextView) findViewById(R.id.txt1);
        txt2 = (TextView) findViewById(R.id.txt2);
        TextView roundTxt = (TextView) findViewById(R.id.roundTxt);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                worldCup();
            }
        });
        image1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                choice1 = true;
            }
        });
        image2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                choice2 = true;
            }
        });
    }
    public void worldCup(){
        int round=8;
        while(true) {
            if (round == 1) {
                image1.setImageResource(photoList.get(0));
                txt1.setText("당신의 최종 이상형입니다.");
                break;
            }
            int i =0;
            while(true) {
                if (i == round) {
                    break;
                } else {
                    image1.setImageResource(photoList.get(i));
                    image2.setImageResource(photoList.get(i + 1));
                    txt1.setText(nameList.get(i));
                    txt2.setText(nameList.get(i + 1));
                    if (choice1) {
                        photoList.remove(i + 1);
                        nameList.remove(i + 1);
                        choice1 = false;
                        i = i + 2;
                    } else if (choice2) {
                        photoList.remove(i);
                        nameList.remove(i);
                        choice2 = false;
                        i = i + 2;
                    }
                }
                round = round / 2;
            }
        }
    }
}
mmmgod
  • 3
  • 2
  • 1
    where `worldCup()` method is being called? – Hai Hack Jun 10 '20 at 06:51
  • start.setOnClickListener(new View.OnClickListener() { //Override public void onClick(View view){ worldCup(); } }); image1.setOnClickListener(new View.OnClickListener() { //Override public void onClick(View view) { choice1 = true; } }); image2.setOnClickListener(new View.OnClickListener() { //Override public void onClick(View view) { choice2 = true; } }); – mmmgod Jun 10 '20 at 07:04
  • The method was called under Oncreate. – mmmgod Jun 10 '20 at 07:06
  • please post your full code! – Hai Hack Jun 10 '20 at 07:10
  • It's been modified. Please check. – mmmgod Jun 10 '20 at 07:24
  • Acttually it was executed. The problem here is it get stuck when i == round = 0 and cannot escape from the loop – Hai Hack Jun 10 '20 at 07:57

1 Answers1

0

Since you used nested loops, you should name the outer loop then break it with the label you have just set.

public void worldCup(){
        int round=8;
        outerloop: //label the loop
        while(true) {
            if (round == 1) {
                image1.setImageResource(photoList.get(0));
                txt1.setText("당신의 최종 이상형입니다.");
                break;
            }
            int i =0;
            while(true) {
                if (i == round) {
                    break outerloop; //break it
                } else {
                    image1.setImageResource(photoList.get(i));
                    image2.setImageResource(photoList.get(i + 1));
                    txt1.setText(nameList.get(i));
                    txt2.setText(nameList.get(i + 1));
                    if (choice1) {
                        photoList.remove(i + 1);
                        nameList.remove(i + 1);
                        choice1 = false;
                        i = i + 2;
                    } else if (choice2) {
                        photoList.remove(i);
                        nameList.remove(i);
                        choice2 = false;
                        i = i + 2;
                    }
                }
                round = round / 2;
            }
        }
    }
Hai Hack
  • 948
  • 13
  • 24
  • Thank you. It's working. But can I ask you one more question? There is no change when each image button in the loop is clicked, do you know the solution to this? – mmmgod Jun 10 '20 at 08:20
  • Because the loop is finished before you tap on your `ImageButton`. Solution is run `worldCup()` one more time when you tap on your `ImageButton`. – Hai Hack Jun 10 '20 at 08:33