布尔if语句被忽略?

时间:2013-12-01 14:45:11

标签: java if-statement boolean

我不明白为什么忽略最终方法中的if语句。是因为我使用了太多if语句吗?

我已经注释掉了所有的if语句,并尝试在printShowChosen(....)中使用此代码来查看布尔值是否实际被转移

System.out.println(showchosen[0]);
System.out.println(showchosen[1]);
System.out.println(showchosen[2]);
System.out.println(showchosen[3]);

它打印出来

true
false
false
true

有人可以解释我哪里出错吗?

以下是完整的代码:

import javax.swing.*;
public class short8 {


public static void main(String[] args) {

    String[] ShowName = {"Les Miserables","Mamma Mia","Cats", "Chicago", "Phantom of the Opera"};
    String[] TheatreName = {"Queens Theatre", "Garrick Theatre", "Palmer Theatre", "Spoa Theatre", "Dend Theatre"};
    boolean[] showchosen = new boolean [5];

    askTouristChoices(TheatreName, showchosen);
    printShowsChosen(ShowName, TheatreName, showchosen);
    }//ends main

    public static String askTouristChoices(String[] ShowName, boolean[] showchosen)
    {
        System.out.println("Please type yes if you are interested in any of the following shows:");
        String interestedMiserables = JOptionPane.showInputDialog("Les Miserables");
        String interestedMammaMia = JOptionPane.showInputDialog("Mamma Mia");
        String interestedCats = JOptionPane.showInputDialog("Cats");
        String interestedChicago = JOptionPane.showInputDialog("Chicago");
        String interestedPhantom = JOptionPane.showInputDialog("Phantom of the Opera");


    if ((interestedMiserables.equals("Yes")) || (interestedMiserables.equals("yes")))
    {
        showchosen[0] = true;

    }
    else if ((interestedMiserables.equals("No")) || (interestedMiserables.equals("no")))
    {
        showchosen[0] = false;
    }
    else
    {
        JOptionPane.showMessageDialog(null, "Enter either Yes or No!!");
    }

        if ((interestedMammaMia.equals("Yes")) || (interestedMammaMia.equals("yes")))
        {
            showchosen[1] = true;

        }
        else if ((interestedMammaMia.equals("No")) || (interestedMammaMia.equals("no")))
        {
            showchosen[1] = false;
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Enter either Yes or No!!");
        }

            if ((interestedCats.equals("Yes")) || (interestedCats.equals("yes")))
            {
                showchosen[2] = true;
            }
            else if ((interestedCats.equals("No")) || (interestedCats.equals("no")))
            {
                showchosen[2] = false;
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Enter either Yes or No!!");
            }

                if ((interestedChicago.equals("Yes")) || (interestedChicago.equals("yes")))
                {
                    showchosen[3] = true;
                }
                else if ((interestedChicago.equals("No")) || (interestedChicago.equals("no")))
                {
                    showchosen[3] = false;
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Enter either Yes or No!!");
                }

            if ((interestedPhantom.equals("Yes")) || (interestedPhantom.equals("yes")))
            {
                showchosen[4] = true;
            }
            else if ((interestedPhantom.equals("No")) || (interestedPhantom.equals("no")))
            {
                showchosen[4] = false;
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Enter either Yes or No!!");
            }

        return null;
    } //ends askTouristChoices

        public static void printShowsChosen(String[] ShowName,String[] TheatreName, boolean[] showchosen)
        {
            if(showchosen[0] = true)
            {
                System.out.println(ShowName[0] + ": " + TheatreName[0]);
            }

            if(showchosen[1] = true)
            {
                System.out.println(ShowName[1] + ": " + TheatreName[1]);
            }

            if(showchosen[2] = true)
            {
                System.out.println(ShowName[2] + ": " + TheatreName[2]);
            }

            if(showchosen[3] = true)
            {
                System.out.println(ShowName[3] + ": " + TheatreName[3]);
            }

            if(showchosen[4] = true)
            {
                System.out.println(ShowName[4] + ": " + TheatreName[4]);
            } 
        }//ends printShowsChosen    


}//ends short8

2 个答案:

答案 0 :(得分:3)

if(showchosen[0] = true)

应该阅读

if(showchosen[0] == true)

if(showchosen[0])

单个=是分配,而不是比较。

答案 1 :(得分:1)

声明

if(showchosen[0] = true)

应该是:

if(showchosen[0] == true)

甚至更好:

if(showchosen[0])