int[] numbers = { 4, 8, 15, 16, 23, 42 }; foreach (int number in numbers) { int total; total += number; if (number == 42) { bool found = true; } } if (found) { Console.WriteLine("Set contains 42"); } Console.WriteLine($"Total: {total}"); в чем проблема? подскажите
Исходя из данного кода наверное правильно сделать так: bool found = true; int total = 0; int[] numbers = new[] { 4, 8, 15, 16, 23, 42 }; for (int i = 0; i < numbers.Length; i++) { total += numbers[i]; if (numbers[i] == 42) found = true; } if (found) Console.WriteLine("Set contains 42"); Console.WriteLine($"Total: {total}"); // output: 108 Код bool found = true; int total = 0; int[] numbers = new[] { 4, 8, 15, 16, 23, 42 }; for (int i = 0; i < numbers.Length; i++) { total += numbers[i]; if (numbers[i] == 42) found = true; } if (found) Console.WriteLine("Set contains 42"); Console.WriteLine($"Total: {total}"); // output: 108
chupokaberko, Точно, не досмотрел, тогда так ставить: bool found = false; Код bool found = false; ТС уже сам разберётся.