结束while循环
本文关键字:循环 while 结束 | 更新日期: 2023-09-27 18:04:46
我编写了一个小型Shotgun应用程序,但是,当AI(称为Genius),用户或两者都被击中时,需要停止游戏的代码部分,我无法开始工作。我做错了什么?我觉得我的代码太复杂了,因为我添加了很多带有不同布尔值的返回,其中一些被传递,而另一些没有。
在现在的测试中,无论用户移动(称为string移动)等于"f",循环都结束。在任何其他情况下,我都不能让循环结束。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
Start("r");
}
public static string Start(string move)
{
Console.Write("Welcome to the Shotgun App'nEnter s for single player and m for multiplayer: ");
string gameType = Console.ReadLine();
if (gameType == "s")
{
Console.Write("Single Player Controls:'n r = reload'n s = shield'n f = fire'nYou start with 1 ammo'nReady to play?");
Console.ReadLine();
int ammo = 1;
int geniusAmmo = 1;
string geniusMove = "";
bool done = false;
while (!done)
{
Console.Write("'nEnter your move: ");
move = Console.ReadLine();
switch (move)
{
case "r":
Console.Write("'nYou have reloaded, press enter for Genius'n");
ammo++;
Console.Write("Your ammo is " + ammo);
Console.ReadLine();
Genius(geniusMove, move, geniusAmmo, done);
break;
case "s":
Console.Write("'nYou have shielded, press enter for Genius'n");
Console.Write("Your ammo is " + ammo);
Console.ReadLine();
Genius(geniusMove, move, geniusAmmo, done);
break;
case "f":
if (ammo != 0)
{
Console.Write("'nYou have fired, press enter for Genius'n");
ammo--;
Console.Write("Your ammo is " + ammo);
Console.ReadLine();
Genius(geniusMove, move, geniusAmmo, done);
}
else
{
Console.Write("You don't have enough ammo, try again");
done = false;
}
break;
default:
Console.Write("'nInvalid move, try again'n");
done = false;
break;
}
done = EndLoop(move, geniusMove, done);
Console.ReadLine();
}
return move;
}
else
{
return move;
}
}
public static string Genius(string geniusMove, string move, int geniusAmmo, bool done)
{
Random RandomNumber = new Random();
int x = RandomNumber.Next(0,3);
if (x == 0)
{
geniusMove = "f";
geniusAmmo--;
Console.Write("Genius had decided to fire.'nGenius ammo is " + geniusAmmo + "'n");
TestMoves(move, geniusMove);
}
else if (x == 1)
{
geniusMove = "r";
geniusAmmo++;
Console.Write("Genius had decided to reload.'nGenius ammo is " + geniusAmmo + "'n");
TestMoves(move, geniusMove);
}
else if (x == 2)
{
geniusMove = "s";
Console.Write("Genius had decided to shield.'nGenius ammo is " + geniusAmmo + "'n");
TestMoves(move, geniusMove);
}
return geniusMove;
}
public static void TestMoves(string move, string geniusMove)
{
bool done = false;
if (move == "s" && geniusMove == "f")
{
Console.Write("Nice shield, no one has died yet");
}
else if (move == "f" && geniusMove == "f")
{
Console.Write("You both died! Good game!");
}
else if (move == "r" && geniusMove == "f")
{
Console.Write("No shield!? You died! Good game!");
}
else if (move == "f" && geniusMove == "s")
{
Console.Write("Genius is too good, no one has died yet");
}
else if (move == "f" && geniusMove != "s")
{
Console.Write("Genius let his guard down! Good game!");
}
else if (move != "f" && geniusMove != "f")
{
Console.Write("Keep playing it safe.");
}
else
{
}
}
static bool EndLoop(string move, string geniusMove, bool done)
{
done = false;
if (move == "s" && geniusMove == "f")
{
return false;
}
else if (move == "f" && geniusMove == "f")
{
return true;
}
else if (move != "s" && geniusMove == "f")
{
return true;
}
else if (move == "f" && geniusMove == "s")
{
return false;
}
else if (move == "f" && geniusMove != "s")
{
return true;
}
else if (move != "f" && geniusMove != "f")
{
return false;
}
else
{
return done;
}
}
}
}
你在几个不同的地方设置了done
,既在一些执行分支的切换情况下,也在调用EndLoop时。EndLoop的赋值将覆盖任何先前的赋值,因此将其设置为The One Place You Set done
。
设置done
与EndLoop在TestMoves没有任何影响,因为你立即返回一个硬编码的值后,你调用EndLoop。
我建议您在调试器中遵循EndLoop。如果让您更容易看到正在发生的事情,您可以考虑将EndLoop的输入参数打印到控制台,以及您最终选择的条件。
geniusMove将始终是一个空字符串,因为您不存储调用Genius方法的结果。
将结果存储在geniusMove变量中或通过引用传递
public static string Genius(ref string geniusMove, string move, int geniusAmmo, bool done)
或
geniusMove = Genius(geniusMove, move, geniusAmmo, done);