首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >做/同时和如果/其他问题

做/同时和如果/其他问题
EN

Stack Overflow用户
提问于 2018-03-02 02:57:48
回答 4查看 85关注 0票数 0
  1. 我必须加一个额外的“考试分数”才能得到答案。(也就是说,我必须输入6-5‘才能得到25)
  2. 如果在"while“范围外有多个数字,则无法获得do/while & if语句循环。我已经很久没有编码了,几个星期了,所以试着把答案分解一下。谢谢你的帮助!

这里是我的代码

代码语言:javascript
复制
Console.Write("Enter the number of tests: ");
int n = int.Parse(Console.ReadLine());
int[] scores = new int[n];
Console.WriteLine("----------------------------------------");
Console.WriteLine("Please enter the test scores");
int i;

do
{
    i = Convert.ToInt32(Console.ReadLine());
    if (i < 0)
    {
        Console.WriteLine("Please enter a value greater than 0");

     }

    if (i > 100)
    {
        Console.WriteLine("Please enter a value less than 100");

    }
} while (i < 0 || i > 100);


for (i = 0; i < n; i++)
{
    scores[i] = int.Parse(Console.ReadLine());

}
int sum = 0;
foreach (int d in scores)
{
    sum += d;
}
Console.WriteLine("The sum of all the scores is {0}",sum);
Console.ReadLine();
EN

回答 4

Stack Overflow用户

发布于 2018-03-02 03:08:31

将执行输入验证的do块放在for循环中:

代码语言:javascript
复制
Console.Write("Enter the number of tests: ");
int n = int.Parse(Console.ReadLine());
int[] scores = new int[n];

Console.WriteLine("----------------------------------------");
Console.WriteLine("Please enter the test scores");

for (int i = 0; i < n; i++)
{
    int input = -1;
    do
    {
        input = Convert.ToInt32(Console.ReadLine());
        if (input < 0)
        {
            Console.WriteLine("Please enter a value greater than 0");    
        }    
        else if (input > 100)
        {
            Console.WriteLine("Please enter a value less than 100");    
        }
    } while (input < 0 || input > 100);

    scores[i] = input;
}

int sum = 0;
foreach (int d in scores)
{
    sum += d;
}

Console.WriteLine("The sum of all the scores is {0}", sum);
Console.ReadLine();
票数 2
EN

Stack Overflow用户

发布于 2018-03-02 03:13:27

n是测试的数量,这意味着记分计数器i的数量应该与测试的数量相同。另外,它更喜欢Convert.ToInt32而不是int.Parse,因为它会抛出异常,以防无法进行转换。

代码语言:javascript
复制
Console.Write("Enter the number of tests: ");
int n = Convert.ToInt32(Console.ReadLine());
int sum = 0, i = 0, score = 0;
int[] scores = new int[n];
Console.WriteLine("----------------------------------------");

do {
    Console.WriteLine("Please enter the test score #" + (i + 1));
    score = Convert.ToInt32(Console.ReadLine());

    if (score < 0) {
        Console.WriteLine("Please enter a value greater or equal to 0");
    }
    else if (score > 100)
    {
        Console.WriteLine("Please enter a value less or equal to 100");
    }
    else {
        scores[i] = score;
        i++;
    }
} while (i < n);

foreach (int d in scores) {
    sum += d;
}

Console.WriteLine("The sum of all the scores is {0}", sum);
Console.ReadLine();
票数 1
EN

Stack Overflow用户

发布于 2018-03-02 03:38:38

只是为了潜在的学习机会,而不是直接回答你的问题,下面是我做这个练习的方法:

代码语言:javascript
复制
Console.Write("Enter the number of tests: ");
int n = int.Parse(Console.ReadLine());

Console.WriteLine("----------------------------------------");
Console.WriteLine("Please enter the test scores");

int AskForInteger()
{
    while (true)
    {
        if (int.TryParse(Console.ReadLine(), out int i) && i >= 0 && i <= 100)
        {
            return i;
        }
        Console.WriteLine("Please enter a value between 0 and 100 inclusive");
    }
}

int[] scores =
    Enumerable
        .Range(0, n)
        .Select(x => AskForInteger())
        .ToArray();

Console.WriteLine("The sum of all the scores is {0}", scores.Sum());
Console.ReadLine();
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49062131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档