我试图让这个程序一遍又一遍地循环,直到arraylist中的双打总和等于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();
}
}
}
答案 0 :(得分: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);
有关此代码的一些注意事项:
答案 1 :(得分: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);
}
答案 2 :(得分: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);
}
}
答案 3 :(得分: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);
您需要修改它,因为我不清楚您的确切要求,但这是一个开始
答案 4 :(得分: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 ");
}
}