用C#写一个程序,输入5个数,确定和显示正数,负数和0的个数
2个回答
2013-08-19
展开全部
新建一个C#的控制台应用程序,写代码: using System;
using System.Collections.Generic;
using System.Text;namespace test4
{
class Program
{
static void Main(string[] args)
{
double temp, a1 = 0, a2 = 0, a3 = 0;
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("请输入第{0}个数:",i);
temp = Convert.ToDouble(Console.ReadLine());
if (temp > 0)
a1++;
else if (temp==0)
a2++;
else
a3++;
}
Console.WriteLine("正数的个数是:{0}",a1);
Console.WriteLine("0的个数是:{0}",a2);
Console.WriteLine("负数的个数是:{0}",a3);
Console.Read();
}
}
}
图:
using System.Collections.Generic;
using System.Text;namespace test4
{
class Program
{
static void Main(string[] args)
{
double temp, a1 = 0, a2 = 0, a3 = 0;
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("请输入第{0}个数:",i);
temp = Convert.ToDouble(Console.ReadLine());
if (temp > 0)
a1++;
else if (temp==0)
a2++;
else
a3++;
}
Console.WriteLine("正数的个数是:{0}",a1);
Console.WriteLine("0的个数是:{0}",a2);
Console.WriteLine("负数的个数是:{0}",a3);
Console.Read();
}
}
}
图:
展开全部
private void button1_Click(object sender, EventArgs e)
{
int a = 0;
int b = 0;
int c = 0;
int[] num=new int[5];
num[0] = Convert.ToInt32(this.textBox1.Text);
num[1] = Convert.ToInt32(this.textBox2.Text);
num[2] = Convert.ToInt32(this.textBox3.Text);
num[3] = Convert.ToInt32(this.textBox4.Text);
num[4] = Convert.ToInt32(this.textBox5.Text);
foreach (int m in num)
{
if (m > 0)
{
a++;
}
else if (m == 0)
{
b++;
}
else
{
c++;
}
}
this.label1.Text="正数有"+a+"个,负数有"+c+"个,0有"+b+"个";
}
{
int a = 0;
int b = 0;
int c = 0;
int[] num=new int[5];
num[0] = Convert.ToInt32(this.textBox1.Text);
num[1] = Convert.ToInt32(this.textBox2.Text);
num[2] = Convert.ToInt32(this.textBox3.Text);
num[3] = Convert.ToInt32(this.textBox4.Text);
num[4] = Convert.ToInt32(this.textBox5.Text);
foreach (int m in num)
{
if (m > 0)
{
a++;
}
else if (m == 0)
{
b++;
}
else
{
c++;
}
}
this.label1.Text="正数有"+a+"个,负数有"+c+"个,0有"+b+"个";
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询