c#编写程序,统计5行4列二维数组中奇数的个数和偶数的个数。

第一行: 1 2 3 4
第二行: 6 7 8 9
第三行: 11 12 13 14
第四行: 16 17 18 19
第五行: 3 56 24 1
最新回答
良辰未赏透

2024-05-03 05:40:26

static void Main(string[] args)
{
int odd = 0;
int even = 0;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 4; j++)
if (arr[i][j] % 2 == 0) even++;
else odd++;
Console.WriteLine("偶数有{0}个, 奇数有{1}个", even, odd);
Console.ReadKey();
}
追问
不对啊
追答
哪里不对了....
你如果是没有定义数组的话
前面加一句
int[,] arr = { { 1, 2, 3, 4 }, { 6, 7, 8, 9 }, { 10, 11, 12, 13 }, { 14, 15, 16, 17 }, { 3, 56, 24, 1 } };
就好了

这样
using System;

namespace csharpconsole
{
class Program
{
static void Main(string[] args)
{
int[,] arr = { { 1, 2, 3, 4 }, { 6, 7, 8, 9 }, { 10, 11, 12, 13 }, { 14, 15, 16, 17 }, { 3, 56, 24, 1 } };

int odd = 0;
int even = 0;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 4; j++)
if (arr[i,j] % 2 == 0) even++;
else odd++;
Console.WriteLine("偶数有{0}个, 奇数有{1}个", even, odd);
Console.ReadKey();
}
}
}
陌念念

2024-05-03 05:22:02

private void button1_Click(object sender, EventArgs e)
{
int[,] a = { { 1, 2, 3, 4 }, { 6, 7, 8, 9 }, { 10, 11, 12, 13 }, { 14, 15, 16, 17 }, { 3, 56, 24, 1 } };
int i ,j = 0;
int js=0,os=0;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 4; j++)
{
if (a[i, j] % 2 == 0)
os++;
else js++;
}
}
textBox1.Text = "偶数个数为"+os.ToString()+"奇数个数为"+js.ToString();
}
追问
不用windows窗体  用控制台应用程序  急~~
追答
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[,] a = { { 1, 2, 3, 4 }, { 6, 7, 8, 9 }, { 10, 11, 12, 13 }, { 14, 15, 16, 17 }, { 3, 56, 24, 1 } };
int i, j = 0;
int js = 0, os = 0;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 4; j++)
{
if (a[i, j] % 2 == 0)
os++;
else js++;
}
}
Console.WriteLine("偶数个数为" + os.ToString() + "奇数个数为" + js.ToString());
Console.ReadKey();
}
}
}