c#每日一题(五),高手莫入!

VBDN 2005-04-05 10:30:15
编写一控制台应用程序,要求完成下列功能:
1.接收一个整数n。
2.如果n为正数,输出1-n间的全部整数。
3.如果n为负数,用break或者return退出程序。
4.转到1继续接收下一个整数。
...全文
632 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
wmm_net 2005-04-29
  • 打赏
  • 举报
回复
studing
wangasp 2005-04-08
  • 打赏
  • 举报
回复
学习
Supernpc 2005-04-08
  • 打赏
  • 举报
回复
楼上的兄弟很赞同我吗?..把我的回贴复制一份..
bthl 2005-04-08
  • 打赏
  • 举报
回复
using System;

class Test
{
static void Main()
{
string strInput;
int n = 0;

while (true)
{
Console.Write("Please Input a Number:");
strInput = Console.ReadLine();

try
{
n = Int32.Parse(strInput);
}
catch (Exception e)
{
Console.WriteLine("你输入的不是一个数值!");
continue;

}

if (n > 0 )
{
for (int i = 1 ; i <= n ; i++)
{
Console.WriteLine(i);
}
}
else
break;
}
}
}
VBDN 2005-04-08
  • 打赏
  • 举报
回复
to czhenq(6月份换新工作,谁给介绍个)
====================================
没看清题么?输入为负则退出,不是输入“X”。
另外你的输出结果不对,比如输入6,输出“1,2,3,4,5,”,少了“6”。
VBDN 2005-04-08
  • 打赏
  • 举报
回复
to goodstuday(路漫漫),你的代码不正确,修改如下:
===============================================
using System;
class test5
{
public static void Main()
{
while(true)
{
Console.Write("请输入一个整数:");
int n=int.Parse(Console.ReadLine());
if (n<0) break;
for (int i=1;i<=n;i++) Console.WriteLine(i);
}
}
}
VBDN 2005-04-08
  • 打赏
  • 举报
回复
参考答案:
using System;
class Test5
{
static void Main()
{
while(true)
{
Console.Write("请输入一个整数(负值结束):");
string str=Console.ReadLine();
try
{
int i=Int32.Parse(str);
if (i<0) break;
for (int j=1;j<=i;j++) Console.WriteLine(j);
}
catch
{
Console.WriteLine("你输入的不是数字或者超出整数的表示范围,请重新输入。");
}
}
}
}
conan19771130 2005-04-06
  • 打赏
  • 举报
回复
楼上正解
Supernpc 2005-04-06
  • 打赏
  • 举报
回复
放标准答案
using System;

class Test
{
static void Main()
{
string strInput;
int n = 0;

while (true)
{
Console.Write("Please Input a Number:");
strInput = Console.ReadLine();

try
{
n = Int32.Parse(strInput);
}
catch (Exception e)
{
Console.WriteLine("你输入的不是一个数值!");
continue;

}

if (n > 0 )
{
for (int i = 1 ; i <= n ; i++)
{
Console.WriteLine(i);
}
}
else
break;
}
}
}
sunkangta 2005-04-06
  • 打赏
  • 举报
回复
public static void Main()
{
while(true)
{
int n = int.Parse(Console.ReadLine());
Doit(n);
}
}

public void Doit(int number)
{
if (number > 0)
{
for(int i=1 ; i<= number ;i++)
{
Console.Write( i+ " ");
}
Console.Write("\n");
}
else
{return;}
}
老汉 2005-04-06
  • 打赏
  • 举报
回复
这么多的回复,加了TRY的只有一贴。
一小时 2005-04-06
  • 打赏
  • 举报
回复
来晚了,只有接分了
yanyzheng 2005-04-06
  • 打赏
  • 举报
回复
学习C#中
czhenq 2005-04-06
  • 打赏
  • 举报
回复
用异常来判断输入的是否为数字将降低程序的执行效率

还不如自己写个来的好:
private bool isNumber(string val)
{
char[] vals = val.ToCharArray();
foreach(char c ini vals)
if(!c.IsNumberic())
return false;
return true;
}
aprex 2005-04-06
  • 打赏
  • 举报
回复
学习ing
_-_-_-_- 2005-04-05
  • 打赏
  • 举报
回复
学习ing
yvesSY 2005-04-05
  • 打赏
  • 举报
回复
class Class1
{

static void Main(string[] args)
{
string sinput;
Int16 num = 0;
Console.WriteLine("please input a number or press \"X\" to exit the program.");
while(true)
{
sinput = Console.ReadLine();
if(sinput == "X" | sinput == "x")
break;

try
{
num = Int16.Parse(sinput);
}
catch(Exception e)
{
Console.WriteLine("非法输入,请重试。");
continue;
}
if (num > 0)
{
for(int i = 1;i < num;i++ )
Console.Write(i+" ");
Console.WriteLine("\n");
}
}
}
//学习ing
rainlake 2005-04-05
  • 打赏
  • 举报
回复
再一个我发现所有人都没有异常处理,嘿嘿
rainlake 2005-04-05
  • 打赏
  • 举报
回复
楼主出题不考虑如何退出程序。
lijipan 2005-04-05
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
int iMax=Convert.ToInt32(Console.ReadLine());
if(iMax>0)
{
for(int i=1;i<=iMax;i++)
{
Console.Write(i.ToString()+"\n");
}
Main(null);
}
}
加载更多回复(11)

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