我目前正在完成一項家庭作業任務,並遇到了一個問題,我無法在我的書籍或在線中找出答案。我不想找人來修復我的代碼,我只需要指出正確的方向。在try塊中創建對象
我目前正試圖在try塊內創建一個對象。在此之前嘗試阻止我要求用戶輸入4個數字。這4個數字是我嘗試在try塊內創建的對象的參數。我不確定如何將這些數據從用戶傳遞到try塊。
我的問題是,我該如何執行一個try塊內的對象的創建?我知道我的當前代碼只要它碰到try塊就將所有東西都重置爲0。
static void Main(string[] args)
{
string choice;
//Input once choice is made----------------------------------------------
do
{
Console.WriteLine("**********************************************");
Console.WriteLine("Create Checking Account \"C\"");
Console.WriteLine("Create Checking Account \"S\"");
Console.WriteLine("Quit the Application \"Q\"");
Console.WriteLine("**********************************************");
Console.Write("Enter choice: ");
choice = Convert.ToString(Console.ReadLine());
if (choice != "Q")
{
switch (choice)
{
case "C":
Console.Write("Enter a name for the Account: ");
CA.setAccountName(Convert.ToString(Console.ReadLine()));
Console.Write("Enter an account Number: ");
CA.setAccountNumber(Convert.ToInt32(Console.ReadLine()));
Console.Write("Enter an initial balance: ");
CA.setBalance(Convert.ToDecimal(Console.ReadLine()));
Console.Write("Enter the fee to be charged per transcation: ");
CA.setFeeCharged(Convert.ToDecimal(Console.ReadLine()));
try
{
CheckingAccount CA = new CheckingAccount("",0,0,0);
CA.PrintAccount();
}
catch (Negative ex)
{
Console.WriteLine("**********************************************");
Console.WriteLine(ex.Message);
}
break;
我不清楚你的問題實際上是什麼。看起來你已經在try塊中聲明瞭一個對象。你遇到的問題是什麼? – pquest
我需要在try塊之前通過將它設置爲null來創建對象。然後,我從來沒有實際存儲過用戶輸入。 最後,一旦使用寫入的代碼進入try塊,我會自動將所有內容重置爲零而不是使用用戶輸入數據。 – bravebutters