在Java中为什么我会收到一条错误消息,说我需要其他if?

时间:2014-02-25 05:16:50

标签: java if-statement compiler-errors

所以我在6个小时内完成了这个庞大的项目,虽然我没有编写所有将要读取的文件,但我正在设置所有的if语句。您看到我正在创建一个数据库,其中用户输入3个数字,作为回报,将获得一个读出的文件。到目前为止,一切似乎都很好,除了底部显示的其他内容:

 if (choose == 4) //enemies from the original Kingdom Hearts II
           {
            System.out.println("Please select the enemy race you wish to view:");
            System.out.println("1. Heartless");
            System.out.println("2. Nobodies");
            int enemies_4 = kH.nextInt();

        if (enemies_4 == 1) //Allows the user to select which kinds of Heartless to view from Kingdom Hearts II.
        {
            System.out.println("Please type the number which corresponds to the Heartless enemy type.");
            System.out.println("1. Pureblood");
            System.out.println("2. Emblem");
            System.out.println("3. Gummi");
            int heartless_4 = kH.nextInt();

            if (heartless_4 = 1) //allows the user to view Pureblood Heartless from KH2
            {

            }

            if (heartless_4 == 2) //allows the user to view the Emblem Heartless in KH2
            {

            }

            else
            {
                System.out.println("Please enter a number which corresponds to one of the Heartless types KH2.");
            }
        }


        if (enemies_4 == 2) //allows the user to view all of the Nobodies in KH2
        {
            System.out.println("Please type the number which corresponds to the Nobody enemy type.");
            System.out.println("1. Lower");
            System.out.println("2. Gummi");
            int nobodies_4 = kH.nextInt();

            if (nobodies_4 == 1) //Gives the selection of the Lower Nodies in Kingdom Hearts II.
            {

            }

            if (nobodies_4 == 2) //Gives the selection of the Gummi Nobodies, which are ONLY in Kingdom Hearts II
                                //(as well as the Final Mix version of it).
            {

            }

            if (nobodies_4 == 3) //Give the selection of all he members of Organization XIII which appear in
                                    //Kingdom Hearts II.
            {

            }

            else
            {
                System.out.println("Please enter a number which corresponds to one of the Nobody types KH2.");
            }

        else
        {
            System.out.println("Please input a number which corresponds to an enemy.");
        }
    }

错误说它应该是其他的,但是当我把if旁边的时候我自动得到100个错误。我该怎么办?

4 个答案:

答案 0 :(得分:4)

如果所有if块都是独占的,那么使用if-else总是好的做法。话虽如此,上述错误是由于错过了“}”

if (choose == 4) //enemies from the original Kingdom Hearts II
       {
        System.out.println("Please select the enemy race you wish to view:");
        System.out.println("1. Heartless");
        System.out.println("2. Nobodies");
        int enemies_4 = kH.nextInt();

    if (enemies_4 == 1) //Allows the user to select which kinds of Heartless to view from Kingdom Hearts II.
    {
        System.out.println("Please type the number which corresponds to the Heartless enemy type.");
        System.out.println("1. Pureblood");
        System.out.println("2. Emblem");
        System.out.println("3. Gummi");
        int heartless_4 = kH.nextInt();

        if (heartless_4 = 1) //allows the user to view Pureblood Heartless from KH2
        {

        }

        if (heartless_4 == 2) //allows the user to view the Emblem Heartless in KH2
        {

        }

        else
        {
            System.out.println("Please enter a number which corresponds to one of the Heartless types KH2.");
        }
    }


    if (enemies_4 == 2) //allows the user to view all of the Nobodies in KH2
    {
        System.out.println("Please type the number which corresponds to the Nobody enemy type.");
        System.out.println("1. Lower");
        System.out.println("2. Gummi");
        int nobodies_4 = kH.nextInt();

        if (nobodies_4 == 1) //Gives the selection of the Lower Nodies in Kingdom Hearts II.
        {

        }

        if (nobodies_4 == 2) //Gives the selection of the Gummi Nobodies, which are ONLY in Kingdom Hearts II
                            //(as well as the Final Mix version of it).
        {

        }

        if (nobodies_4 == 3) //Give the selection of all he members of Organization XIII which appear in
                                //Kingdom Hearts II.
        {

        }

        else
        {
            System.out.println("Please enter a number which corresponds to one of the Nobody types KH2.");
        }
     } // You were missing this
    else
    {
        System.out.println("Please input a number which corresponds to an enemy.");
    }
}

答案 1 :(得分:0)

条件没有结束}if (enemies_4 == 2),您需要添加}

答案 2 :(得分:0)

对我来说,看起来第一个if-condition错过了右括号。

if (choose == 4) //enemies from the original Kingdom Hearts II
{

} // This is missing.

如果这个条件块看起来没问题,那么你应该在下面的块中寻找缺少的右括号。

if (enemies_4 == 2) //allows the user to view all of the Nobodies in KH2
{

} // This is missing. 

正确缩进代码非常重要。如果您使用的是Eclipse或任何其他IDE,那么只需单击一下即可管理代码缩进。只要您在代码窗口中选择代码,Control + Shift + F就会执行此操作。

此外,您应该注意使用if-else-ifswitch-case块来优化条件。使用正确的必需块可以减少出错的可能性。

if-else优化的示例如下所示。

Java if statement checking Java if statement checking

Shishir

答案 3 :(得分:0)

if (choose == 4) //enemies from the original Kingdom Hearts II
       {
        System.out.println("Please select the enemy race you wish to view:");
        System.out.println("1. Heartless");
        System.out.println("2. Nobodies");
        int enemies_4 = kH.nextInt();

    if (enemies_4 == 1) //Allows the user to select which kinds of Heartless to view from Kingdom Hearts II.
    {
        System.out.println("Please type the number which corresponds to the Heartless enemy type.");
        System.out.println("1. Pureblood");
        System.out.println("2. Emblem");
        System.out.println("3. Gummi");
        int heartless_4 = kH.nextInt();

        if (heartless_4 = 1) //allows the user to view Pureblood Heartless from KH2
        {

        }

        if (heartless_4 == 2) //allows the user to view the Emblem Heartless in KH2
        {

        }

        else
        {
            System.out.println("Please enter a number which corresponds to one of the Heartless types KH2.");
        }
    }


    if (enemies_4 == 2) //allows the user to view all of the Nobodies in KH2
    {
        System.out.println("Please type the number which corresponds to the Nobody enemy type.");
        System.out.println("1. Lower");
        System.out.println("2. Gummi");
        int nobodies_4 = kH.nextInt();

        if (nobodies_4 == 1) //Gives the selection of the Lower Nodies in Kingdom Hearts II.
        {

        }

        if (nobodies_4 == 2) //Gives the selection of the Gummi Nobodies, which are ONLY in Kingdom Hearts II
                            //(as well as the Final Mix version of it).
        {

        }

        if (nobodies_4 == 3) //Give the selection of all he members of Organization XIII which appear in
                                //Kingdom Hearts II.
        {

        }

        else
        {
            System.out.println("Please enter a number which corresponds to one of the Nobody types KH2.");
        }
     }// you miss this 
    else
    {
        System.out.println("Please input a number which corresponds to an enemy.");
    }
}