Search code examples
androidtournament

If the start button is clicked, it will not proceed


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;
            }
        }
    }
}

Solution

  • 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;
                }
            }
        }