C#如何修改此代码以允许用户输入十进制数(3.0、2.5等)?

C#如何修改此代码以允许用户输入十进制数(3.0、2.5等)?,c#,C#,我是C#的新手,所以请温柔一点!我正在使用sentinel控制回路编写一个简化的GPA计算器。到目前为止,它仍然有效,但我希望用户能够输入带小数点的分数(例如2.3或3.5),并且仍然能够输入不带小数点的数字。任何建议都将不胜感激!谢谢 代码: 如果您希望用户能够输入带有小数点的数字,则需要使用支持小数点的类型,如十进制(就像您已经为等级总数)所做的那样)。为此,您需要更改解析和存储用户输入的方式,方法是将int更改为decimal: decimal[] score = new decimal[

我是C#的新手,所以请温柔一点!我正在使用sentinel控制回路编写一个简化的GPA计算器。到目前为止,它仍然有效,但我希望用户能够输入带小数点的分数(例如2.3或3.5),并且仍然能够输入不带小数点的数字。任何建议都将不胜感激!谢谢 代码:


如果您希望用户能够输入带有小数点的数字,则需要使用支持小数点的类型,如
十进制
(就像您已经为
等级总数
)所做的那样)。为此,您需要更改解析和存储用户输入的方式,方法是将
int
更改为
decimal

decimal[] score = new decimal[100];

if (decimal.TryParse(inValue, out score[scoreCnt]) == false)
但是你还需要做一些其他的改变。当我们说
gradeTotal+=decimal.Parse(inValue)时,如果用户输入非数字字符串,我们将得到一个异常。我们希望避免异常,因此应该使用
TryParse
,就像我们在
if
条件中已经做的那样

事实上,我们可以通过在前面输入
while
循环,并使用已经存在的
TryParse
来重用我们已经拥有的一些代码

另外,如果输入无效,为什么要在数组中存储任何数据(零)?我们可以先将它存储在一个临时变量中,并且只有在它有效的情况下才将它添加到数组中。此外,我们应该记住,除了非数字输入之外,还有无效数据。我们可以添加一些附加条件,以确保数字介于0和4之间

我们可以删除的另外两个变量是
gradeTotal
scoreCnt
变量。我们总是可以通过对数组中的项目求和得到总数,通过使用数组中的项目计数得到分数计数(假设我们不添加无效项目)

因此,为什么要使用数组呢?
列表
更简单,因为我们不必事先声明任何设置大小。在当前的实现中,我们限制为100个条目,包括无效条目

下面是一些包含这些更改的代码:

var scores = new List<decimal>();

Console.WriteLine("When entering grades, use a 0-4 scale. Remember:");
Console.WriteLine("A = 4, B = 3, C = 2, D = 1, F = 0\n");

Console.Write("Enter grade 1 (or 'X' to exit): ");
var inValue = Console.ReadLine();

while (inValue.ToUpper() != "X")
{
    decimal input;

    if (!decimal.TryParse(inValue, out input) || input < 0 || input > 4)
    {
        Console.WriteLine("Invalid data; enter a number from 0 to 4, or 'X' to exit."); 
    }
    else
    {
        scores.Add(input);
    }

    Console.Write("Enter grade {0} (or 'X' to exit): ", scores.Count + 1);
    inValue = Console.ReadLine();
}

Console.WriteLine("The number of scores: " + scores.Count);
Console.WriteLine("Your grade total is: " + scores.Sum());
Console.WriteLine("Your GPA is: {0:0.00}", scores.Average());

Console.ReadLine();
var分数=新列表();
WriteLine(“当输入成绩时,使用0-4分制。记住:”);
WriteLine(“A=4,B=3,C=2,D=1,F=0\n”);
控制台。写入(“进入1级(或“X”退出):”;
var inValue=Console.ReadLine();
while(inValue.ToUpper()!=“X”)
{
十进制输入;
如果(!decimal.TryParse(无效,输出输入)| |输入<0 | |输入>4)
{
Console.WriteLine(“无效数据;输入0到4之间的数字,或输入“X”以退出”);
}
其他的
{
分数。添加(输入);
}
Write(“输入等级{0}(或“X”退出):”,scores.Count+1);
inValue=Console.ReadLine();
}
Console.WriteLine(“分数数:+scores.Count”);
Console.WriteLine(“你的总成绩是:+scores.Sum());
WriteLine(“你的GPA是:{0:0.00}”,scores.Average());
Console.ReadLine();

尝试使用双精度而不是整数
int[]分数=新整数[100]。当您将浮点值(带小数点的值)存储在
int
数组中时,您希望用户如何输入它们?谢谢!将其更改为浮动,它可以工作!1) 只解析一次精确的变量,例如:
decimalscore;bool success=小数点后三分(无效,超出分数)
然后您可以重复使用结果,而无需再次解析它。2) 总是解析到正确的类型。对于您来说,这是
双精度
十进制
。在两个位置解析为
decimal
,在另一个位置解析为
int
。因此,你似乎部分了解,但需要保持一致。(比如不要混合使用浮点和十进制)3)
int[]score
似乎根本不被使用。不要把你不打算使用的无用变量放在身边。
var scores = new List<decimal>();

Console.WriteLine("When entering grades, use a 0-4 scale. Remember:");
Console.WriteLine("A = 4, B = 3, C = 2, D = 1, F = 0\n");

Console.Write("Enter grade 1 (or 'X' to exit): ");
var inValue = Console.ReadLine();

while (inValue.ToUpper() != "X")
{
    decimal input;

    if (!decimal.TryParse(inValue, out input) || input < 0 || input > 4)
    {
        Console.WriteLine("Invalid data; enter a number from 0 to 4, or 'X' to exit."); 
    }
    else
    {
        scores.Add(input);
    }

    Console.Write("Enter grade {0} (or 'X' to exit): ", scores.Count + 1);
    inValue = Console.ReadLine();
}

Console.WriteLine("The number of scores: " + scores.Count);
Console.WriteLine("Your grade total is: " + scores.Sum());
Console.WriteLine("Your GPA is: {0:0.00}", scores.Average());

Console.ReadLine();