C#中如何跳出if判断语句?
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;
}
} --------------------编程问答-------------------- public static int Quotient(int a, int b)
{
int quotientTotal;
if (b == 0)
{
Console.WriteLine("Error! The divisor can't be zero!");
return -1;
}
else
{
quotientTotal = a / b;
return quotientTotal;
}
}
--------------------编程问答-------------------- 这段程序这样写可能更好
public static int Quotient(int a, int b)
{
int quotientTotal;
if (b == 0)
{
return -1;
}
quotientTotal = a / b;
return quotientTotal;
}
--------------------编程问答-------------------- 楼主基础没学好..... --------------------编程问答-------------------- UP:
暴汗.....
偶也是很不好意思地提问啊....... --------------------编程问答-------------------- 顶一楼,二楼,楼主加油
没有什么不好意思的,大家都是这么过来的! --------------------编程问答-------------------- 这次把话说完整.
谢过各位老大先~~~~
using System;
// this class calculate the quotient of two numbers
public class SimpleCalculator
{
public static void Main()
{
int x;
int y;
Console.Write("Enter first number: ");
x = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The quotient is: " + Quotient(x, y)); //①
}
// Quotient calculates the divisions of three int's
public static int Quotient(int a, int b)
{
int quotientTotal;
if (b == 0) //如果除数为0,则打印下列提示语句.并不执行①语句
{
Console.WriteLine("Error! The divisor can't be zero!");
return -1; //求教如何返回主函数?
//用return语句返回,结果仍会打印①语句,我不想让它执行①,即在上句就结束程序,请问该怎么做?
}
else
{
quotientTotal = a / b;
return quotientTotal;
}
}
} --------------------编程问答-------------------- if (b == 0)
throw new DivideByZeroException(); --------------------编程问答-------------------- 用return --------------------编程问答-------------------- 用return语句
搂住以前是做VB的? --------------------编程问答-------------------- if (b == 0)
throw new DivideByZeroException();
就这个答案
--------------------编程问答-------------------- 学习中 --------------------编程问答-------------------- if (b == 0)
throw new DivideByZeroException();
是对的 --------------------编程问答-------------------- liberte: 用return语句
搂住以前是做VB的?
-------------------------------------------------------
我都是用return的,请问有什么区别吗?
--------------------编程问答-------------------- 一般用return跳出整个函数,
if (b == 0)
throw new DivideByZeroException();也可
--------------------编程问答-------------------- 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();
} --------------------编程问答-------------------- ^_^顶,没啥,勇于问问题总比仍旧不知道的混下去强~ --------------------编程问答-------------------- 用continue --------------------编程问答-------------------- if (b == 0)
throw new DivideByZeroException();
不用判断,直接quotientTotal = a / b;
return quotientTotal; 即可。 都是抛出异常,多次一举干吗。
--------------------编程问答-------------------- 如果不抛异常可以用nullable类型
public static int? Quotient(int a, int b)
{
return b=0? null:a/b;
}
--------------------编程问答-------------------- 直接return 如果你想跳出一层 就用break --------------------编程问答-------------------- 楼主,楼上说的都很好。
还有个问题public static int Quotient(int a, int b),不能用int,相除后会出现小数,应该使用double --------------------编程问答-------------------- 我也觉得没必要判断,19楼的方法就够用了,想返回什么都可以自己约定。 --------------------编程问答--------------------
这个有问题,null怎么可以当做int返回?? --------------------编程问答-------------------- if还用问? --------------------编程问答-------------------- break --------------------编程问答-------------------- 怎么将break用在了if语句,break是用于跳出循环的。 --------------------编程问答--------------------
break 跳出循环
continue,跳出当前循环,继续下次循环
补充:.NET技术 , C#