在 C# 中的数组中乘以不同的数据类型
Multiplying Different Data Types in an Array in C#
我收到一个错误 "Operator '*' cannot be applied to operands of type 'int' and 'decimal[]'",因为我试图将两个具有不同数据类型的值相乘(一个是位于数组中的值)。我的问题是如何在下面的代码中使用多个 numberOfMinutes * perMinuteRate?我的变量称为 total,我将其声明为 double 数据类型(尽管可能不正确)。
我尝试更改数据类型并尝试格式化(如 ToString),但我不确定该怎么做。我也尝试 google 没有成功的答案。
我绝不是专业的程序员;我不在学校。我是一名正在学习编程的数据分析师。
这是我的代码:
static void Main(string[] args)
{
int[] areaCodes = { 262, 414, 608, 715, 815, 920 };
decimal[] perMinuteRate = { .07m, .1m, .05m, .16m, .24m, .14m };
int numberOfMinutes;
int userAreaCode;
string inputString = "1";
while (inputString != "0")
{
int x;
Console.WriteLine("Enter the area code for your call (or 1 to end):");
inputString = Console.ReadLine();
userAreaCode = Convert.ToInt32(inputString);
Console.WriteLine("How many minutes will your call last?");
inputString = Console.ReadLine();
numberOfMinutes = Convert.ToInt32(inputString);
for (x = 0; x < areaCodes.Length; x++)
{
if (userAreaCode == areaCodes[x])
{
***double total = numberOfMinutes * perMinuteRate;***
Console.WriteLine("You call to {0} will cost {1} per minute for a total of {2}.", areaCodes[x], perMinuteRate[x].ToString("C"), total.ToString("C"));
x = areaCodes.Length;
}
}
if (x != areaCodes.Length)
{
Console.WriteLine("I'm sorry; we don't cover that area.");
inputString = "1";
}
else
{
Console.WriteLine("Thanks for being our customer.");
inputString = "0";
}
Console.ReadLine();
}
}
提前致谢。
变化:
double total = numberOfMinutes * perMinuteRate;
到
double total = (double)(numberOfMinutes * perMinuteRate[x]);
与您在正下方的行中索引 perMinuteRate
的方式相同。
表达式 [int] * [decimal]
将产生一个小数,而转换 (double)
会将其转换为双精度
为避免精度损失,将其更改为:
decimal total = numberOfMinutes * perMinuteRate[x];
我收到一个错误 "Operator '*' cannot be applied to operands of type 'int' and 'decimal[]'",因为我试图将两个具有不同数据类型的值相乘(一个是位于数组中的值)。我的问题是如何在下面的代码中使用多个 numberOfMinutes * perMinuteRate?我的变量称为 total,我将其声明为 double 数据类型(尽管可能不正确)。
我尝试更改数据类型并尝试格式化(如 ToString),但我不确定该怎么做。我也尝试 google 没有成功的答案。
我绝不是专业的程序员;我不在学校。我是一名正在学习编程的数据分析师。
这是我的代码:
static void Main(string[] args)
{
int[] areaCodes = { 262, 414, 608, 715, 815, 920 };
decimal[] perMinuteRate = { .07m, .1m, .05m, .16m, .24m, .14m };
int numberOfMinutes;
int userAreaCode;
string inputString = "1";
while (inputString != "0")
{
int x;
Console.WriteLine("Enter the area code for your call (or 1 to end):");
inputString = Console.ReadLine();
userAreaCode = Convert.ToInt32(inputString);
Console.WriteLine("How many minutes will your call last?");
inputString = Console.ReadLine();
numberOfMinutes = Convert.ToInt32(inputString);
for (x = 0; x < areaCodes.Length; x++)
{
if (userAreaCode == areaCodes[x])
{
***double total = numberOfMinutes * perMinuteRate;***
Console.WriteLine("You call to {0} will cost {1} per minute for a total of {2}.", areaCodes[x], perMinuteRate[x].ToString("C"), total.ToString("C"));
x = areaCodes.Length;
}
}
if (x != areaCodes.Length)
{
Console.WriteLine("I'm sorry; we don't cover that area.");
inputString = "1";
}
else
{
Console.WriteLine("Thanks for being our customer.");
inputString = "0";
}
Console.ReadLine();
}
}
提前致谢。
变化:
double total = numberOfMinutes * perMinuteRate;
到
double total = (double)(numberOfMinutes * perMinuteRate[x]);
与您在正下方的行中索引 perMinuteRate
的方式相同。
表达式 [int] * [decimal]
将产生一个小数,而转换 (double)
会将其转换为双精度
为避免精度损失,将其更改为:
decimal total = numberOfMinutes * perMinuteRate[x];