【问题标题】:Why does this bool not recognize being used?为什么这个 bool 不能识别被使用?
【发布时间】:2021-01-09 12:34:52
【问题描述】:

我做了一个输入名字和姓氏的例子。为什么即使我添加了 false 和 true 值,分配的变量“EmptyInput”也不能识别它正在被使用?不知道下面的绿色波浪线叫什么。

using System.Collections.Generic;
using System;
using System.Threading;

namespace MyLogbook
{
    public static class Program
    {
        class PInfo
        {
            public string FirstName;
            public string LastName;
        }
        public static void Main(string[] args)
        {

        Console.WriteLine("\tWelcome to your new logbook!");
            Console.WriteLine("\tPlease enter your first and last name.\n");

            PInfo pInfo = new PInfo();
            bool EmptyInput;
            do
            {
                Console.Write("\tFirst name: ");
                pInfo.FirstName = Console.ReadLine();

                Console.Write("\tLast name: ");
                pInfo.LastName = Console.ReadLine();

                if (!string.IsNullOrEmpty(pInfo.FirstName + pInfo.LastName))
                {
                    Console.WriteLine("\n\tHi, " + pInfo.FirstName + " " + pInfo.LastName + ".\n");
                    EmptyInput = false;
                }
                else
                {
                    Console.Write("\n\tEmpty input, please try again.\n\n");
                    EmptyInput = true;
                }
            } while (string.IsNullOrEmpty(pInfo.FirstName + pInfo.LastName));
        }
    }
}

【问题讨论】:

  • 当您将鼠标悬停在绿色“波浪状”上时,是否可能表示您分配了值,但从不使用它们?绿色是警告,错误(如“未分配”)将是红色。

标签: c# .net boolean


【解决方案1】:

也许你的意思是这样做

} while (EmptyInput);

而不是

} while (string.IsNullOrEmpty(pInfo.FirstName + pInfo.LastName));

因为你只是给变量赋值,但从不使用它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多