如何从方法返回布尔值
我有一个 while 循环,一直循环到 bool done = true; 在方法 TestMoves() 中,根据用户输入,该方法返回 bool done 为 true 或 false。但是,我不知道如何将此值“发送”回 Start() 方法中的 while 循环以停止循环。这是我的代码:
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\n ***you start with ammo\n Ready to play?");
Console.ReadLine();
int ammo = 1;
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();
string geniusMove = "";
Genius(geniusMove, move, done);
Console.ReadLine();
break;
case "s":
Console.Write("\nYou have shielded, press enter for Genius\n");
Console.Write("Your ammo is " + ammo);
Console.ReadLine();
geniusMove = "";
Genius(geniusMove, move, done);
Console.ReadLine();
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();
geniusMove = "";
Genius(geniusMove, move, done);
Console.ReadLine();
}
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;
}
}
return move;
}
else
{
return move;
}
}
static string Genius(string geniusMove, string move, bool done)
{
int geniusAmmo = 1;
geniusMove = "r";
if (geniusMove == "f")
{
geniusAmmo--;
Console.Write("Genius had decided to fire.\nGenius ammo is " + geniusAmmo + "\n");
}
else if (geniusMove == "r")
{
geniusAmmo++;
Console.Write("Genius had decided to reload.\nGenius ammo is " + geniusAmmo + "\n");
}
else if (geniusMove == "s")
{
Console.Write("Genius had decided to shield.\nGenius ammo is " + geniusAmmo + "\n");
}
TestMoves(move, geniusMove, done);
return geniusMove;
}
static bool TestMoves(string move, string geniusMove, bool done)
{
if (move == "s" && geniusMove == "f")
{
Console.Write("No one has died yet");
done = false;
return done;
}
else if (move == "f" && geniusMove == "f")
{
Console.Write("You both died! Good game!");
done = true;
return done;
}
else if (move != "s" && geniusMove == "f")
{
Console.Write("You died! Good game!");
done = true;
return done;
}
else if (move == "f" && geniusMove == "s")
{
Console.Write("No one has died yet");
done = false;
return done;
}
else if (move == "f" && geniusMove != "s")
{
Console.Write("Genius died! Good game!");
done = true;
return done;
}
else if (move != "f" && geniusMove != "f")
{
Console.Write("No one has died yet");
done = false;
return done;
}
else
{
return done;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否需要从
Genius
内部调用TestMoves
而不是从循环中调用?在我看来,您的代码可以这样重写:所有代码的整体流程对我来说有点令人困惑。每个函数都返回一个值,但似乎没有对返回值执行任何操作。我有一种感觉,通过一些重构,你可以使这段代码更短、更符合逻辑。
进一步查看代码后,您似乎可以将对 TestMoves 的调用放在循环的最后:
Is there a reason you need to call
TestMoves
from inside ofGenius
instead of from your loop? It seems to me that your code could be rewritten like this:The overall flow of all the code is a bit confusing to me. You have each function returning a value, but don't appear to be doing anything with the return value. I have a feeling that with a bit of refactoring, you could make this code much shorter and more logical.
After looking at your code a bit more it looks like you could place the call to TestMoves at the very end of your loop:
您可以通过 ref: 传递值
并从 TestMoves 返回它:
调用它:
You can pass the value by ref:
And return it from
TestMoves
:To call it:
由于
bool
是值类型,而不是引用类型,因此您无法像这样传递 bool。使用out
关键字明确指定要将其作为引用传递:http://msdn.microsoft.com/en-us/library/ee332485.aspx
Since
bool
is a value type, not a reference type, you're not able to pass the bool through like that. Use theout
keyword to explicitly specify that you want to pass it as a reference:http://msdn.microsoft.com/en-us/library/ee332485.aspx
您可以使用 ref 或done 参数上的 rel="nofollow">out 关键字
You could use the ref or the out keyword on the
done
parameter