尝试比较 C# 中的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14000504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:20:29  来源:igfitidea点击:

Trying to compare chars in C#

c#inputwhile-loopcomparecharacter

提问by JahKnows

I am new to C# I have started learning it to broaden the programming languages to my disposal but I have run into a little problem I did not encounter in neither C nor Java.

我是 C# 的新手我已经开始学习它以扩展我可以使用的编程语言,但是我遇到了一个小问题,我在 C 和 Java 中都没有遇到过。

I am trying to get a user response from the keyboard and then comparing it with probable cases and if none of those cases matched up then I want the user to have to repeat the process until he has entered in a correct value.

我试图从键盘获取用户响应,然后将其与可能的情况进行比较,如果这些情况都不匹配,那么我希望用户必须重复该过程,直到他输入了正确的值。

String input = Console.ReadLine();

while ((input[0] != 'N') || (input[0] != 'Y'))
    input = Console.ReadLine();       
if (input[0] == 'N')
{
    Console.WriteLine("NO");
    Console.ReadKey();
}
else if (input[0] == 'Y')
{
    Console.WriteLine("YES");
    Console.ReadKey();
} 

This is by far not the most efficient way I have tried, i also tried doing a do while loop and many other variants.

到目前为止,这不是我尝试过的最有效的方法,我还尝试过 do while 循环和许多其他变体。

The problem I encounter is that when the while loop is not activated everything works just fine, but when I add it in, it always enters the loop even if the input is N or Y and can never leave the loop even though it is clear that it is wrong.

我遇到的问题是,当 while 循环未激活时,一切正常,但是当我添加它时,即使输入是 N 或 Y,它也总是进入循环,并且永远不会离开循环,即使很明显这是错误的。

Please if someone can give me some insight as to why this is occurring or if someone may propose a better way of doing this it would be greatly appreciated. Thank you.

请如果有人能给我一些关于为什么会发生这种情况的见解,或者如果有人可以提出更好的方法,我将不胜感激。谢谢你。

Karim

卡里姆

采纳答案by Brandon

Right now, the conditional in your while statement will always be true. A good way to test for this is to put a breakpoint where the while loop is being tested, and then "Add watch" to each part of the conditional.

现在,while 语句中的条件将始终为真。对此进行测试的一个好方法是在测试 while 循环的位置放置一个断点,然后“添加监视”到条件的每个部分。

Change

改变

while ((input[0] != 'N') || (input[0] != 'Y'))

to

while ((input[0] != 'N') && (input[0] != 'Y'))

回答by Dan Hunex

Well your condition is the problem. Your two conditions are Ored , meaning if one of the condition is true, then the loop will execute. So you first Or the conditions and Not the result like !(condition1 || condition2) as in below

那么你的条件是问题。您的两个条件是 Ored ,这意味着如果条件之一为真,则循环将执行。所以你首先 Or 条件而不是结果像 !(condition1 || condition2) 如下

        String input = Console.ReadLine();

        while (!((input[0] != 'N') || (input[0] != 'Y')))
        {
            input = Console.ReadLine();
        }

        if (input[0] == 'N')
        {
            Console.WriteLine("NO");
            Console.ReadKey();
        }

        else if (input[0] == 'Y')
        {
            Console.WriteLine("YES");
            Console.ReadKey();
        } 

回答by Mudassir Hasan

The problem is in condition checking.

问题在于条件检查。

while ((input[0] != 'N') || (input[0] != 'Y'))

Suppose 'N'is entered. Now condition (input[0] != 'Y')becomes falseand it should break out of while loop but because of || with (input[0] != 'N')which remains true, the end result in condition comes out to be trueand hence it never breaks out of loop.

假设输入了“N”。现在条件(input[0] != 'Y')变为,它应该跳出 while 循环,但因为 || 与(input[0] != 'N')这仍然是真实的,最终的结果病情出来是真的,因此它永远不会打破循环了。

Replace ||with &&

替换|| &&

回答by Rahul Tripathi

Try this:-

尝试这个:-

String input = Console.ReadLine();
        while ((input[0] != 'N') && (input[0] != 'Y'))
        {
            input = Console.ReadLine();

        }
        if (input[0] == 'N')
        {
            Console.WriteLine("NO");
            Console.ReadKey();
        }
        else if (input[0] == 'Y')
        {
            Console.WriteLine("YES");
            Console.ReadKey();
        }

回答by Ergwun

As everyone has already pointed out, you are using ||where you should be using &&.

正如每个人都已经指出的那样,您正在使用||应该使用的地方&&

Beyond that, you are also attempting to access the first character of a string that is possible empty. This will cause an exception if the user just hits 'Enter'.

除此之外,您还试图访问可能为空的字符串的第一个字符。如果用户只是点击“Enter”,这将导致异常。

Since you asked for a better way, here's one alternative, that uses Console.ReadKeyinstead of Console.ReadLine, as you only seem to be interested in getting a character anyway. It also has the advantage that it is not case-sensitive.

由于您要求更好的方法,这里有一个替代方法,它使用Console.ReadKey代替Console.ReadLine,因为无论如何您似乎只对获取角色感兴趣。它还具有不区分大小写的优点。

while (true)
{
    ConsoleKeyInfo key = Console.ReadKey();
    Console.WriteLine(""); // Just for nice typesetting.

    if (key.Key == ConsoleKey.N)
    {
        Console.WriteLine("NO");
        break;
    }

    if (key.Key == ConsoleKey.Y)
    {
        Console.WriteLine("YES");
        break;
    }
}