从控制台获取c#输入但未返回正确的输出

时间:2016-04-11 19:42:14

标签: c# input

C#程序试图从用户那里获得输入并使用这两个数字来查找产品。

using System;

namespace Program
{
    class MainClass
    {
        static int computeProd1(int num1, int num2)
        {
            return (num1 * num2);
        }

        public static void Main (string[] args)
        {
            int first, second;

            Console.Write ("enter first number: ");
            first = Convert.ToInt32 (Console.ReadLine ());
            Console.Write ("enter 2nd number: ");
            second = Convert.ToInt32 (Console.ReadLine ());

            int product = computeProd1 (first, second);
            //Console.WriteLine ("\n First number is: {0}" , first);
            //Console.WriteLine ("\n Second number is: {0}" , second);
            Console.WriteLine ("\nTheir product is: {0}" , product);
            Console.ReadKey ();     
        }
    }
}

无论我放了什么,第一个和第二个数字都会输出一个数字。

3 个答案:

答案 0 :(得分:1)

也许试试这段代码。

更新

使用int.tryParse()进行更多的故障验证方法。

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

namespace ConsoleApplication1
{
    class Program
    {
        static int computeProd1(int num1, int num2)
        {
            return (num1 * num2);

        }

        public static void Main(string[] args)

        {
            try
            {
                int first, second, product = 0;

                if (int.TryParse(Console.ReadLine(), out first))
                {
                    if (int.TryParse(Console.ReadLine(), out second))
                    {
                        product = computeProd1(first, second);
                    }
                    else
                    {
                        Restart();
                    }
                }
                else
                {
                    Restart();
                }


                Console.WriteLine("Their product is:\t" + product);

                Console.ReadLine();// So it wont close.
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public static void Restart()
        {
            Console.WriteLine("Oops something went wrong thats not a number!");
            Console.WriteLine("Restarting in 3 seconds...");
            System.Threading.Thread.Sleep(3000);
            var fileName =   System.Reflection.Assembly.GetExecutingAssembly().Location;
            System.Diagnostics.Process.Start(fileName);
            Environment.Exit(0);
        }
    }
}

答案 1 :(得分:1)

console.Read返回输入的char的ascii代码而不是int值

尝试使用此代码,它会询问数字(仅限数字),然后返回产品:

class Program
{
    static int computeProd1(int num1, int num2)
    {
        return (num1 * num2);

    }

    public static void Main(string[] args)
    {

        int? first = null, second = null;
        int first_i = 0, second_i = 0;

        while (!first.HasValue)
        {
            Console.WriteLine("enter first number");

            if (int.TryParse(Console.ReadLine(), out first_i))
            {
                first = first_i;
            }
        }

        while (!second.HasValue)
        {
            Console.WriteLine("enter second number");

            if (int.TryParse(Console.ReadLine(), out second_i))
            {
                second = first_i;
            }
        }

        int product = computeProd1(first_i, second_i);
        Console.WriteLine("Their product is:\t" + product);
        Console.Read();
    }
}

答案 2 :(得分:0)

默认情况下,Console.Read()会根据当前character encoding返回输入字符的值。例如,这里是windows-1250的链接,它可以解释为什么你得到48为0等等。

如果您只想使用个位数,可以使用Convert.ToChar()获取实际的键值。对于多位数值,您可以使用int.TryParse()方法。以下是两个例子:

int first, second;
//for single digit inputs
var singleDigitInput1 = Convert.ToChar(Console.Read).ToString();
var singleDigitInput2 = Convert.ToChar(Console.Read).ToString();

//for multi digit inputs
var multiDigitInput1 = Console.ReadLine();
var multiDigitInput2 = Console.ReadLine()

if (int.TryParse(singleDigitInput1, out first) && int.TryParse(singleDigitInput2, out second))
{
    int product = computeProd1(first, second);
    Console.WriteLine("Their product is:\t" + product);
}
else
{
    //input cannot be converted to int
}