Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

c# - Console (Input string was not in correct format)

Hi guys so i'm starting to learn C# and I came up with this problem when I was trying to mix things up

It says "Input string was not in correct format" at the n = Convert.ToInt32(Console.ReadLine());

Here's the whole code

namespace Exercise13
{
    class Program
    {
        static void Main(string[] args)
        {
            char choice;
            Console.Write("What operation would you like to use?");
            Console.WriteLine("
a. Addition 	b. Subtraction 	c.Multiplication 	d.Division");
            choice = (char)Console.Read();
            if (choice == 'a')
            {
                sumValues();
            }
            else if (choice == 'b') 
            {
                minusValues();
            }
            else if (choice == 'c') 
            {
                timesValues();
            }
            Console.ReadLine();

        }
        static void sumValues() 
        {


            int n = 0;
            int sum = 0;
            int i = 0,val = 0;
            Console.Write("How many numbers do you want calculate: ");
            n = Convert.ToInt32(Console.ReadLine());            

            for (i = 0; i < n; i++) 
            {
                Console.Write("
Input number: ");
                val = Convert.ToInt32(Console.ReadLine());
                sum += val;

            }
            Console.Write("
The Answer is: "+sum);

        }
        static void minusValues() 
        {
            int diff = 0, m, z, value;
            Console.Write("How many numbers do you want calculate: ");
            m = int.Parse(Console.ReadLine());
            for (z = 0; z < m; z++)
            {
                Console.Write("
Input number: ");
                value = int.Parse(Console.ReadLine());
                diff -= value;

            }
            Console.Write("
The Answer is: " + diff);

        }
        static void timesValues()
        {
            int prod = 0, e, i, val;
            Console.Write("How many numbers do you want to calculate: ");
            e = Convert.ToInt32(Console.ReadLine());
            for (i = 0; i < e; i++) 
            {
                Console.Write("
Input number: ");
                val = int.Parse(Console.ReadLine());
                prod *= val;
            }
            Console.Write("
The answer is: " + prod);
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use Integer.TryParse to handle the strings potentially not being numbers. Then prompt the user if the input is not parsable to enter valid input.

Convert and Parse both will throw exceptions if the string is not an exact number.

https://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx

        int n = 0;
        int sum = 0;
        int i = 0,val = 0;
        Console.Write("How many numbers do you want calculate: ");
        var isValidNumber = Int32.TryParse(Console.ReadLine(), out n);
        if(!isValidNumber) {
            Console.WriteLine("Invalid number entered!");
        }
        else {
           //Use the number
        }           

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...