java - 我的 if 语句是否扰乱了我的菜单?

标签 java oop if-statement

我创建了一个带有菜单的钓鱼游戏。用户只需输入一个字母即可执行该选项。然而,当我尝试访问相应 if 语句下的代码时,由于某种原因,它要么跳过它,要么显示高分统计数据。我尝试更改字母,看看这是否是问题所在。也许这是我比较用户输入字符串的方式(我正在使用 .equals),或者也许这是我没有看到的更大问题。简而言之,我的 if 语句不能作为菜单使用,并且一些 if 语句(例如我的 r “准备好”钓鱼)不会执行任何操作。或者请您“购买升级版”钓鱼竿。

//Intense Fishing Simulator 2019
//2019-10-29
//Ormim Bari
//to practice the usage of realtionships.
package com.mycompany.intensefishingsimulator2019_ormimbari;

 import java.io.*;

public class DriverClass 
{
    public static void main (String args[]) throws IOException
    {
        BufferedReader MyInput=new BufferedReader (new InputStreamReader(System.in));

        Fisher joe = new Fisher(); //creates the fisher

        System.out.println("Welcome to Intesne Fishing Simulator 2019");
        System.out.println("You join fisher Joe's in the intense world of Fishing");
        int money=0;
        int endGame=0;
        boolean purchase1=false;//if purchased firsst upgrade
        boolean purchase2=false;//if purchased last upgrade
        do
        {
            //menu
            System.out.println("Enter r when ready to catch a fish");
            System.out.println("Enter c to see total of cash earned");
            System.out.println("Enter f to see total amount of fish caught");
            System.out.println("Enter u to upgrade your fishing rod");
            System.out.println("Enter joe to see stats");
            System.out.println("Enter Exit to end game ");

            String strInput=MyInput.readLine(); //gathers ihnput

            //ends game
            if (strInput.equals("Exit"))
            {
                endGame=999;
            }
            //display hp and fishing rod damage
            if (strInput.equals("joe"))
            {
                strInput="";
                System.out.println("Joe has a total hp of " +joe.getHp());
                System.out.println("Joes fishingrod does a total of "+joe.fr.getDamage() +" damage");

            }
            //display career total of fish caught
            if (strInput.equals("f"));
            {
                strInput="";
                System.out.println("You caught a total of"+joe.GetCatches()+"fishes");

            }
            //display career total of cash earned
            if (strInput.equals("c"));
            {
                strInput="";
                System.out.println( "You earned a total of $"+joe.getCashTotal());

            }
            if (strInput.equals("u"))
            {

                if (purchase1==false)
                {
                    System.out.println("Welcome to the shop! The first upgrade is $30. To buy enter b");
                    strInput=MyInput.readLine();
                    if (strInput.equals("b"))
                    {
                        strInput="";
                       boolean sucess= joe.fr.Upgrade1(money, joe, joe.fr);
                       if (sucess==true)
                       {
                           purchase1=true;
                       }
                    }
                }
                if (purchase1==true)
                {
                    System.out.println("Welcome to the shop! The last upgrade is $50. To buy enter b");
                    strInput=MyInput.readLine();
                    if (strInput.equals("b"))
                    {
                        strInput="";
                       boolean sucess= joe.fr.Upgrade2(money, joe, joe.fr);
                       if (sucess==true)
                       {
                           purchase2=true;
                       }
                    }
                }
                if (purchase2==true)
                {
                    System.out.println("You purchased all the upgrades");
                }
            }
            //user chooses to 'ready up' to fish
            if (strInput.equals("r"))
            {
                strInput="";
                int endFishing=0;

                //creation of fish to fight
                Fish fish= new Fish();

                //ceratopm of all fish 
                TunaFish tf= new TunaFish(); 
                SilverCarp sc= new SilverCarp();    
                Shark shark= new Shark();

                //intro
                System.out.println("You have been approached by a " + fish.getType());
                do
                {
                    //instructions
                    System.out.println("Enter a to attack the fish or r to run away");
                    strInput=MyInput.readLine(); 
                    //if chosing to run away
                    if (strInput.equals("r"))
                    {
                        strInput="";
                        boolean sucess;
                        sucess=joe.RunAway();
                        //if sucesfull
                        if (sucess==true)
                        {
                            joe.RunAwaySucess(joe);
                            endFishing=999;
                        }
                        //if joe failed to runaway
                        if (sucess==false)
                        {
                            //if tuna
                            if (fish.getType().equals("Tuna Fish"))
                            {   

                                boolean damageGiven= tf.AttackChanceTuna();
                                //if tuna dosnt miss
                                if (damageGiven==true)
                                {
                                    //joe gets the damage 
                                    joe.HarmJoe(tf.returnTunaDamage());
                                    System.out.println("Joe has been harmed by the tuna for "+ tf.returnTunaDamage());
                                    System.out.println("Joe now has" + joe.getHp());

                                    //if joe dies
                                    if (joe.getHp()<=0)
                                    {
                                        joe.EndGame(joe);
                                        endGame=999; //ends game
                                    }
                                }
                            }

                            //if carp
                            if (fish.getType().equals("Silver Carp"))
                            {
                                boolean damageGiven= sc.AttackChanceCarp();
                                //if carp dosnt miss
                                if (damageGiven==true)
                                {
                                    //harms joe
                                    joe.HarmJoe(sc.returnCarpDamage());
                                    System.out.println("Joe has been harmed by the silver carp for "+ sc.returnCarpDamage());
                                    System.out.println("Joe now has" + joe.getHp());
                                }
                                //if joe dies
                                    if (joe.getHp()<=0)
                                    {
                                        joe.EndGame(joe);
                                        endGame=999; //ends game
                                    }
                            }
                            //if shark
                            if (fish.getType().equals("Shark"))
                            {
                                boolean damageGiven= shark.AttackChanceShark();
                                //if shark dosnt miss
                                if (damageGiven==true)
                                {
                                    //harms joe
                                    joe.HarmJoe(shark.returnSharkDamage());
                                    System.out.println("Joe has been harmed by the silver carp for "+ shark.returnSharkDamage());
                                    System.out.println("Joe now has" + joe.getHp());


                                }
                                //if joe dies
                                    if (joe.getHp()<=0)
                                    {
                                        joe.EndGame(joe);
                                        endGame=999; //ends game
                                    }
                            }

                        }

                    }
                    //if attacking
                    if (strInput.equals("a"))
                    {
                        strInput="";
                        //tells user it did attack
                        fish.HarmFish(joe.fr.getDamage());
                        System.out.println("Joe has hurt " + fish.getType()+ "for "+ joe.fr.getDamage()+"damage");

                         //if tuna
                            if (fish.getType().equals("Tuna Fish"))
                            {
                                //damage the tuna
                                tf.harmTuna(joe.fr.getDamage());

                                boolean damageGiven= tf.AttackChanceTuna();
                                //if tuna attacks
                                if (damageGiven==true)
                                {
                                    //joe gets the damage if true
                                    joe.HarmJoe(tf.returnTunaDamage());
                                    System.out.println("Joe has been harmed by the tuna for "+ tf.returnTunaDamage());
                                    System.out.println("Joe now has" + joe.getHp());

                                    //if joe dies
                                    if (joe.getHp()<=0)
                                    {
                                        joe.EndGame(joe);
                                        endGame=999;
                                    }
                                    //if tuna dies
                                    if (tf.getTunaHp()<=0)
                                    {
                                        //reward screen output
                                        System.out.println("You beat the tuna!You got " + tf.fl.getLootType());
                                        System.out.println("You sold the " + tf.fl.getLootType()+ "for $" + tf.fl.getLootPrice());
                                        joe.increaseCash(tf.fl.getLootPrice());
                                        System.out.println("You now have $" + joe.getMoney()); //cash update
                                        tf.healTuna();//reset for next potential round
                                        joe.CareerCashTotal(tf.fl.getLootPrice()); //add to highscore
                                        joe.addCatches(); // add to high score
                                        endFishing=999; //stops the fishing
                                    }
                                }
                            }

                            //if carp
                            if (fish.getType().equals("Silver Carp"))
                            {
                                //damages the tuna
                                sc.harmCarp(joe.fr.getDamage());
                                boolean damageGiven= sc.AttackChanceCarp();
                                //if tuna dosnt miss
                                if (damageGiven==true)
                                {
                                    //harms joe
                                    joe.HarmJoe(sc.returnCarpDamage());
                                    System.out.println("Joe has been harmed by the silver carp for "+ sc.returnCarpDamage());
                                    System.out.println("Joe now has" + joe.getHp());
                                }
                                //if joe dies
                                    if (joe.getHp()<=0)
                                    {
                                        joe.EndGame(joe);
                                        endGame=999;
                                    }
                                    if (sc.getCarpHp()<=0)
                                    {
                                         //reward screen output
                                        System.out.println("You beat the silver carp!You got " + sc.fl.getLootType());
                                        System.out.println("You sold the " + sc.fl.getLootType()+ "for $" + sc.fl.getLootPrice());
                                        joe.increaseCash(sc.fl.getLootPrice());
                                        System.out.println("You now have $" + joe.getMoney()); //upgrade cash
                                        sc.healCarp();//reset for next potential round
                                        joe.CareerCashTotal(sc.fl.getLootPrice()); //add to high score
                                        joe.addCatches(); //add to high score
                                        endFishing=999; //stops the fishing
                                    }
                            }
                            //if shark
                            if (fish.getType().equals("Shark"))
                            { 
                                //harms the shark
                                shark.harmShark(joe.fr.getDamage());
                                boolean damageGiven= shark.AttackChanceShark();
                                //if shark dosnt miss
                                if (damageGiven==true)
                                {
                                    //harms joe
                                    joe.HarmJoe(shark.returnSharkDamage());
                                    System.out.println("Joe has been harmed by the silver carp for "+ shark.returnSharkDamage());
                                    System.out.println("Joe now has" + joe.getHp());


                                }
                                //if joe dies
                                    if (joe.getHp()<=0)
                                    {
                                        joe.EndGame(joe);
                                        endGame=999;
                                    }
                                //if shark dies
                                if (shark.getSharkHp()<=0)
                                    {
                                        //reward screen output
                                        System.out.println("You beat the Shark!You got " + shark.fl.getLootType());
                                        System.out.println("You sold the " + shark.fl.getLootType()+ "for $" + shark.fl.getLootPrice());
                                        joe.increaseCash(shark.fl.getLootPrice());
                                        System.out.println("You now have $" + joe.getMoney()); //updatees cash
                                        shark.healShark();//reset for next potential round
                                        joe.CareerCashTotal(shark.fl.getLootPrice()); //adds to high score
                                        joe.addCatches(); //adds to high score
                                        endFishing=999;
                                    }
                            }

                        }

                } while (endFishing!=999);
            }



        }while (endGame!=999);

        System.out.println("Thanks for joining Joe in the world of Intense Fishing ");


    }



}

