Please help me with the task: InputValidation.Battleship

Hello, I’m working on this task:

Write a program that validates a battleship move. It first reads a letter [A–J] or [a–j] and then an integer [1–10], each from the new line. If the input for the move is valid, program should output “Next move is {letter}{number}.” Otherwise, it should output “Letter should be from A to J. Number from 1 to 10. Try again!” The program should continue to ask for and read values for the battleship move until the user inputs valid values for both of them. Pay attention: it should be case insensitive; ‘A’ and ‘a’ are both allowable. Your program should, however, output letter in the same case as it was entered. For example:
>a
>
Letter should be from A to J. Number from 1 to 10. Try again!

>10
>a
Letter should be from A to J. Number from 1 to 10. Try again!

>#
>10
Letter should be from A to J. Number from 1 to 10. Try again!

>c
>5
Next move is c5.

Here is my code:


namespace InputValidation
{
   class Battleship
   {
       static void Main(string[] args)
       {
           var letterAsString = Console.ReadLine();
           var numberAsString = Console.ReadLine();

           char letter;
           int number;


           while (!char.TryParse(letterAsString, out letter) || !int.TryParse(numberAsString, out number)
           || string.IsNullOrEmpty(letterAsString) || string.IsNullOrEmpty(numberAsString)
           || letter > 'j' || number > 10 || number < 1 || !char.IsLetter(letter))


           {
               Console.WriteLine("Letter should be from A to J. Number from 1 to 10. Try again!");
               letterAsString = Console.ReadLine();
               numberAsString = Console.ReadLine();
           }

               Console.WriteLine($"Next move is {letter}{number}.");
       }
   }
}

Can anyone please help me to solve it?

Як на мене, коли ви маєте дуже багато statements (умов перевірки), код набагато важче читати, та шукати в ньому помилки.

Спробуйте розбити ваш код на декілька логічних кроків і у вас вийде знайти помилку. Для прикладу візьмемо схожу задачку:

var finishedCourseAsString = Console.ReadLine();
var ageAsString = Console.ReadLine();
bool finishedCourse;
int age;
while (!bool.TryParse(finishedCourseAsString, out finishedCourse) || !int.TryParse(ageAsString, out age) || age > 65 || age < 5 || !finishedCourse)
{
  Console.WriteLine("You still have lot's of time to finish the course before retirement, keep going!");
  finishedCourseAsString = Console.ReadLine();
  ageAsString = Console.ReadLine();
}

а тепер давайте її трохи перепишемо:

var finishedCourseAsString = Console.ReadLine();
var ageAsString = Console.ReadLine();
bool finishedCourse;
int age;
bool passValidation = false;
while (!passValidation)
{
   if (!bool.TryParse(finishedCourseAsString, out finishedCourse) || 
        !int.TryParse(ageAsString, out age))
    {
       passValidation = false;
    }
    else if (age > 65 || age < 5)
    {
       passValidation = false;
    }
    else if (!finishedCourse)
    {
       passValidation = false;
    }
    else 
    {
       passValidation = true;
    }
  
    if (!passValidation)
    {
        Console.WriteLine("You still have lot's of time to finish the course before retirement, keep going!");
        finishedCourseAsString = Console.ReadLine();
        ageAsString = Console.ReadLine();
    }
}

Більше коду, але перевірки розбиті на невеличкі блоки, які легше перевірити.