2014-11-04 33 views
1

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

谷歌是豐富的資源: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

+0

您並不需要循環,您可以調用借記卡(c),因爲c是新餘額。 – Philippe 2014-11-04 15:46:09

+3

有意義的變量名將幫助所有試圖閱讀代碼的人 – Jonesopolis 2014-11-04 15:49:52

回答

3

真正的代碼需要重構。治療帳戶就像一個thing更有意義,因爲它應該是它自己的對象,你應該告訴它做的事:

public class Account 
{ 
    public int Balance { get; set; } 

    public Account(int startBalance) 
    { 
     Balance = startBalance; 
    } 

    public void Debit(int amount) { Balance -= amount; } 
    public void Credit(int amount) { Balance += amount; } 
} 

現在,你可以問用戶他們想用自己的賬戶做什麼,並且您有足夠的空間添加多個帳戶。因此,該程序可能看起來像:

int startingAmount = int.Parse(Console.ReadLine()); 
var account = new Account(startingAmount); 

Console.WriteLine("1 to credit, 2 to debit, 0 to exit"); 
var input = int.Parse(Console.ReadLine()); 
while (input != 0) 
{ 
    //manipulate account   
} 

我開始閱讀約佔靜態VS實例對象

0

環路可在借記方法實現:

public static void Debit(int AccountBalance) 
{ 
    int result = 0; 
    do 
    { 
     Console.Write("\n\nEnter the amount you want to withdraw in Rs: "); 
     var WithdrawalAmount = Convert.ToInt32(Console.ReadLine()); 

     result = AccountTest.DebitTest(AccountBalance, WithdrawalAmount); 

    } while (result != 0); 
} 
0

您應該while循環讀了。基本上你想要的是返回一個數字的方法,這個數字決定了程序下一步應該做什麼,或者什麼時候該結束。把循環看作你的引擎,聲明「只要[選擇的鍵]沒有按下,就繼續做一些事情」。

一個小例子,有[選擇鍵]爲1,將是:

int choice = 0; 
int balance = 100; 
while (choice != 1) { 
    Console.WriteLine("Enter withdraw amount"); 
    string userInput = Console.ReadLine(); 

    // This will crash the program if the user enters text. 
    // Used for demonstration only. int.TryParse is preferred. 
    int value = int.Parse(userInput); 

    choice = AccountTest.DebitTest(balance, value); 
} 

class AccountTest { 
    public static int DebitTest(int AccountBalance, int WithdrawalAmount) 
    { 
     // do the test if the debit is OK 
     //.......... 

     // At the end, you can do this. This till return the value entered and 
     // choice will be given a new value. If it was 1, the program will end. 
     // NOTE: It's not safe to use convert as     
     // it will crash the program if the user enters text. 
     return Convert.ToInt32(Console.ReadLine()); 
    } 

} 

注意,這不是一個官能ATM-程序,作爲餘量永遠不會被更新。我離開那個爲你解決,因爲我猜這是一個在編程=)

1

首先,歡迎來到編碼社區沒有幼稚的問題隨時問,當你需要,有一些誰將回答你「谷歌有答案」,但不擔心其他許多人會幫助你,我看到你已經選擇了一個答案,但不適合爲你和其他新程序員增加了我的觀點。

a。如果你的新編碼永遠不會從代碼開始,它只會使事情變得複雜,而是從一個流程圖開始,這個流程圖說明你在程序和每個組件(類,函數等)中獲得的結果,它更容易要將其轉換爲代碼,您可以嘗試使用this site 它看起來非常用戶友好,並會用正確的格式繪製流程圖。

b。就像我之前在這裏所說的人們從不使用像a,b,c這樣的變量,因爲第二天你會試着繼續你離開的地方,你會忘記你的意思。

c。試着想辦法使用代碼來防止重複自己。 d)。使用硬編碼值是不好的做法(硬編碼含義:

return "this value to return is hard coded and will never change"; 

通過我得到回答你的問題的時候,我已經看到@Jonesy答案,是正確的,像我想什麼 建議這樣的病讓他回答。

希望這可以幫助別人:)

+0

非常感謝您抽出時間和打字,這確實有助於編碼對我而言是新的。 :) 但我有一個問題..你是什麼意思「防止重複自己」?在循環? – Ali 2014-11-04 16:26:39

+0

有很多地方我們可以「懶惰」,反覆使用相同的代碼行,如果你想檢查字符串是空還是空,你可以這樣寫: if(str == null || str == string.Empty)(注意string.Empty ill稍後解釋它) 或者你可以寫一個函數來實現這一點(這裏所有的舊編碼器都知道String對象中有一個函數用於演示) ,如果你繼續使用重複的if語句。 – Liran 2014-11-04 16:41:14

+0

現在因爲我說注意到「string.Empty」它就像當我說所有變量必須意味着什麼string.Empty意味着你的意思是它在那裏和字符串之間的區別.Empty和「」在我們的頭,你會看到,當你編寫更多的代碼時,會有一些地方你會離開「」以後填充,所以當你看到string.Empty你知道注意應該替換它。 – Liran 2014-11-04 16:41:58

0

嗯,這是我的版本的程序。我稍微改變了一下行爲(比如再次詢問該值是否無效)。這並不完美;例如,消息和RequestDebit方法應該在Account類之外處理,也許在AccountHandler類中處理,但是對於這個簡單的練習來說,所有這些都可能是矯枉過正的。無論如何,我希望你覺得它有用:

public class Account 
{ 
    public int Balance { get; set; } 

    public Account(int startingBalance) 
    { 
     this.Balance = startingBalance; 
    } 

    private bool Debit(int amount) 
    { 
     if (amount <= 0) 
     { 
      Console.WriteLine("\n\nError. Incorrect amount."); 
      return false; 
     } 

     if (amount > this.Balance) 
     { 
      Console.WriteLine("\n\nDebit amount exceeded account balance."); 
      return false; 
     } 

     this.Balance -= amount; 
     Console.WriteLine("\n\nWithdrawal was successful. Thankyou for using our services."); 
     return true; 
    } 

    public void RequestDebit() 
    { 
     bool success; 
     do 
     { 
      Console.Write("\n\nEnter the amount you want to withdraw in Rs: "); 
      int withdrawalAmount = Convert.ToInt32(Console.ReadLine()); 
      success = this.Debit(withdrawalAmount); 
     } while (!success); 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     int accountBalance; 
     do 
     { 
      Console.Write("Enter your account balance: "); 
      accountBalance = Convert.ToInt32(Console.ReadLine()); 
     } while (accountBalance <= 0); 

     var myAccount = new Account(accountBalance); 

     myAccount.RequestDebit(); 

     int inputNumber; 
     do 
     { 
      Console.WriteLine("\n\nPress 1 to exit, 2 to withdraw again, 3 to check your account balance.\n"); 
      inputNumber = Convert.ToInt32(Console.ReadLine()); 
      switch (inputNumber) 
      { 
       case 2: myAccount.RequestDebit(); 
        break; 
       case 3: 
        Console.WriteLine("\n\nYour remaining account balance is: {0}", myAccount.Balance); 
        break; 
      } 

     } while (inputNumber != 1); 
    } 
}