C# 将两个数字乘以一个小数

C# 将两个数字乘以一个小数,c#,C#,我正在尝试运行一个程序,将狗的体重乘以寄宿天数,再乘以每磅0.50美元。我想不出如何将费用与体重和天数结合起来。救命啊!这是我到目前为止所做的,也是我的家庭作业。我知道速率丢失了,但我不知道在这个程序中注入到哪里 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplicati

我正在尝试运行一个程序,将狗的体重乘以寄宿天数,再乘以每磅0.50美元。我想不出如何将费用与体重和天数结合起来。救命啊!这是我到目前为止所做的,也是我的家庭作业。我知道速率丢失了,但我不知道在这个程序中注入到哪里

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

namespace ConsoleApplication9
{
class Program
{
    static void Main(string[] args)
    {
        string dogWeight, boardDays;
        Console.Write("Enter dog's weight ");
        dogWeight = Console.ReadLine();
        Console.Write("Enter number of days  ");
        boardDays = Console.ReadLine();
        Console.Write("Total amount is $ ");
        Console.ReadLine();
     }
}

如果我很好地理解了这个问题,那么您应该这样做:

double c= (double)dogWeight*boardDays;
double rate = c*(0.50);
Console.write(rate);
记住,dogWeight可以是十进制值,board days可以是整数,我们应该将它们的乘积输入到double中,以便精确


我希望我能消除你的疑虑

如果你和老师讨论你的问题,你可能会从这个家庭作业中得到更多的好处。他们知道更多关于他们认为他们教给你的东西,以及他们特别想让你学习的概念,因此可以提供更好的建议

也就是说,从你发布的代码来看,你遗漏了两件不同的事情:

  • 您允许用户以
    字符串
    类型输入信息,但计算机将无法对其进行运算。您将需要转换为一些合适的数字类型;对于涉及货币值的这种特定类型的计算,最适合使用
    decimal
    类型。转换可以通过多种方式完成,但最简单的是使用
    decimal.Parse()
    方法
  • 你需要加入每磅的汇率。这不仅包括了解汇率本身,还包括在适当的计算中使用汇率。由于速率是恒定的,而不是由用户输入,因此在声明分配给该值的变量时,可以在程序中使用
    const
    关键字
  • 以下是两段示例代码,说明了上述内容,而不是为您编写整个家庭作业:

    // This will convert from the string the user entered to a decimal
    // value you can use in a calculation. Do something similar for boardDays
    // as well.
    
    decimal dogWeightNumber = decimal.Parse(dogWeight);
    

    利率是“每天每磅美元”,因此将其乘以重量(磅)和停留时间(天)就可以去掉磅和天的单位,只剩下美元,这就是你想要的结果


    希望你能把所有这些都放在你的程序中完成家庭作业。如果没有,我强烈建议您与您的老师会面,以获得更多帮助。帮助你学习是他们的工作,他们能够为你提供最好的课程帮助。

    只需添加另一个变量
    0.5
    。把字符串转换成小数或双精度,然后做你的数学运算。特别是因为这是家庭作业,除非你准确地解释为什么答案是正确的,否则答案对运算并没有真正的帮助。仅仅展示代码并不能帮助他们学习。此外,当向新手提供代码时,您应该格外努力确保您发布的代码能够真正编译。初学者很难知道简单的打字错误和他们不理解的概念之间的区别。
    // This will declare a constant of the correct type and value. Note the M
    // at the end of the literal. This is what C# uses to indicate that the
    // literal value should have the decimal type instead of double (the default)
    const decimal perPoundRate = 0.5M;
    
    // Then you can put all of the values together in a single total cost:
    
    decimal total = dogWeightNumber * perPoundRate * boardDaysNumber;