2015-02-24 57 views
-9

我試圖讓這個代碼在顯示最後一行時不斷重複,然後重複任意次數,然後一旦爲帳號輸入一定的數字就顯示終止消息。什麼是最好的方法來做到這一點?我一直在努力爭取一個if語句來解決問題,也許有人有解決方案。如何在C#中使用重複?

public static void Main(string[] args) 
{ //begin main 
    int AccountNumber; //declare account number 

    int BeginingBalance; //declare begining balance 

    int TotalItemsCharged; //declare total items charged 

    int TotalCreditsApplied; //declare total credits applied 

    int CreditLimit; //declare credit limit 

    Double Balance; //declare end balance 

    { 
    Console.Write("account number: "); // prompt user for account number 
    AccountNumber = Convert.ToInt32(Console.ReadLine()); 

    Console.Write("begining balance: "); //prompt user for begining balance 
    BeginingBalance = Convert.ToInt32(Console.ReadLine()); 

    Console.Write("total items charged: "); 
    TotalItemsCharged = Convert.ToInt32(Console.ReadLine()); //prompt user for items charged 

    Console.Write("total credits applied:"); 
    TotalCreditsApplied = Convert.ToInt32(Console.ReadLine()); //prompt user for credits applied 

    Console.Write("credit limit:"); //prompt user for credit limit 
    CreditLimit = Convert.ToInt32(Console.ReadLine()); 

    Balance = BeginingBalance + TotalItemsCharged - TotalCreditsApplied; 

    Console.Write("balance is: {0}", Balance); //display calculated balance 

    if (Balance > CreditLimit) //set if statement 
     Console.WriteLine(" Credit Limit Exceeded"); //set display for true if statement 

    Console.ReadLine(); 
     } // end main 
    } 
} 
+0

有你熟悉了'while'循環? – 2015-02-24 16:00:31

+2

請拿一本好的C#書,這是一個非常基本的問題。 – dymanoid 2015-02-24 16:00:38

+0

https://msdn.microsoft.com/en-us/library/2aeyhxcd.aspx – 2015-02-24 16:01:07

回答

1
// Keep asking for an account number until 0 is entered as account number 
while ((AccountNumber = Convert.ToInt32(Console.ReadLine())) != 0) 
{ 
    // No need to ask for the account number again, the while loop takes care 
    // of that. 

    // So... 
    // do stuff, such as... 
    BeginningBalance = Convert.ToInt32(Console.ReadLine()); 
    // and more stuff... etc... 
}