我有一个循环,循环直到bool done = true; 在方法TestMoves()中,根据用户输入,该方法将bool作为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;
}
}
}
}
答案 0 :(得分:2)
您是否有理由需要从TestMoves
内部而不是从您的循环中调用Genius
?在我看来,您的代码可以像这样重写:
//Every instance of:
string geniusMove = "";
Genius(geniusMove, move, done);
Console.ReadLine();
//seems like it could be rewritten as:
string geniusMove = "";
Genius(geniusMove, move, done);
done = TestMoves(geniusMove, move, done);
Console.ReadLine();
//and then you can remove the call to TestMoves from Genius
所有代码的整体流程对我来说有点混乱。每个函数都返回一个值,但似乎没有对返回值执行任何操作。我有一种感觉,通过一些重构,你可以使这段代码更短更合理。
看了你的代码后,看起来你可以在循环结束时调用TestMoves:
default:
Console.Write("\nInvalid move, try again\n");
done = false;
break;
}
//add it here:
done = TestMoves(geniusMove, move, done);
}
return move;
答案 1 :(得分:0)
您可以通过ref:
传递值static string Genius(string geniusMove, string move, ref bool done) ...
从TestMoves
返回
static bool TestMoves(string move, string geniusMove) ...
要打电话:
Genius(geniusMove, move, ref done);
答案 2 :(得分:0)
由于bool
是值类型而不是引用类型,因此您无法像这样通过bool。使用out
关键字明确指定您要将其作为参考传递:
答案 3 :(得分:0)