如何从一个类方法调用循环的用户输入到另一个类方法?

时间:2020-08-25 15:28:41

标签: c#

我对编程还很陌生,刚刚开始学习C#和C ++。我正在用C#构建一个简单的程序,该程序会提出问题并为用户提供两个选择。我有一个系统,可以使用while循环和switch语句根据问题确定用户是否选择了可行的选项,但是我无法弄清楚如何从UserChoice调用该方法到Program的main方法。这是程序。

using System;
using System.Security.Cryptography.X509Certificates;

namespace Learn_C_Sharp
{
class UserChoice
{
    public static bool loopContinue = true;
    public static void YesOrNo(string v)
    {
        
        Console.WriteLine("Enter 'Y' for yes or 'N' for no.\n");
        char answerYN = Console.ReadLine()[0];
        while (loopContinue)
        {
            switch (answerYN)
            {
                case 'y':
                case 'Y':
                    Console.WriteLine("You have selected yes.\n");
                    loopContinue = !loopContinue;
                    break;

                case 'n':
                case 'N':
                    Console.WriteLine("You have selected no.\n");
                    loopContinue = !loopContinue;
                    break;

                default:
                    loopContinue = true;
                    break;
            }
            break;
        }

    }
    public static void LeftOrRight()
    {
        Console.WriteLine("Enter 'L' for left or 'R' for right.\n");
        char answerLR = Console.ReadLine()[0];
        while (loopContinue)
        {
            switch (answerLR)
            {
                case 'l':
                case 'L':
                    Console.WriteLine("You have selected left.\n");
                    loopContinue = !loopContinue;
                    break;

                case 'r':
                case 'R':
                    Console.WriteLine("You have selected right.\n");
                    loopContinue = !loopContinue;
                    break;

                default:
                    loopContinue = true;
                    break;
            }
            break;
        }

    }
}

class Program
{
   
   
    static void Main(string[] args)
    {
        Console.WriteLine("Enter your name.");
        string name = Console.ReadLine();
        Console.WriteLine("Your name is " + name + ". \n");
        Console.WriteLine("Would you like to try a tasty treat?\n");
        UserChoice treat = new UserChoice();
        
        
       
        
        
    }

}
}

非常感谢您的帮助!谢谢,祝你有美好的一天。

2 个答案:

答案 0 :(得分:1)

您基本上都在两种方法中的两个选项之间进行选择,因此这是使用bool返回类型而不是void的绝好机会。

因此,当您检查它是否以y / Y开头时,可以使用return true;而不是break;。我将第二种方法更改留给您练习:)

您还声明了这两种方法都是静态的,但是在UserChoice中创建了Main的实例,因此可能不需要它们是静态的。而且您有一个多余的参数string v,您不会使用它。

所以

public static void YesOrNo(string v)

成为

public bool YesOrNo()

Main中,您将拥有:

UserChoice treat = new UserChoice();
bool yes = treat.YesOrNo();

我在您的代码中发现的一个错误是,您要求while循环的用户输入 outside ,这意味着它永远不会更新,并且会无限循环。只需在While循环内移动ReadLine。

哦,您应该在两个方法中都将loopContinue保留为局部变量,而不是类字段,因为您无需在方法调用之间保留值(相反,这将导致错误,如果您调用2个连续的方法,因为该值与第一个方法调用的值相同,那就是false)。

答案 1 :(得分:0)

您的意图是学习c#。这也是一个选择。

class UserChoice
{
    public static void YesOrNo()
    {
        bool loopState;
        do
        {
            Console.WriteLine("Enter 'Y' for yes or 'N' for no.\n");
            var choice = Console.ReadKey();

            // string you wants to check character                 
            switch (choice.Key) //choice.KeyChar.ToString().ToUpper() // Then only check 'Y'/'N'
            {
                case ConsoleKey.Y: // Both 'Y' & 'y' accept here;
                    Console.WriteLine("You have selected yes");
                    loopState = false;
                    break;
                case ConsoleKey.N: // Both 'N' & 'n' accept here;
                    Console.WriteLine("You have selected No");
                    loopState = false;
                    break;
                default:
                    loopState = true;
                    break;
            }

        } while (!loopState);
    }
}