最佳答案

我觉得单号的尺寸main方法会扰乱您对代码行为的理解。

您可以通过编写如下注释来提示自己代码的预期用途://display career total of fish caught//user chooses to 'ready up' to fish 。这是错误的。

所有这些提示都应该重构为方法。例如:displayFishCaught()isReadyUpFish() .

这是一个过程化编程;您可能想研究使用类和对象将代码移出单个类。您可以找到许多面向对象的编程类(class)和教程。

重构后,您会看到,例如,当用户想要退出游戏时,所有不相关的代码仍然会被执行,并受到 if大海的保护。声明。这证明您的代码无法在没有重大问题的情况下维护。

很抱歉,这可能不是您希望得到的答案。

关于java - 我的 if 语句是否扰乱了我的菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58617191/

相关文章:

java - 使用 Java、SQL 和 Oracle 引用和引用表的 List 或 ArrayList

C++:从函数更改类成员值

oop - Web 服务中 DTO 的命名约定是什么

ios - 检查 Core Data managedObject 中的多个 nil 值?

c# - C#中的简单乒乓球游戏

java - (MockEJB) 从容器内测试中获取模拟实体 bean

java - FileUtils.deleteDirectory 尝试删除以句点结尾的目录

PHP:检测重复列结果的结尾(日历日)

java - 我如何在我的 Android 应用程序的搜索小部件上实现 Google 放置自动完成功能?

php - 使用 ArrayObject 的配置类