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;
}
      }

解决方案 »

  1.   

    这段程序这样写可能更好
    public static int Quotient(int a, int b)
    {
    int quotientTotal; if (b == 0)
    {
    return -1;
    } quotientTotal = a / b;
    return quotientTotal;
          }
      

  2.   

    这次把话说完整.
    谢过各位老大先~~~~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;
    }
    }
    }
      

  3.   

    if (b == 0)
      throw new DivideByZeroException();
      

  4.   

    用return语句
    搂住以前是做VB的?
      

  5.   

    if (b == 0)
      throw new DivideByZeroException();
    就这个答案
      

  6.   

    if (b == 0)
      throw new DivideByZeroException();是对的
      

  7.   

    liberte: 用return语句
    搂住以前是做VB的?
    -------------------------------------------------------
    我都是用return的,请问有什么区别吗?
      
     
      

  8.   

    一般用return跳出整个函数,
    if (b == 0)
      throw new DivideByZeroException();也可
      

  9.   

    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();
                }
      

  10.   

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

  11.   

    如果不抛异常可以用nullable类型
    public   static   int?   Quotient(int   a,   int   b) 

        return b=0? null:a/b;
      

  12.   

    直接return  如果你想跳出一层 就用break
      

  13.   

    楼主,楼上说的都很好。
    还有个问题public   static   int   Quotient(int   a,   int   b),不能用int,相除后会出现小数,应该使用double
      

  14.   

    这个有问题,null怎么可以当做int返回??
      

  15.   

    怎么将break用在了if语句,break是用于跳出循环的。
      

  16.   


    break 跳出循环
    continue,跳出当前循环,继续下次循环