繁体   English   中英

代码在使用 Visual Studio 代码的 c# 中意外运行

[英]Code is working unexpectedly in c# using visual studio code

我是 c# 的初学者,我想创建一个简单的计算器。 我已经编写了所有代码,它没有显示任何错误,但是,它没有正确显示。 这是我正在使用的所有代码:

using System;
namespace C_
{
    class Program
    {
        static void Main(string[] args)
        {
           Console.Title = "Calculator"; 

            float num1;
            float num2;
            float resultSum;
            float resultSub;
            float resultProd;
            float resultDiv;


            Console.Write("Enter your first number ");
             
            num1 = Convert.ToInt32(Console.Read());
            num2 = Convert.ToInt32(Console.Read());

            resultSum = num1 + num2;
            Console.Write("The sum is " + resultSum);

            resultSub = num1 - num2;
            Console.Write("The differnce is " + resultSub);

            resultProd = num1 * num2;
            Console.Write("The product is " + resultProd);

            resultDiv = num1 / num2;
            Console.Write("The quotient is " + resultDiv);


            Console.ReadKey();
        }
            
    }
}

当我在没有调试的情况下运行它时,控制台显示:

这是我在没有调试的情况下运行代码时显示的内容

https://docs.microsoft.com/en-us/dotnet/api/system.console.writeline?view=netcore-3.1

如果将Console.Write...替换为Console.WriteLine... ,它将在打印语句的末尾添加换行符,因此您的输出应如下所示:

The sum is 63
The difference is 37 
...

您的代码使用控制台键值,而不是您输入的数值。 2的consolekey是50,return的consolekey是13。

不确定我是否会使用float作为类型。 特别是因为您要转换为int32 我在你的程序中加入了一些不同的技术。 因为你只是在学习这些是很好的事情要知道。 我在代码的注释中提供了解释。

    static void Main(string[] args)
    {
        Console.Title = "Calculator";

        // No need to put the type multiple times
        // Just use a comma to separate the names
        int num1, num2, resultSum, resultSub, resultProd, resultDiv;


        Console.WriteLine("Enter your first number:");

        // This is just a label
        Num1Entry:
        try
        {
            num1 = Convert.ToInt32(Console.ReadLine());
        }
        // This exception is for when you don't get a number from the user. i.e. num1 = a
        catch (FormatException)
        {
            Console.WriteLine("Not a number. Please enter a valid number.");
            // This will jump your program back to the beginning of the try-catch so you can enter a valid number for num1
            goto Num1Entry;
        }
        // This exception is for when the number is out of range for the data type. i.e. num1 = 2147483648 is too big for an int data type.
        catch (OverflowException)
        {
            Console.WriteLine("Invalid number. Please enter a valid number.");
            // This will jump your program back to the beginning of the try-catch so you can enter a valid number for num1
            goto Num1Entry;
        }


        Console.WriteLine("Enter your second number:");

        // This is just a label
        Num2Entry:
        try
        {
            num2 = Convert.ToInt32(Console.ReadLine());
        }
        // This exception is for when you don't get a number from the user. i.e. num2 = a
        catch (FormatException)
        {
            Console.WriteLine("Not a number. Please enter a valid number.");
            // This will jump your program back to the beginning of the try-catch so you can enter a valid number for num2
            goto Num2Entry;
        }
        // This exception is for when the number is out of range for the data type. i.e. num2 = 2147483648 is too big for an int data type.
        catch (OverflowException)
        {
            Console.WriteLine("Invalid number. Please enter a valid number.");
            // This will jump your program back to the beginning of the try-catch so you can enter a valid number for num2
            goto Num2Entry;
        }

        resultSum = num1 + num2;
        Console.WriteLine("The sum is " + resultSum);

        resultSub = num1 - num2;
        Console.WriteLine("The differnce is " + resultSub);

        resultProd = num1 * num2;
        Console.WriteLine("The product is " + resultProd);

        // if num2 = 0 you will get an exception.
        // Use a try-catch to keep your program from failing.
        try
        {
            resultDiv = num1 / num2;
            Console.WriteLine("The quotient is " + resultDiv);
        }
        catch (DivideByZeroException)
        {
            Console.WriteLine("You cannot divide by 0");
        }


        
        Console.ReadKey();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM