Ошибка с выводом ошибки - C#

Узнай цену своей работы

Формулировка задачи:

Суть в том,что нужно вывести ошибку если такого поезда нет. Ввожу 9 поезд и оно просто игнорирует,подправте пожалуйста.
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
 
namespace ConsoleApplication1
{
    public struct Train : IComparable
    {
        string name;
        public int number;
        DateTime date;
 
        public Train(string name, int number, DateTime date)
        {
            this.name = name;
            this.number = number;
            this.date = date;
        }
 
        public int CompareTo(object obj)
        {
            Train a = (Train)obj;
 
            if (this.number.CompareTo(a.number) == -1) return -1;
            if (this.number.CompareTo(a.number) == 0) return 0;
            else return 1;
        }
 
        public override string ToString()
        {
            return String.Format("Название пункта назначения " + this.name + " \n \tНомер поезда " + number + " \n \tВремя отправления" + date);
        }
    }
 
    class Program
    {
        static void Main()
        {
            ArrayList nL = new ArrayList();
 
            for (int i = 0, k = 8; i < 8; i++)
            {
                nL.Add(new Train("name" + i.ToString(), k, DateTime.Now));
                k--;
            }
 
            foreach (Train c in nL)
            {
                //Console.WriteLine(c.ToString());
            }
 
            Console.WriteLine("\nСортировка по номеру поезда");
            nL.Sort();
 
            foreach (Train c in nL)
            {
                Console.WriteLine(c.ToString());
            }
 
            string str;
            bool b = true;
            int count = 0;
            while (b)
            {
                Console.WriteLine("\n\nВведите номер поезда для поиска");
                str = Console.ReadLine();
 
                for (int i = 0; i < str.Length; i++)
                {
                    if (char.IsDigit(str[i]) && (Convert.ToInt32(str[i]) <= 8 || Convert.ToInt32(str[i]) >= 0)) count++;
                }
 
                if (count == str.Length)
                {
                    foreach (Train c in nL)
                    {
                        if (str == c.number.ToString()) Console.WriteLine(c.ToString());
                    }
                    b = false;
                }
                else Console.WriteLine("Повторите попытку");
            }
 
            Console.ReadLine();
        }
    }
}

Решение задачи: «Ошибка с выводом ошибки»

textual
Листинг программы
(Convert.ToInt32(str[i]) <= 8 && Convert.ToInt32(str[i]) >= 0)

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 4.091 из 5