Хранить с несколькими словами, принятыми в консольном приложении - PullRequest
0 голосов
/ 10 марта 2020

Я храню строку с несколькими словами (любое из выбранных слов принимается). Но я получил сообщение об ошибке:

Недопустимый термин выражения

Как правильно хранить несколько слов?

string correctDeviceSerialId = ["AEVL2020", "AEVL2021", "AEVL2022" ];
string correctUserId = "";                                            
string repeatDeviceSerialId = "";
while (repeatDeviceSerialId != correctDeviceSerialId)
{
    Console.Write("Enter your Device Serial: ");
    repeatDeviceSerialId = Convert.ToString(Console.ReadLine());
}

Ответы [ 2 ]

2 голосов
/ 10 марта 2020

В коде было несколько ошибок. Я исправил их и добавил комментарий выше.

// WRONG: string correctDeviceSerialId = ["AEVL2020", "AEVL2021", "AEVL2022" ];
// You should have a string array instead of string
string[] correctDeviceSerialId = { "AEVL2020", "AEVL2021", "AEVL2022" }; 
string correctUserId = "";                           
string repeatDeviceSerialId = "";

// WRONG: while (repeatDeviceSerialId != correctDeviceSerialId)
// You were trying to compare string array of strings.
// If you'd like to just check if the string contains in your correctDeviceSerialId
while (!correctDeviceSerialId.Contains(repeatDeviceSerialId))
{
    Console.Write("Enter your Device Serial: ");
    repeatDeviceSerialId = Convert.ToString(Console.ReadLine());
}

PS Также не забудьте добавить using System.Linq в начало вашего файла

1 голос
/ 10 марта 2020

Как правильно хранить несколько слов?

В объявлении в вашем массиве есть синтаксическая ошибка.

replace

string correctDeviceSerialId = ["AEVL2020", "AEVL2021", "AEVL2022" ];

с

string[] correctDeviceSerialId = { "AEVL2020", "AEVL2021", "AEVL2022" };

, который является ярлыком для

string[] correctDeviceSerialId = new string[] { "AEVL2020", "AEVL2021", "AEVL2022" };
...