how I can modify my loop to print only one copy of number(lineNum) if number does not meet the condition (lineNum % element.number == 0). I want to make FizzBuzzEndFile.txt to look like this: 1 2 Fizz 4 Buzz 6 Fuzz 8 Fizz
etc.
Код: Выделить всё
using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Diagnostics.Eventing.Reader; using System.Diagnostics.PerformanceData; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FB3 { public class MyClass { public int number; public string response; } internal class Program { /// /// This program is checking list of numbers from txt file and converting it to new txt file /// /// including string type from txt file and int type from converted string /// new txt file with replacment of prime numbers to words: Fizz, Buzz, Fuzz, Bizz. static void Main(string[] args) { StreamReader sr = new StreamReader("C:\\Users\\45714\\Desktop\\FB3\\FB3\\FizzBuzz.txt"); StreamWriter sw = new StreamWriter("C:\\Users\\45714\\Desktop\\FB3\\FB3\\FizzBuzzEndFile.txt", false); var listNW = new List() { (3, "Fizz"), (5, "Buzz"), (7, "Fuzz"), (11, "Bizz"), (13, "Fezz"), }; string lineStr; int lineNum; bool isPrime = true; while ((lineStr = sr.ReadLine()) != null) { StringBuilder sb = new StringBuilder(); lineNum = Convert.ToInt32(lineStr); foreach (var element in listNW) { if (lineNum % element.number == 0) { sb.Append(element.response); } else { sb.Append(lineNum); } } sw.WriteLine(sb); } sw.Close(); sr.Close(); } } } Источник: https://stackoverflow.com/questions/781 ... rs-program
Мобильная версия