- //先声明一个数组
- int[] number1 = { 1,2,3,4,5,6,7,8,9};
- //再声明一个存储奇数的数组
- int[] number2 = new int[number1.Length];
- int j=0;
- for (int i = 0; i < number1.Length; i++)
- {
- //判断number1数组里的奇偶数,奇数就存到number2数组里
- if (number1[i] % 2 != 0)
- {
- number2[j] = number1[i];
- j++;
- }
-
- }
- //输出
- for (int i = 0; i < j; i++)
- {
- Console.WriteLine(number2[i]);
- }
- Console.ReadKey();
复制代码 |