本文介绍了我的前卫。在c#的switch case中,我得到的错误不能隐式地将类型'string'转换为'int'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(String[]args)
        {
            int n;
            Console.WriteLine("enter your choice ");
            n =   Console.ReadLine();
            switch (n)
            {
                case 1: if (n == 1)
                        Console.WriteLine("1:Monday");
                    break;


                case 2: if (n == 2)
                        Console.WriteLine("2:Tuesday");
                    break;

                case 3: if (n == 3)
                        Console.WriteLine("3:Wednesday");
                    break;

                case 4: if (n == 4)
                        Console.WriteLine("4:Thursday");
                    break;


                case 5: if (n == 5)
                        Console.WriteLine("5:friday");
                    break;


                case 6: if (n == 6)
                        Console.WriteLine("6:saturday");
                    break;

                case 7: if (n == 7)
                        Console.WriteLine("7:sunday");
                    break;

                default: Console.WriteLine("wrong choice");
                    break;
            }
        }
    }
}





已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

static void Main(String[]args)
{
   string input;
   Console.WriteLine("enter your choice ");
   input = Console.ReadLine();
   int n;
   if (int.TryParse(input, out n)) {
      switch (n) {
         case 1:
            Console.WriteLine("1:Monday");
            break;

         case 2:
            Console.WriteLine("2:Tuesday");
            break;

         case 3:
            Console.WriteLine("3:Wednesday");
            break;

         case 4:
            Console.WriteLine("4:Thursday");
            break;

         case 5:
            Console.WriteLine("5:friday");
            break;

         case 6:
            Console.WriteLine("6:saturday");
            break;

         case 7:
            Console.WriteLine("7:sunday");
            break;

         default:
            Console.WriteLine("wrong choice");
            break;
      }
   }
   else {
      Console.WriteLine("Wrong input! You did not seem to enter a valid integer.");
   }
}





希望这会有所帮助。



Hope this helps.


int n;
Console.WriteLine("enter your choice ");
string s = Console.ReadLine();
if (!int.TryParse(s, out n))
    {
    Console.WriteLine("\"{0}\" is not a number", s);
    }
else
    {
    switch (n)
        {
        case 1: if (n == 1)
                Console.WriteLine("1:Monday");
            break;


        case 2: if (n == 2)
                Console.WriteLine("2:Tuesday");
            break;

        case 3: if (n == 3)
                Console.WriteLine("3:Wednesday");
            break;

        case 4: if (n == 4)
                Console.WriteLine("4:Thursday");
            break;


        case 5: if (n == 5)
                Console.WriteLine("5:friday");
            break;


        case 6: if (n == 6)
                Console.WriteLine("6:saturday");
            break;

        case 7: if (n == 7)
                Console.WriteLine("7:sunday");
            break;

        default: Console.WriteLine("wrong choice");
            break;
        }
    }


这篇关于我的前卫。在c#的switch case中,我得到的错误不能隐式地将类型'string'转换为'int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 07:24