Ai'm在這裏新的和新的編程。我很抱歉,如果這個問題真的是幼稚的,但我在一個簡單的C#程序中使用返回類型有一些麻煩。 這是我的代碼文件,在AccountTest類的d == 2中,我希望提取過程重新開始,但這次不要求用戶輸入帳戶餘額。一個朋友的建議使用while循環,但我不知道如何使用while循環在這裏。提前致謝。 :)關於如何在C中使用返回值的困惑#
using System;
public class Account
{
public static void Main(string[] args)
{
Console.Write("Enter your account balance: ");
int AccountBalance = Convert.ToInt32(Console.ReadLine());
Account.Debit(AccountBalance);
}
public static void Debit(int AccountBalance)
{
Console.Write("\n\nEnter the amount you want to withdraw in Rs: ");
int WithdrawalAmount = Convert.ToInt32(Console.ReadLine());
AccountTest.DebitTest(AccountBalance, WithdrawalAmount);
}
}
我的賬戶類
public class AccountTest
{
public static int DebitTest(int AccountBalance, int WithdrawalAmount)
{
if (WithdrawalAmount > AccountBalance)
{
Console.WriteLine("\n\nDebit amount exceeded account balance.");
Console.ReadLine();
return 0;
}
else if (WithdrawalAmount <= 0)
{
Console.WriteLine("\n\nError. Incorrect amount.");
Console.ReadLine();
return 0;
}
else
{
int newBalance = AccountBalance - WithdrawalAmount;
Console.WriteLine("\n\nWithdrawal was successful. Thankyou for using our services.\n\nPress 1 to exit, 2 to withdraw again, 3 to check your account balance.\n");
int InputNumber = Convert.ToInt32(Console.ReadLine());
if (InputNumber == 1)
{
Console.ReadLine();
return 0;
}
else if (InputNumber == 2)
{
return WithdrawalAmount;
}
else if (InputNumber == 3)
{
Console.WriteLine("\n\nYour remaining account balance is: {0}", newBalance);
Console.ReadLine();
return 0;
}
}
return 0;
}
}
谷歌是豐富的資源:http://msdn.microsoft.com/en-us/library/2aeyhxcd.aspx(**,而(C#參考)**) ,搜索結果爲:https://www.google.ca/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#safe=off&q=while%20loop%20c%23 – 2014-11-04 15:44:10
您並不需要循環,您可以調用借記卡(c),因爲c是新餘額。 – Philippe 2014-11-04 15:46:09
有意義的變量名將幫助所有試圖閱讀代碼的人 – Jonesopolis 2014-11-04 15:49:52