level 4
要求输入两个数字第二个数字要大于第一个数字 如果第二个数字低于第一个要显示出错提示 然后这两个数之间的所有数字相加并的出来和 例如
请输入第一个数字 5
请输入第二个数字11
5+6+7+8+9+10+11=56
选取的数字范围在 -20到+20 之间
这个真的无语了··· 只有一小部分有头绪 其他的一点招都没了··· 很乱 求大神帮忙···
2013年08月05日 21点08分
1
level 12
先判断你的n和m,在不在范围内,在的话
int sum=0;
for(i=n;i<=m;i++)
{
sum+=i
}
2013年08月06日 01点08分
8
出错的话,返回重新输入怎么写啊?
2013年08月06日 02点08分
回复 a剑风无情 :可以用循环啊。
2013年08月07日 00点08分
回复 自由战士1127 :求大神指点
2013年08月07日 03点08分
回复 a剑风无情 :for(int i=0;i<=100;i++) { //这里不就是循环的内容么,你把你的要写得东西写在这里面,运行完了一次不就又回到开始继续运行么。 }
2013年08月07日 05点08分
level 6
int diyige, dierge, he = 0;;
string zhuangtai="";
do
{
do
{
Console.Clear();
Console.WriteLine("输入第一个数");
diyige = ShuRuShu();
Console.WriteLine("输入第二个数字");
dierge = ShuRuShu();
if (diyige < dierge)
{
break;
}
else
{
Console.WriteLine("第二个数必须小于第一个数,任意键继续");
Console.ReadKey();
continue;
} } while (true);
for (int i = diyige; i <= dierge; i++)
{
he += i;
}
Console.WriteLine("第一个数到第二个数区间的数相加是{0}",he);
Console.WriteLine("任意键继续/n退出程序");
zhuangtai =Console .ReadLine ();
} while (zhuangtai !="n" );
}
public static int ShuRuShu()
{
int shu;
do
{
try
{
shu = Convert.ToInt32(Console.ReadLine());
if (shu > 20 || shu < -20)
{
Console.WriteLine("超出范围");
continue;
}
return shu;
}
catch
{
Console.WriteLine("输入的不是数字!重新输入");
}
} while (true);
2013年08月06日 02点08分
9
多谢 ····
2013年08月06日 03点08分
回复 n35190630 :那个变量he应该挪在for循环的上面。。。。
2013年08月06日 04点08分
level 5
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数字");
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第二个数字");
int y = Convert.ToInt32(Console.ReadLine());
int sum = 0;
if (x > y)
{
Console.WriteLine("必须第二个数字比第一个数字大");
}
else
{
for (int i = x; i <= y; i++)
{
sum += i;
}
}
Console.WriteLine("结果为:");
Console.WriteLine(sum);
Console.ReadKey();
}
2013年08月06日 05点08分
12
这个是不是你想要的结果啊
2013年08月06日 05点08分
回复 happyxiaoxiami :大致上是这样的 但是还差一点 还有个要求是 输入的数值是要再-20到20之间的数
2013年08月06日 10点08分
level 6
int diyige, dierge;
string zhuangtai;
do
{ do
{
Console.Clear();
Console.WriteLine("输入第一个数");
diyige = ShuRuShu();
Console.WriteLine("输入第二个数字");
dierge = ShuRuShu();
} while (PanDuan (diyige ,dierge )!=1);
int he = JiShuan(diyige, dierge);
Console.WriteLine("第一到第二个数区间数的和是{0}", he);
Console.WriteLine("任意键继续/“n”键结束");
zhuangtai = Console.ReadLine();
} while (zhuangtai !="n");
}
//计算数字的和;
public static int JiShuan(int diyige1,int dierge2)
{
int he = 0;
for (int i = diyige1; i <= dierge2; i++)
{
he += i;
}
return he;
}
//判断是不是第一个小于第二个数(是返回1不是返回2);
public static int PanDuan(int diyige1,int dierge2)
{
if (diyige1 < dierge2)
{ return 1;}
else
{
Console.WriteLine("第二个数必须小于第一个数,任意键重新输入");
Console.ReadKey();
return 2;
}
}
//输入一个-20~20的有效数字;
public static int ShuRuShu()
{
int shu;
do
{
try
{
shu = Convert.ToInt32(Console.ReadLine());
if (shu > 20 || shu < -20)
{
Console.WriteLine("超出范围");
continue;
}
return shu;
}
catch
{
Console.WriteLine("输入的不是数字!重新输入");
} } while (true);
2013年08月06日 05点08分
13
//判断是不是第一个小于第二个数(是返回1不是返回2);方法中间的那句话写反了
2013年08月06日 05点08分
你这命名看的我头疼..public static int PanDuan(int diyige1,int dierge2)特别是这....你给定个num1,num2也比你这命名好看点呀.
2013年08月06日 06点08分
回复 awfitwhtuter :(⊙o⊙)…不认识英文名称,汉语拼音的舌前舌后音也分不清楚。学的很吃力,写的也很乱。
2013年08月06日 07点08分
回复 心如割刀 :好吧..那实在不行你其实可以考虑中文变量名.好处当然很明显..不过坏处是有可能生成报错..而且是非常诡异的错....
2013年08月06日 09点08分
level 1
static void Main(string[] args)
{
one:
Console.WriteLine("请输入第一个数");
int a = Convert.ToInt32(Console.ReadLine());
if (ifcan(a))
{
two:
Console.WriteLine("请输入第二个数");
int b = Convert.ToInt32(Console.ReadLine());
if (ifcan(b) && ba(b, a))
{
Console.WriteLine("{0}", sum(a, b));
Console.ReadKey();
}
else
{
Console.WriteLine("输入的第二个数字必须在-20~20之间,且必须比{0}大",a);
goto two;
}
}
else
{
Console.WriteLine("输入的数字必须在-20~20之间");
goto one;
}
}
static string sum(int a, int b)
{
string strsum = string.Empty;
int intsum = 0;
for (int c = a; c <= b; c++)
{
strsum += c.ToString() + "+";
intsum += c;
}
return strsum.Substring(0, strsum.Length - 1) + "=" + intsum.ToString();
}
static bool ifcan(int a)
{
bool B =false;
if((a>=-20) && (a<=20))
{
B = true;
}
return B;
}
static bool ba(int b,int a)
{
bool B = false;
if (b > a) B = true;
return B;
}
2013年08月06日 13点08分
15
level 5
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数字");
int x = Convert.ToInt32(Console.ReadLine());
if (x > 20 || x < -20)
{ Console.WriteLine("您输入的第一个数字不在范围内");
}
else
{
Console.WriteLine("请输入第二个数字");
int y = Convert.ToInt32(Console.ReadLine());
if (y > 20 || y < -20)
{
Console.WriteLine("您输入的第二个数字不在范围内");
}
else
{
int sum = 0;
if (x >= y)
{
Console.WriteLine("必须第二个数字比第一个数字大");
}
else
{
for (int i = x; i <= y; i++)
{
sum += i;
}
}
Console.WriteLine("结果为:");
Console.WriteLine(sum);
}
}
Console.ReadKey();
}
2013年08月06日 14点08分
16