Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 你不是像我想的那样工作吗?_C# - Fatal编程技术网

C# 你不是像我想的那样工作吗?

C# 你不是像我想的那样工作吗?,c#,C#,我在c#中使用的方法有一些问题。我试图阻止用户输入y和n以外的任何内容。这几乎是我想要的工作,但用户仍然可以输入多个标志,然后它就不工作了!如何检查char是否不止一个char?我以为特瑞帕斯解决了这个问题?谢谢 // Method to check if item is food or not private void ReadIfFoodItem() { Console.Write("Enter if food item or not (y/n): ");

我在c#中使用的方法有一些问题。我试图阻止用户输入y和n以外的任何内容。这几乎是我想要的工作,但用户仍然可以输入多个标志,然后它就不工作了!如何检查char是否不止一个char?我以为特瑞帕斯解决了这个问题?谢谢

// Method to check if item is food or not
    private void ReadIfFoodItem()
    {
        Console.Write("Enter if food item or not (y/n): ");

        if (char.TryParse(Console.ReadLine(), out responseFoodItem))
        {
            if(Char.IsNumber(responseFoodItem))
            {
                Console.WriteLine(errorMessage);
                ReadIfFoodItem();
            }
            else
            {
                // Set true or false to variable depending on the response
                if ((responseFoodItem == 'y' || responseFoodItem == 'Y'))
                {
                    foodItem = true;
                    selectedVATRate = 12; // Extra variable to store type of VAT
                }
                else if ((responseFoodItem == 'n' || responseFoodItem == 'N'))
                {
                    foodItem = false;
                    selectedVATRate = 25; // Extra variable to store type of VAT
                }
                else
                {
                    Console.WriteLine(errorMessage);
                    ReadIfFoodItem();
                }
            }
        }
    }

char.TryParse
只是尝试解析作为参数提供的字符串,它不限制您可以使用
console.ReadLine
输入控制台的字符数

当用户输入多个字符时,
char.TryParse
将失败,因为
Console.ReadLine
返回的字符串不包含单个字符

您应该使用
控制台。请阅读

如何检查char是否不止一个char


要读取单个字符,请使用
Console.ReadChar()

Console.ReadLine允许用户键入任意长度的字符串并按enter键

如何检查char是否不止一个char?我以为特瑞帕斯解决了这个问题

发件人:

将指定字符串的值转换为其等效的Unicode字符。返回码指示转换是成功还是失败…如果s参数为null或s的长度不是1,则转换失败。


您是否尝试过使用Console.ReadKey而不是ReadLine?

要检查它,用户插入了多个字符,您可以检查字符串长度而不是使用char.TryParse

......
private void ReadIfFoodItem()
{
 string answer=string.empty;
 Console.Write("Enter if food item or not (y/n): ");
 answer=Console.ReadLine()
 if (answer.lenght>=1))
    {
           //error
          .......
    }   

使用Console.ReadKey()限制字符数。要测试是Y还是N,可以比较ASCII码或使用正则表达式

char
表示一个字符,那么如何检查char是否不止一个字符呢?我以为是锥虫解决了这个问题?
似乎有点荒谬
TryParse
将尝试解析输入中的单个字符,如果值为
null
或长度大于1,则将显式失败

不检查字符,只检查字符串,例如:

string line = Console.ReadLine();
switch (line.ToUpperInvariant())
{
    case "Y":
        // Do work for y/Y
       break;
    case "N":
        // Do work for n/N
        break;
    default:
        // Show error.
        break;
}

这个答案应该能帮助你:


控制台不限制用户输入(当然是限制,但限制为256个字符),也不限制char.TryParse
,它根本不限制输入长度。

您可以尝试使用console.ReadKey
用户只需按一次键,就不会有超过一个字符。

为什么不与输入的字符串进行比较?
为什么不简化比较呢

using System.Linq;

private static string[] ValidAnswers = new string[]{ "y", "yes" };

// Method to check if item is food or not
private void ReadIfFoodItem() {
    Console.Write("Enter if food item or not (y/n): ");
    string ans = Console.ReadLine();
    // Checks if the answer matches any of the valid ones, ignoring case.
    if (PositiveAnswers.Any(a => string.Compare(a, ans, true) == 0)) {
        foodItem = true;
        selectedVATRate = 12; // Extra variable to store type of VAT
    } else {
        foodItem = false;
        selectedVATRate = 25; // Extra variable to store type of VAT
    }
}
或者,正如其他人所说,您可以使用。

以下代码“工作”,因为它可以产生预期的结果

        char responseFoodItem;

        Console.Write("Enter if food item or not (y/n): ");

        if (char.TryParse(Console.ReadLine(), out responseFoodItem))
        {
            // Set true or false to variable depending on the response
            if ((responseFoodItem == 'y' || responseFoodItem == 'Y'))
            {
                Console.WriteLine("foodItem = true");
            }
            else if ((responseFoodItem == 'n' || responseFoodItem == 'N'))
            {
                Console.WriteLine("foodItem = false");
            }
            else
            {
                Console.WriteLine("Unrecognised input");
            }
        }
        else
        {
            Console.WriteLine("Invalid input");
        }

但是,也有人指出,如果要将输入限制为单个键,那么使用
ReadKey
是一个更好的解决方案。这也意味着用户不必按回车/回车键就可以接受输入。

你肯定希望得到很多!如果你想限制用户输入1个字符,你就不能使用
Console.Read()
?也许我应该补充一点,我是C#新手,这个解决方案不是最好的!:)当我只使用Console.Read()时,代码下面会有一条红线!?对不起,这是读键嗨,我正在尝试各种组合来使用控制台。读吧,但我总是得到恩仁下划线?有什么不对劲吗?
        char responseFoodItem;

        Console.Write("Enter if food item or not (y/n): ");

        if (char.TryParse(Console.ReadLine(), out responseFoodItem))
        {
            // Set true or false to variable depending on the response
            if ((responseFoodItem == 'y' || responseFoodItem == 'Y'))
            {
                Console.WriteLine("foodItem = true");
            }
            else if ((responseFoodItem == 'n' || responseFoodItem == 'N'))
            {
                Console.WriteLine("foodItem = false");
            }
            else
            {
                Console.WriteLine("Unrecognised input");
            }
        }
        else
        {
            Console.WriteLine("Invalid input");
        }