checkBalance(ref bool)并非所有代码路径都返回一个值?

时间:2012-09-04 06:13:51

标签: c# boolean

我刚刚开始学习C#所以我很新...我正在尝试为Check平衡编写方法,但一直得到错误我在标题中说...我知道代码没有完成但只是想能够暂时返回账户类型菜单吗?

也有人能够解释这一行" static void start(ref bool dimensionsUpdated)"我真的不知道这是做什么的。三江源!!

class Program
{
   static void start(ref bool dimensionsUpdated){
        int userOption = 0;

        //Repeats the loop until the user wishes to exit
        do
        {
        Console.WriteLine("Welcome to EziTeller ATM Machine\n\n");

        Console.WriteLine("Transaction Menu"
                        + "\n================\n"
                        + "\n1) Check Balance"
                        + "\n2) Withdraw"
                        + "\n3) Transfer");
        Console.WriteLine("\n\nPlease Enter Your Option: 1, 2, 3 or 0 to exit: ");

        //Read in the users choice
        userOption = int.Parse(Console.ReadLine());

            Console.ReadKey(); 

            //Run a series of checks to see what the user chose.
            //Open the desired method, otherwise show error message
            //Asking the user to input a VALID option.
            if (userOption == 0)
            {
                Console.WriteLine("Thank you for using EziTeller!");
                Environment.Exit(0);
            }
            else if (userOption == 1){
                checkBalance(ref dimensionsUpdated);
            }
            else if (userOption == 2){
                withdrawMoney(ref dimensionsUpdated);
            }
            else if (userOption == 3){
                transferMoney(ref dimensionsUpdated);
            }
            else Console.WriteLine("\n\nPlease enter a valid option, either 1, 2, 3, or 0 to exit\n\n");
        }   while (userOption != 0);
   }
    public static double checkBalance(ref bool dimensionsUpdated){
        Console.WriteLine("Account Types"
                        + "\n============\n"
                        + "\n1) Savings Account"
                        + "\n2) Debit Card"
                        + "\n3) Credit Card"
                        + "\n4) Line of Credit");
        Console.WriteLine("\n\nPlease Enter Your Option: 1...4 or 0 to exit: ");

4 个答案:

答案 0 :(得分:0)

您可以将return 0;放在checkBalance(暂时)的末尾以禁用错误。

如果你将函数声明为返回double但你实际上没有返回任何东西,那么c#认为你有错误。

答案 1 :(得分:0)

这是因为您的方法返回double值并且您没有从方法返回任何内容。

public static double checkBalance(ref bool dimensionsUpdated)

由于问题中不存在完整代码,您可以发布完整代码。

您可能在double内返回if statement值,如果是这样,您需要确保您的任何方法路径都应返回double值。

或者,如果这是您的checkBalance方法的完整代码,并且您没有从中返回任何内容,那么您可以更改方法签名以返回void

public static void checkBalance(ref bool dimensionsUpdated)

答案 2 :(得分:0)

checkBalance函数在任何情况下都应返回值。

public static double checkBalance(ref bool dimensionsUpdated){
    if(...){
        return 1;
    }
    else if(...){
        return 1;
    }
    return 0;
}

答案 3 :(得分:0)

我认为你应该阅读the basics of object orientation

回答你的问题:

  • 您的方法checkBalance似乎没有返回值,尽管它的签名状态,它会返回多个类型“double”。但我们无法确切知道,因为方法的结尾会在您发布的代码中被截断。
  • static void start(ref bool dimensionsUpdated)是返回类型为“void”的名为“start”的方法的另一个方法声明(即它不返回任何内容)。它有一个reference parameter类型为“boolean”,名为“dimensionsUpdated”