Utmaning: Arrayloopar
Loop through the numbers array and if the value of the element is even, add 1 to it, and if it's odd, multiply it by two. The original array should be modified.
main.cs
123456789101112131415161718192021222324252627using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for(_____) { if(_____) { _____ } else { _____ } } foreach(int i in numbers) { Console.WriteLine(i); } } } }
- All the numbers that give a remainder
0when divided by2are called even numbers. - Use the loop variable
ito index and access the array elements.
main.cs
1234567891011121314151617181920212223242526272829using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for(int i = 0; i < numbers.Length; i++) { if(numbers[i] % 2 == 0) { numbers[i] += 1; } else { numbers[i] *= 2; } } foreach(int i in numbers) { Console.WriteLine(i); } } } }
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 5. Kapitel 8
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Suggested prompts:
Can you show me an example of the `numbers` array?
What programming language should I use for this task?
Can you explain how to use a loop to modify the array in place?
Awesome!
Completion rate improved to 1.59
Utmaning: Arrayloopar
Svep för att visa menyn
Loop through the numbers array and if the value of the element is even, add 1 to it, and if it's odd, multiply it by two. The original array should be modified.
main.cs
123456789101112131415161718192021222324252627using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for(_____) { if(_____) { _____ } else { _____ } } foreach(int i in numbers) { Console.WriteLine(i); } } } }
- All the numbers that give a remainder
0when divided by2are called even numbers. - Use the loop variable
ito index and access the array elements.
main.cs
1234567891011121314151617181920212223242526272829using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for(int i = 0; i < numbers.Length; i++) { if(numbers[i] % 2 == 0) { numbers[i] += 1; } else { numbers[i] *= 2; } } foreach(int i in numbers) { Console.WriteLine(i); } } } }
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 5. Kapitel 8