C# checkBalance(ref bool)不是所有代码路径都返回值吗?
我刚刚开始学习C,所以我是个新手。。。我正在尝试写检查余额的方法,但不断得到我在标题中说的错误。。。我知道代码还没有完成,但我只是想暂时能够返回AccountTypes菜单 也有人能解释这条线的静态无效startref bool维数,我真的不知道这是怎么回事。谢谢C# checkBalance(ref bool)不是所有代码路径都返回值吗?,c#,boolean,C#,Boolean,我刚刚开始学习C,所以我是个新手。。。我正在尝试写检查余额的方法,但不断得到我在标题中说的错误。。。我知道代码还没有完成,但我只是想暂时能够返回AccountTypes菜单 也有人能解释这条线的静态无效startref bool维数,我真的不知道这是怎么回事。谢谢 class Program { static void start(ref bool dimensionsUpdated){ int userOption = 0; //Repeats the
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: ");
你可以把返回0;在checkBalance结束时,暂时禁用该错误
如果您将函数声明为返回一个double,但实际上没有返回任何内容,那么c会认为您有错误。这是因为您的方法返回一个double值,而您没有从方法返回任何内容
public static double checkBalance(ref bool dimensionsUpdated)
由于问题中没有完整的代码,您可以发布完整的代码
可能是在if语句中返回了一个双精度值,如果是这样,则需要确保任何方法路径都应返回双精度值
或者,如果这是checkBalance方法的完整代码,并且您没有从中返回任何内容,那么您可以将方法签名更改为返回void
checkBalance函数在任何情况下都应返回值
public static double checkBalance(ref bool dimensionsUpdated){
if(...){
return 1;
}
else if(...){
return 1;
}
return 0;
}
我想你应该读一些有关这方面的书 回答您的问题: 您的方法checkBalance似乎没有返回值,尽管它的签名声明它将返回一个double类型的数字。但是我们不能确定,因为方法的结尾在您发布的代码中被截断了。 静态void startref bool dimensionsUpdated是名为start of return type void的方法的另一种方法声明,即它不返回任何内容。它具有一个名为dimensionsUpdated的布尔类型
你好像一次问两个问题。你能把这些分成两个独立的问题吗,请?我主要想知道为什么我得到错误checkBalanceref bool不是所有的代码路径都返回一个值?请至少发布足够的代码,这样我们就可以看到checkBalance方法是如何结束的。这就是我到目前为止的所有代码…对于checkBalance方法,我希望它通过选择不同的帐户显示来显示不同的帐户类型该帐户中的$value设置了值。。。要做到这一点,我会使用double还是void?@user1645218,如果您只显示结果,而没有在checkBalance方法中返回任何内容,那么使用void type。
public static double checkBalance(ref bool dimensionsUpdated){
if(...){
return 1;
}
else if(...){
return 1;
}
return 0;
}