Reputation: 51
I have a problem with the task. Firstly here are some guidelines for the task. Input of program: A natural number is loaded from the standard input first n and then n natural numbers each in a new line. Output of program:All loaded numbers divisible by 3 are printed in order on the standard output. Print each number in a separate line. Example I entered the numbers:
100
99
3
4
6
Each number must be in new line.
When I press enter the program must show me which numbers are divisible by 3. Example:
99
3
6
Here is my code
string a;
int n;
while (true)
{
a = Console.ReadLine();
if (a == string.Empty)
break;
n = Convert.ToInt32(a);
{
if (n % 3 == 0)
Console.WriteLine(n);
}
}
Problem is that when I enter a number that is divisible by 3 the program prints it immediately instead of waiting for me to finish entering numbers and only then to print numbers that are divisible by 3.
Upvotes: 2
Views: 151
Reputation: 1024
Show bellow maybe you can find something usefull:
static void Main()
{
var output = string.Empty;
if (int.TryParse(Console.ReadLine(), out int iterations))
{
while (iterations-- > 0) // or iterations > 0 and if number pass then increase the iterations
{
if (int.TryParse(Console.ReadLine(), out int number) /*&& number != 0*/ && number % 3 == 0)
{
output += $"{Environment.NewLine}{number}";
// iterations--;
}
}
}
Console.WriteLine(output);
}
for the people they say string should not be added:
static void Main()
{
var output = new StringBuilder(Environment.NewLine);
if (int.TryParse(Console.ReadLine(), out int iterations))
{
while (iterations-- > 0) // or iterations > 0 and if number pass then increase the iterations
{
if (int.TryParse(Console.ReadLine(), out int number) /*&& number != 0*/ && number % 3 == 0)
{
output.AppendLine($"{Environment.NewLine}{number}");
//iterations--;
}
}
Console.WriteLine(output);
}
}
and solution with list:
static void Main()
{
if (int.TryParse(Console.ReadLine(), out int iterations))
{
//var output = new List<int>();
var length = iterations;
var output = new int[length];
while (iterations > 0) // or iterations > 0 and if number pass then increase the iterations
{
if (int.TryParse(Console.ReadLine(), out int number) /*&& number != 0*/ && number % 3 == 0)
{
//outpu.Add(number);
output[length - iterations] = number;
iterations--;
}
}
Console.WriteLine(string.Join(Environment.NewLine, output));
}
}
Upvotes: 3
Reputation: 51653
You need to restucture your program to first take all inputs and then process them.
You can do it like this:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// storage for all numbers
var numbers = new List<int>();
while (true)
{
var a = Console.ReadLine();
if (a == string.Empty)
break;
// error handling to avoid crashing if non integers are given
try
{
// adding all numbers in case you want to do other things with them
numbers.Add(Convert.ToInt32(a));
}
catch
{
Console.WriteLine($"Not a number: {a}");
}
}
// process the collected numbers
foreach (var n in numbers)
{
if (n % 3 == 0)
Console.WriteLine(n);
}
}
}
Testrun:
2
5
6
99
Apple
Not a number: Apple
102
6
99
102
As suggested in the comments: this will not fullfill the task given, you would need to read one number, then read as many inputs (so no while true: ...
).
Upvotes: 2