我用菜单创建了一个钓鱼游戏。用户只需输入一个字母即可执行该选项。但是,当我尝试访问相应的if语句下的代码时,由于某种原因,它要么跳过它,要么显示高分统计信息。我尝试更改字母以查看是否这是行不通的问题。也许这是我比较用户输入字符串的方式(我正在使用.equals),或者这是一个我没有看到的更大的问题。简而言之,我的if语句不能用作菜单,而某些if语句(例如我的“准备就绪”以进行钓鱼)则无济于事。还是我,您可以为钓鱼竿“购买升级”。
//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 ");
}
}
答案 0 :(得分:1)
我认为单个main
方法的大小会干扰您对代码行为的理解。
您可以通过编写诸如//display career total of fish caught
和//user chooses to 'ready up' to fish
之类的注释来提示代码应执行的操作。这是错误的。
所有这些提示都应重构为方法。例如:displayFishCaught()
和isReadyUpFish()
。
这是一个程序编程;您可能需要研究使用类和对象将代码移出单个类。您可以找到许多面向对象的编程课程和教程。
重构后,您会看到,例如,当用户想要退出游戏时,所有不受关联的代码仍会被if
语句的 sea 保护。这证明您的代码没有重大麻烦就无法维护。
很抱歉,这可能不是您希望得到的答案。
答案 1 :(得分:0)
您有一个
|
因此,当条件为true(if (...);
{
,后跟;
块时,始终输入空语句。
变量{ ... }
太全局,并且在各处都已更改,以后可能会输入错误的if语句。
一种解决方案使用strInput
:
else if
您也可以删除变量:
String strInput=MyInput.readLine(); //gathers ihnput
if (strInput.equals("Exit"))
{
//ends game
endGame=999;
} else if (strInput.equals("joe")) {
//display hp and fishing rod damage
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");
} else if (strInput.equals("f")) {
//display career total of fish caught
strInput="";
System.out.println("You caught a total of"+joe.GetCatches()+"fishes");
}
也可以使用特定的局部变量。
switch (MyInput.readLine()) { //gathers ihnput
case "Exit":
//ends game
endGame=999;
break;
case "joe":
//display hp and fishing rod damage
System.out.println("Joe has a total hp of " +joe.getHp());
System.out.println("Joes fishingrod does a total of "+joe.fr.getDamage() +" damage");
break;
case "f":
//display career total of fish caught
System.out.println("You caught a total of"+joe.GetCatches()+"fishes");
breaK;
default:
System.out.println("Wrong input");
}
Java约定
与其他语言相反,java中未使用波兰语符号 String menuAnswer = MyInput.readLine();
switch (menuAnswer) {
...
default:
System.out.println("Wrong input: " + menuAnswer);
}
。
按照惯例,变量也以小写字母开头。
变量声明为尽可能接近第一次使用。
类名以大写字母开头。