переключить оператор обратно на значение по умолчанию независимо от ввода пользователя? - PullRequest
0 голосов
/ 28 мая 2019

Я хочу, чтобы программа запрашивала ввод данных пользователем, принимала число 1-7 и выплевывала правильный оператор WriteLine.Все компилируется нормально, но я получаю опцию WriteLine по умолчанию независимо от того, какой номер я ввожу.Куда я иду не так?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace week4discussion
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number: ");
            int userInput = Console.Read();

            switch (userInput) {
                case 1:
                    Console.WriteLine("Your selected course is IT 145");
                    break;
                case 2:
                    Console.WriteLine("Your selected course is IT 200");
                    break;
                case 3:
                    Console.WriteLine("Your selected course is IT 201");
                    break;
                case 4:
                    Console.WriteLine("Your selected course is IT 270");
                    break;
                case 5:
                    Console.WriteLine("Your selected course is IT 315");
                    break;
                case 6:
                    Console.WriteLine("Your selected course is IT 328");
                    break;
                case 7:
                    Console.WriteLine("Your selected course is IT 330");
                    break;
                default:
                    Console.WriteLine("Please enter a number 1-7");
                    break;

            }
        }
    }
}

1 Ответ

4 голосов
/ 28 мая 2019

Вам нужно это:

 int userInput = Convert.ToInt32(Console.ReadLine());
...