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;
}
}
{
int quotientTotal; if (b == 0)
{
Console.WriteLine("Error! The divisor can't be zero!");
return -1;
}
else
{
quotientTotal = a / b;
return quotientTotal;
}
}
解决方案 »
- swfobject.js
- 怎么修改软件描述和公司
- 有兄弟用过vpn吗?
- WPF MVVM模式下,viewmodel与view交互怎么办?
- DataGridView控件的一个问题 在线等 着急
- 为什么用Visual C# 2008 Express Edition编译完的程序自己机器上可以运行,别人的机器上运行没反应呢?
- 基本功問題﹕不用異常處理﹐如何判斷類A是類B的繼承類﹖
- C# 新手问题... 轻喷 跪求解答
- e.Item.ItemIndex是什么?
- 请教,如何做像C#设计器中“属性”那样的控件?
- 关于winsock的GetData问题
- 救命:::用.net调用java的webservice 报错 :WSDoAllReceiver: Request does not contain required Security header
public static int Quotient(int a, int b)
{
int quotientTotal; if (b == 0)
{
return -1;
} quotientTotal = a / b;
return quotientTotal;
}
谢过各位老大先~~~~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;
}
}
}
throw new DivideByZeroException();
搂住以前是做VB的?
throw new DivideByZeroException();
就这个答案
throw new DivideByZeroException();是对的
搂住以前是做VB的?
-------------------------------------------------------
我都是用return的,请问有什么区别吗?
if (b == 0)
throw new DivideByZeroException();也可
{
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();
}
throw new DivideByZeroException();不用判断,直接quotientTotal = a / b;
return quotientTotal; 即可。 都是抛出异常,多次一举干吗。
public static int? Quotient(int a, int b)
{
return b=0? null:a/b;
}
还有个问题public static int Quotient(int a, int b),不能用int,相除后会出现小数,应该使用double
break 跳出循环
continue,跳出当前循环,继续下次循环