我的对象命令中的if语句有问题吗



我是OOP的新手,正在制作一款钓鱼游戏。现在我有一个叫船的物体,它可以根据燃料行驶一定的距离。然而,如果它的燃料耗尽,并且在海洋中央,游戏应该结束。我的问题是,出于某种原因,船没有行驶一定的距离,而不是结束比赛,因为它不在地标上,而且燃料会是空的。

public class Boat 
{
int fuelCapacity;
int fuelLevel;
int fishCapacity;
int location;
public Boat()
{
fuelCapacity=10;
fuelLevel=10;
fishCapacity=10;
location=0;
}

public int getFuelCapacity()
{
return this.fuelCapacity;
}
public int getFuelLevel()
{
return this.fuelLevel;
}
public int getLocation()
{
return this.location;
}

//start the process of travel and determine what happens
//// 
public int Travel(int Fuel,int Location,int FuelCapacity) throws IOException
{
int FuelConsumed=0;
//Enable IO
BufferedReader MyInput=new BufferedReader (new InputStreamReader(System.in));

System.out.println("How many meters do you want to travel");
System.out.println("Please enter only in increments of 10m");
System.out.println("Enter in negative number to go west");
System.out.println("Please in positive number to go east");
int distance=Integer.parseInt(MyInput.readLine());

//if they go east
if (distance<0)
{
for (int x=0;x<=(distance/10);x++)
{
FuelConsumed++;
}
}
//if they go west
if (distance>0)
{
for (int x=0;x<=((distance*(-1))/10);x++)
{
FuelConsumed++;
}
}
Location=Location+distance;
int FuelLeft=Fuel-FuelConsumed;

//Checks which sceanrio it falls under and gives apporpiate output
if (FuelLeft==0 && Location!=100) //End game
{
System.out.println("Oh No your stuck in the middle of ocean with no fuel");
System.out.println("Hope you can survive the middle of ocean by youself");
System.out.println("GAME OVER");
distance=9999;
return distance;
}
else if (FuelLeft==0 && Location!=0) ///End game
{
System.out.println("Oh No your stuck in the middle of ocean with no fuel");
System.out.println("Hope you can survive the middle of ocean by youself");
System.out.println("GAME OVER");
distance=9999;
return distance;
}
else if (FuelLeft==0 && Location!=200) ///End game
{
System.out.println("Oh No your stuck in the middle of ocean with no fuel");
System.out.println("Hope you can survive the middle of ocean by youself");
System.out.println("GAME OVER");
distance=9999;
return distance;
}
else if (FuelLeft==0 && Location!=300) ///End game
{
System.out.println("Oh No your stuck in the middle of ocean with no fuel");
System.out.println("Hope you can survive the middle of ocean by youself");
System.out.println("GAME OVER");
distance=9999;
return distance;
}
else if (FuelLeft==0 && Location!=400) //stuck in ocean
{
System.out.println("Oh No your stuck in the middle of ocean with no fuel");
System.out.println("Hope you can survive the middle of ocean by youself");
System.out.println("GAME OVER");
distance=9999;
return distance;
}


else if (Location>400) //error catching (out of bounds)
{
System.out.println("You cant go out of bounds! Its restricted waters");
distance=0;
return distance;
}
else if (Location<0) //error catching (out of bounds)
{
System.out.println("You cant go out of bounds! Its restricted waters");
distance=0;
return distance;
}


else if (FuelLeft<(distance/10)) //error catching (trying togo father then the amount of fuel allows them too)
{
System.out.println("You dont have engough fuel to go there");
return distance;
}
else if (FuelLeft<((distance*(-1))/10)) //error catching (trying togo father then the amount of fuel allows them too)
{
System.out.println("You dont have engough fuel to go there");
return distance;
}


else 
{
System.out.println("You travelled " + distance + "m");
return distance;
}
}
}
Boat boat= new Boat();

System.out.println("You have " +boat.getFuelLevel());
System.out.println("1 fuel goes 10m ");
System.out.println("The map");
System.out.println("Legend:");
System.out.println("- eligable fishing spot and is 10m each  ");
System.out.println("# is a town");
System.out.println("East: ->");
System.out.println("West: <-");


System.out.println("  1          2          3          4          5");
System.out.println("  |----------|----------|----------|----------| ");
System.out.println("  0m         100m       200m       300m       400m");

boat.fuelLevel=5;
boat.location=100;
boat.fuelCapacity=10;
boat.Travel(boat.getFuelLevel(), boat.getLocation(), boat.getFuelCapacity());

如果Fuel不等于0(例如FuelLeft==0 && Location!=100(,则不会触发游戏结束事件。你应该把它改成这样。

if (FuelLeft <= 0 && Location !=100)

这将使你有能力耗尽燃料,并且它知道这一点。当你检查距离时:

if (distance<0) //Change to if (distance <= 0)

if(distance>0) //Change to if (distance >= 0)

这应该允许最终的GameOver发生。

-编辑-

我对您的代码进行了更多的研究,发现您的问题在于您的输入。您的输入永远不会影响您的船对象。您需要更改输入法。也许是从主函数而不是从船对象获取输入。

你还需要添加一个循环,让你重复"钓鱼"或"移动"的过程,因为每次你的燃料耗尽时,你的"错误检查器"都在工作,因为它会说你没有足够的燃料来做这件事。因为它只运行一次,所以你不可能达到所需的规格,因为你不会耗尽燃料。

最后,您在代码中已经将燃油设置为5,因此当您运行程序时,燃油将始终为5。以下是我的建议:

  1. 在主函数中进行输入。

  2. 当你收到某个值时,改变你的船类来做一些事情。(现在,如果你只是更改boat对象的输入,它什么都不做,你必须添加一个参数或其他东西,这样它才能跟踪你想要移动的距离。你可以通过创建一个存储每个值的类变量来做到这一点,然后boat对象在移动后返回值。(

  3. 将代码分解为更多的部分。看起来你试图同时做太多的事情。让它为每个单独的事物提供单独的功能。例如,Travel不一定是int,因为您希望它移动船只。您也不需要Travel中的所有这些参数,因为它们已经存储在boat变量中。

  4. 确保你明白,每次按下跑步按钮,一切都会重置。这也可能是为什么你总是有相同数量的燃料。围绕输入部分做一个循环(这可能在主函数中(,然后再次让boat函数在此基础上进行操作。

  5. 我会展示我所有测试的例子(我做了(,但为了解决你的问题,我必须重做几乎整个输入和输出程序,这将花费太多时间。但这里有一个建议可以让你开始:

    public void Move(int distance) throws IOException
    { 
    Travel( fuelLevel, location, fuelCapacity, distance); //distance is the distance
    }
    

您可以添加此项以获得主功能中的输入:

Scanner keyboard = new Scanner(System.in);
int distance = keyboard.nextInt();
boat.Move(distance);

移动函数(如上(将移动船,并将类变量值更改为正确的值。然后你可以做一个

do{
//display where the boat is with your picture
//get user input
}while(!gameOver);

祝你比赛顺利!我希望这能有所帮助!

很简单,在您的代码中。。。boat.fuelLevel=5;boat.location=100;boat.fuelCapacity=10;这些值在具有静态值的编译器中进行了推理。

最新更新