如何从方法返回布尔值

发布于 2024-11-28 02:23:49 字数 5429 浏览 0 评论 0 原文

我有一个 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;
            }

        }
    }
}

I have a while loop that loops until bool done = true;
In the method TestMoves(), depending on user input, the method returns the bool done as either true or false. However, I do not know how to "send" this value back to the while loop in my Start() method to stop the loop. Here is my code:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心病无药医 2024-12-05 02:23:49

您是否需要从 Genius 内部调用 TestMoves 而不是从循环中调用?在我看来,您的代码可以这样重写:

//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;

Is there a reason you need to call TestMoves from inside of Genius instead of from your loop? It seems to me that your code could be rewritten like this:

//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

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:

                    default:
                        Console.Write("\nInvalid move, try again\n");
                        done = false;
                        break;
                }

                //add it here:
                done = TestMoves(geniusMove, move, done);

            }
            return move;
深空失忆 2024-12-05 02:23:49

您可以通过 ref: 传递值

static string Genius(string geniusMove, string move, ref bool done) ...

并从 TestMoves 返回它:

static bool TestMoves(string move, string geniusMove) ...

调用它:

Genius(geniusMove, move, ref done);

You can pass the value by ref:

static string Genius(string geniusMove, string move, ref bool done) ...

And return it from TestMoves:

static bool TestMoves(string move, string geniusMove) ...

To call it:

Genius(geniusMove, move, ref done);
匿名的好友 2024-12-05 02:23:49

由于 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 the out keyword to explicitly specify that you want to pass it as a reference:

http://msdn.microsoft.com/en-us/library/ee332485.aspx

疯狂的代价 2024-12-05 02:23:49

您可以使用 refdone 参数上的 rel="nofollow">out 关键字

You could use the ref or the out keyword on the done parameter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文