Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to loop while your sum of 100 isn't achieved, so the condition is <code>inputList.Sum() &lt; 100</code>. </p> <p>If the sum and new number is greater than 100, you tell the user. If it's not the case, then you can add them together.</p> <p>Also, you should query the user for an <code>int</code>, unless you really need a decimal number (if it's the case you'll need to check the equality with an epsilon, decimals and 'exactly equal' do not fit together well).</p> <p>This gives the following :</p> <pre><code>static void Main(string[] args) { List&lt;int&gt; inputList = new List&lt;int&gt;(); do { Console.Write("Enter your number: "); int numberToAdd = Convert.ToInt32(Console.ReadLine()); if (numberToAdd + inputList.Sum() &gt; 100) { Console.WriteLine("Number will not be added, {0} is greater than 100", numberToAdd + inputList.Sum()); } else { inputList.Add(numberToAdd); } } while (inputList.Sum() &lt; 100); double sum = inputList.Sum(); Console.WriteLine("The sum is " + sum); double avg = sum / inputList.Count; Console.WriteLine("The average is " + avg); Console.ReadLine(); } </code></pre> <hr> <p>Once cleaned a bit :</p> <pre><code> static void Main(string[] args) { List&lt;int&gt; inputList = AskUserForInput(); PrintSum(inputList); PrintAverage(inputList); Console.ReadLine(); } private static List&lt;int&gt; AskUserForInput() { List&lt;int&gt; inputList = new List&lt;int&gt;(); 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&lt;int&gt; inputList) { return numberToAdd + inputList.Sum() &gt; 100; } private static bool IsSumLowerThan100(List&lt;int&gt; inputList) { return inputList.Sum() &lt; 100; } private static void PrintAverage(List&lt;int&gt; inputList) { double avg = inputList.Sum() / inputList.Count; Console.WriteLine("The average is " + avg); } private static void PrintSum(List&lt;int&gt; inputList) { double sum = inputList.Sum(); Console.WriteLine("The sum is " + sum); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload