C#中如何跳出if判断语句?

zjbstu 2007-05-30 10:35:35
if(除数为零)
{
不执行后续操作,退回主程序
}
else
{
计算并返回两数之商
}


// Quotient calculates the divisions of three int's
public static int Quotient(int a, int b)
{
int quotientTotal;

if (b == 0)
{
Console.WriteLine("Error! The divisor can't be zero!");
break; (?C#语言如何终止Quotient函数,返回调用它的Main函数中)
}
else
{
quotientTotal = a / b;
return quotientTotal;
}
}
...全文
7832 27 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjw20040401 2011-10-29
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 axu234 的回复:]
怎么将break用在了if语句,break是用于跳出循环的。
[/Quote]

break 跳出循环
continue,跳出当前循环,继续下次循环
隽永Jasen 2011-10-29
  • 打赏
  • 举报
回复
怎么将break用在了if语句,break是用于跳出循环的。
mrpmc 2011-10-29
  • 打赏
  • 举报
回复
break
ningweidong 2011-10-27
  • 打赏
  • 举报
回复
if还用问?
suhangkeji 2011-10-27
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 mabaolin 的回复:]
如果不抛异常可以用nullable类型
public static int? Quotient(int a, int b)
{
return b=0? null:a/b;
}
[/Quote]
这个有问题,null怎么可以当做int返回??
winter_2000 2011-10-27
  • 打赏
  • 举报
回复
我也觉得没必要判断,19楼的方法就够用了,想返回什么都可以自己约定。
suhangkeji 2011-10-27
  • 打赏
  • 举报
回复
楼主,楼上说的都很好。
还有个问题public static int Quotient(int a, int b),不能用int,相除后会出现小数,应该使用double
小火龙果 2011-10-27
  • 打赏
  • 举报
回复
直接return 如果你想跳出一层 就用break
mabaolin 2011-10-26
  • 打赏
  • 举报
回复
如果不抛异常可以用nullable类型
public static int? Quotient(int a, int b)
{
return b=0? null:a/b;
}

mabaolin 2011-10-26
  • 打赏
  • 举报
回复
if (b == 0)
throw new DivideByZeroException();

不用判断,直接quotientTotal = a / b;
return quotientTotal; 即可。 都是抛出异常,多次一举干吗。

xuhaijin 2011-10-26
  • 打赏
  • 举报
回复
用continue
liberte 2007-05-31
  • 打赏
  • 举报
回复
用return语句
搂住以前是做VB的?
Arieslns 2007-05-31
  • 打赏
  • 举报
回复
^_^顶,没啥,勇于问问题总比仍旧不知道的混下去强~
J_S_S 2007-05-31
  • 打赏
  • 举报
回复
public static int Quotient(int a, int b)
{
int quotientTotal;

if (b == 0)
{
throw new Exception("Error! The divisor can't be zero!");
}

quotientTotal = a / b;
return quotientTotal;
}
==========================================================
try
{
Console.WriteLine("The quotient is: " + Quotient(x, y));//①
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadLine();
}
hsgzs 2007-05-31
  • 打赏
  • 举报
回复
一般用return跳出整个函数,
if (b == 0)
throw new DivideByZeroException();也可

fwt1314111 2007-05-31
  • 打赏
  • 举报
回复
liberte: 用return语句
搂住以前是做VB的?
-------------------------------------------------------
我都是用return的,请问有什么区别吗?


pfa2008 2007-05-31
  • 打赏
  • 举报
回复
if (b == 0)
throw new DivideByZeroException();

是对的
Sunniness 2007-05-31
  • 打赏
  • 举报
回复
学习中
ameily_99 2007-05-31
  • 打赏
  • 举报
回复
if (b == 0)
throw new DivideByZeroException();


就这个答案
tantj 2007-05-30
  • 打赏
  • 举报
回复
用return
加载更多回复(7)

111,092

社区成员

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

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

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