将两个数乘以十进制数

时间:2015-02-22 02:48:19

标签: c#

我正在尝试运行一个程序,将狗的体重乘以每磅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();
     }
}

2 个答案:

答案 0 :(得分:2)

如果您与老师讨论您的问题,您可能会从这项功课中获得更多好处。他们更多地了解他们认为他们教给你的东西,以及他们试图让你学习什么概念,因此可以提供更好的建议。

那就是说,从你发布的代码中,你会发现一些不同的东西:

  1. 您允许用户以string类型输入信息,但计算机无法使用该数据进行数学计算。您需要转换为某种合适的数字类型;对于这种特定类型的计算,涉及货币价值时,decimal类型最合适。转换可以通过多种方式完成,但最简单的方法是使用decimal.Parse()方法。
  2. 您需要加入每磅费率。这不仅涉及了解速率本身,还涉及在适当的计算中使用它。由于费率是常数,而不是由用户输入,因此在声明分配给该值的变量时,您可以在程序中使用const关键字。
  3. 不是为你编写整个家庭作业,而是以下几个代码示例:

    // 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);
    


    // 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;
    

    费率是"每磅每天",所以将它乘以重量(磅)和停留时间(天)除去磅和天数单位,让你只需要美元,这是你想要的结果。

    希望您可以将所有这些放在您的计划中以完成作业。如果没有,我强烈鼓励您与老师见面以获得额外帮助。帮助您学习是他们的工作,他们能够为您提供课程作业的最佳帮助。

答案 1 :(得分:-1)

如果我理解问题,那么你应该这样做:

double c= (double)dogWeight*boardDays;
double rate = c*(0.50);
Console.write(rate);

请记住,dogWeight可以是十进制值,也可以是整数上的板数天,我们应该将它们的产品拼写为double,以便精确。

我希望我明白你的疑问!

相关问题