简体   繁体   English

双数据类型计算器C#

[英]Double data type calculator C#

Hello I am fairly new to C# programming and I need some help with my calculator. 您好,我是C#编程的新手,我的计算器需要一些帮助。 I'm try to use double data members and method, but when I use it nothing happens. 我尝试使用双重数据成员和方法,但是当我使用它时,什么也没有发生。 Could someone explain what I am doing wrong? 有人可以解释我在做什么错吗?

static void Main(string[] args)
    {
        Double FirstOperand;
        Double SecondOperand;
        Double Result;

        FirstOperand = Convert.ToDouble(Console.ReadLine());
        Console.Write("What is your first number?");
        SecondOperand = Convert.ToDouble(Console.ReadLine());
        Console.Write("What is your second number?");
        Result = FirstOperand + SecondOperand;
        Console.Write("Result is {0}", Result);
    }

You're asking for input before printing the message that asks for input. 打印要求输入的消息之前,您需要输入信息。 Swap those operations: 交换这些操作:

    Console.Write("What is your first number?");
    FirstOperand = Convert.ToDouble(Console.ReadLine());
    Console.Write("What is your second number?");
    SecondOperand = Convert.ToDouble(Console.ReadLine());
    Result = FirstOperand + SecondOperand;
    Console.Write("Result is {0}", Result);

The next steps in your journey could be to add error handling/retry logic, formatting, etc. 您旅程的下一步可能是添加错误处理/重试逻辑,格式化等。

The problem is that you are reading the value before asking for the value. 问题是您在索要值之前先读取了该值。 So when your application starts the first thing that would happen is that it would request for the value so nothing would show up on your screen. 因此,当您的应用程序启动时,发生的第一件事是它将请求该值,因此屏幕上不会显示任何内容。 it would be waiting for you to enter a value. 它将等待您输入一个值。 So the correct code should be 所以正确的代码应该是

    static void Main(string[] args)
{
    Double FirstOperand;
    Double SecondOperand;
    Double Result;

    Console.Write("What is your first number?");
    FirstOperand = Convert.ToDouble(Console.ReadLine());

    Console.Write("What is your second number?");
    SecondOperand = Convert.ToDouble(Console.ReadLine());
    Result = FirstOperand + SecondOperand;
    Console.Write("Result is {0}", Result);
}

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

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