When I debug this console application, it gives a strange result. Obviously something that I donβt notice.
class Program { static void Main(string[] args) { int ops = int.Parse(Console.ReadLine()); int inte = 0; StringBuilder roll = new StringBuilder(); Operations(roll, inte, ops); } static void Operations(StringBuilder roll, int inte, int ops) { for (int i = 0; i < ops; i++) { roll.AppendLine(Console.ReadLine()); inte = roll.ToString().IndexOf("^"); if (inte == i) roll.Remove(i, 1); if (roll.ToString().IndexOf("!") == i) roll.Remove(i, 1); } Console.WriteLine(roll.ToString()); Console.ReadLine(); } }
To enter, enter "3" for the first input (integer "inte") and "^ A", "^ B" "^ D" for the remaining 3 ReadLine entries. At the first iteration of the loop, it will remove ^ as expected. But in the second iteration (i = 1) it reads inte as 3. But it should be 1, because in the second round Stringbuilder adds β^ Bβ to A, so now it is βA ^ B". I try to remove the "^" (and "!"), Too, when they are entered, when the string block adds strings. Why is "inte" an integer reading of 3 in the second round?
source share