C Sharp. jede zweite Zahl einlesen
Hallo liebe Gemeinde.
Ich möchte gerne mit einer Schleife eine Zeile einlesen.
Danach jede zweite Position ausgeben.
Als delimiter das Komma.
Bsp. Input
(-21.22,11.11,55.55,33.33,44.44,-66.66,99.99,77.77,100))
Output
11.11,33.33,-66.66,77.77
Vielen Dank für die Mühe
Grüße
Ich möchte gerne mit einer Schleife eine Zeile einlesen.
Danach jede zweite Position ausgeben.
Als delimiter das Komma.
Bsp. Input
(-21.22,11.11,55.55,33.33,44.44,-66.66,99.99,77.77,100))
Output
11.11,33.33,-66.66,77.77
Vielen Dank für die Mühe
Grüße
Please also mark the comments that contributed to the solution of the article
Content-Key: 392187
Url: https://administrator.de/contentid/392187
Printed on: April 28, 2024 at 12:04 o'clock
4 Comments
Latest comment
Guten Abend,
Das ist schön. Warum machst du das dann nicht einfach?
Oder brauchst du für diese Aufgabe hilfe? Wenn ja, dann musst du uns schon mitteilen, wo's hängt.
Poste dann auch deinen aktuellen Code.
Viele Grüße,
Exception
Ich möchte gerne mit einer Schleife eine Zeile einlesen.
Danach jede zweite Position ausgeben.
Als delimiter das Komma.
Danach jede zweite Position ausgeben.
Als delimiter das Komma.
Das ist schön. Warum machst du das dann nicht einfach?
Oder brauchst du für diese Aufgabe hilfe? Wenn ja, dann musst du uns schon mitteilen, wo's hängt.
Poste dann auch deinen aktuellen Code.
Viele Grüße,
Exception
There are a few methods which can be used here are some of them (ex. are using c# console application project):
LINQ variant
for-loop with step
for-loop with if check of remainder of division
Regards
LINQ variant
// file
const string FILE = @"D:\test.txt";
// read file
string strINPUT = System.IO.File.ReadAllLines(FILE);
// remove parentheses, convert all array elements to real numbers and only leave every second item of the array
IEnumerable<double> numbers = Array.ConvertAll(strINPUT.Trim(new char { '(', ')' }).Split(new char { ',' }), x => double.Parse(x, System.Globalization.CultureInfo.GetCultureInfo("en-us"))).Where((x,i) => (i+1) % 2 == 0);
// FOR-Schleife über die Zahlen
foreach(double num in numbers) {
Console.WriteLine(num);
}
Console.ReadLine();
// file
const string FILE = @"D:\test.txt";
// read file
string strINPUT = System.IO.File.ReadAllLines(FILE);
// read first line of file trim parenthesis and split with comma
string numbers = strINPUT.Trim(new char { '(', ')' }).Split(new char { ',' });
// loop through array starting with second item and 2 step incrementor
for (int i = 1; i < numbers.Length; i+=2) {
Console.WriteLine(numbers[i]);
}
Console.ReadLine();
// file
const string FILE = @"D:\test.txt";
// read file
string strINPUT = System.IO.File.ReadAllLines(FILE);
// read first line of file trim parenthesis and split with comma
string numbers = strINPUT.Trim(new char { '(', ')' }).Split(new char { ',' });
// loop through array starting with second item and 2 step incrementor
for (int i = 0; i < numbers.Length; i++) {
if ((i+1) % 2 == 0) {
Console.WriteLine(numbers[i]);
}
}
Console.ReadLine();