【发布时间】:2015-11-12 03:30:18
【问题描述】:
我确信这很简单,但我不知道我做错了什么。我正在尝试输入从 300 到 850 的有效信用评分,其中 -99 作为标记值,并据此计算平均值。我可能遗漏了一些非常简单的东西(很可能)...请帮忙!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CreditScores
{
class Program
{
static void Main(string[] args)
{
int creditScore;
int totalScore = 0;
int count = 0;
int average;
string user;
Console.WriteLine("Enter credit score; press -99 to exit");
user = Console.ReadLine();
creditScore = Convert.ToInt32(user);
count = ++count;
totalScore += creditScore;
while (creditScore != -99)
{
Console.WriteLine("Enter credit score; press -99 to exit");
user = Console.ReadLine();
creditScore = Convert.ToInt32(user);
count = ++count;
totalScore += creditScore;
while (creditScore < 300 || creditScore > 850)
{
Console.WriteLine("Invalid score; please enter a valid credit score");
user = Console.ReadLine();
creditScore = Convert.ToInt32(user);
count = ++count;
totalScore += creditScore;
}
}
average = totalScore / count;
Console.WriteLine("The average of all {0} scores is {1}", count, average);
}
}
}
【问题讨论】:
-
如果我输入 -99,它总是返回“无效分数;请输入有效的信用分数”并且从不显示平均值。
-
即使是您输入的第一个数字也会发生这种情况吗?
-
如果是我输入的第一个数字,它会说“所有 1 分的平均值是 -99”。
-
尝试运行代码step by step 看看哪里出错了。
标签: c# while-loop average sentinel