do / while和if / else问题

时间:2018-03-02 02:57:49

标签: c# if-statement do-while

  1. 我必须加上额外的测试分数"得到答案。 (即我必须输入6 5'才能获得25)
  2. 我无法获得do / while& if语句如果在"之外有多个数字,则循环#34;范围。我已经编码很长时间了,几周之后所以试着打破答案。谢谢你的帮助!
  3. 这是我的代码

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

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

n是测试次数,这意味着得分计数器i的数量应与测试数量相同。也更喜欢Convert.ToInt32int.Parse,因为它会在无法进行转换的情况下抛出异常。

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

答案 2 :(得分:-1)

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

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

答案 3 :(得分:-2)

有两种选择:

一个。从1开始而不是0 - > for(i = 1; i&lt; n; i ++)

湾将n的值降低1 - > for(i = 1; i 祝你好运