2015-02-24 49 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... 
}