0

我试图让这个程序一遍又一遍地循环,直到数组列表中双精度数的总和等于 100。基本上我希望用户输入任何数字,但是一旦它达到 100,它就不会再添加了。或者,如果他们添加 50,然后尝试添加 51,它会告诉他们超过 100。我尝试了许多不同的循环,但我仍然无法让它正常工作。我只需要一个正确的方向。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            List<double> myList = new List<double>();
            do
            {
                Console.Write("Enter your number: ");
                myList.Add(Convert.ToDouble(Console.ReadLine()));

            } while (myList.Sum() >= 100);
            double sum = myList.Sum();
            Console.WriteLine("The sum is " + sum);

            double avg = sum / myList.Count;
            Console.WriteLine("The average is " + avg);
            Console.ReadLine();
        }
    }
}
4

5 回答 5

0

这将读取所有输入的数字并将它们加在一起。一旦总和大于(或等于 100),它将退出。

static void Main(string[] args)
{
    List<double> myList = new List<double>();
    var sum = 0;
    do
    {
        Console.Write("Enter your number: ");
        myList.Add(Convert.ToDouble(Console.ReadLine()));

        sum = myList.Sum();

        if (sum > 100)
        {
            Console.Write("Your sum is greater than 100.");
        }
    } while (sum <= 100)

    Console.WriteLine("The sum is " + sum);

    double avg = sum / myList.Count;
    Console.WriteLine("The average is " + avg);
}
于 2013-10-29T17:51:46.210 回答
0

您需要在未达到 100 的总和时进行循环,因此条件为inputList.Sum() < 100.

如果总和和新数字大于 100,则告诉用户。如果不是这种情况,那么您可以将它们加在一起。

此外,您应该向用户查询int,除非您确实需要一个十进制数(如果是这种情况,您需要使用 epsilon 检查相等性,小数和“完全相等”不能很好地结合在一起)。

这给出了以下内容:

static void Main(string[] args)
{
    List<int> inputList = new List<int>();

    do
    {
        Console.Write("Enter your number: ");
        int numberToAdd = Convert.ToInt32(Console.ReadLine());

        if (numberToAdd + inputList.Sum() > 100)
        {
            Console.WriteLine("Number will not be added, {0} is greater than 100", numberToAdd + inputList.Sum());
        }
        else
        {
            inputList.Add(numberToAdd);
        }

    } while (inputList.Sum() < 100);

    double sum = inputList.Sum();
    Console.WriteLine("The sum is " + sum);

    double avg = sum / inputList.Count;
    Console.WriteLine("The average is " + avg);

    Console.ReadLine();
}

稍微清理一下:

    static void Main(string[] args)
    {
        List<int> inputList = AskUserForInput();
        PrintSum(inputList);
        PrintAverage(inputList);

        Console.ReadLine();
    }

    private static List<int> AskUserForInput()
    {
        List<int> inputList = new List<int>();

        do
        {
            Console.Write("Enter your number: ");
            int numberToAdd = Convert.ToInt32(Console.ReadLine());

            if (IsGreaterThan100(numberToAdd, inputList))
            {
                Console.WriteLine("Number will not be added, {0} is greater than 100", numberToAdd + inputList.Sum());
            }
            else
            {
                inputList.Add(numberToAdd);
            }
        } while (IsSumLowerThan100(inputList));

        return inputList;
    }

    private static bool IsGreaterThan100(int numberToAdd, List<int> inputList)
    {
        return numberToAdd + inputList.Sum() > 100;
    }

    private static bool IsSumLowerThan100(List<int> inputList)
    {
        return inputList.Sum() < 100;
    }

    private static void PrintAverage(List<int> inputList)
    {
        double avg = inputList.Sum() / inputList.Count;
        Console.WriteLine("The average is " + avg);
    }

    private static void PrintSum(List<int> inputList)
    {
        double sum = inputList.Sum();
        Console.WriteLine("The sum is " + sum);
    }
}
于 2013-10-29T17:52:25.150 回答
0

你的循环需要看起来更像这样

    do
    {
        Console.Write("Enter your number: ");

        double num = Convert.ToDouble(Console.ReadLine());
        if ((myList.Sum() + num) <= 100)
            myList.Add(num);
        else
            break;

    } while (true);

你需要修改它,因为我不清楚你的确切要求,但这是一个开始

于 2013-10-29T17:52:55.710 回答
0

检查循环内数字的总和,如果总和超过您的配额,则写入消息:

double sum = 0.0;
bool continue = true;
do
{
    Console.Write("Enter your number: ");
    var input = Console.ReadLine();
    if (input.Length > 0)
    {
        double next = Convert.ToDouble(input);
        if (sum + next <= 100)
        {
            sum += next;
            myList.Add(next);
        }
        else
        {
            Console.WriteLine("Input exceeds quota.");
        }
    }
    else
    {
        continue = false;
    }
} while (continue);

关于这段代码的一些注意事项:

  • 这将继续读取输入,直到用户在没有任何输入的情况下简单地按下返回。
  • 我没有在每次迭代中重新计算总和,而是使用局部变量。
  • 它允许用户输入一个新值,如果超过配额,则输入一个较小的值。
于 2013-10-29T17:41:58.450 回答
0

试试这个循环:

     while (true)
        {
            Console.Write("Enter your number: ");
            double temp = Convert.ToDouble(Console.ReadLine());
            if (myList.Sum() + temp <= 100)
            {
                myList.Add(temp);
                if (myList.Sum() >= 100)
                {
                    break;
                }
            }
            else
            {
                Console.Write("You Exceed The Limit ");
            }
        }
于 2013-10-29T17:53:04.663 回答