【问题标题】:How to use repetition in C#?如何在 C# 中使用重复?
【发布时间】:2015-02-24 15:58:39
【问题描述】:

我试图让此代码在显示最后一行后继续重复,并根据需要重复多次,然后在为帐号输入特定数字后显示终止消息。最好的方法是什么?我一直在努力尝试解决 if 语句,也许有人有解决方案。

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
   }
}

【问题讨论】:

  • 您熟悉while 循环吗?
  • 请买一本好的C#书,这是一个非常基础的问题。
  • while (true) 怎么样?那肯定会重复... :)
  • 我猜最里面的括号是你想要重复的,在几乎所有的编程中,这被称为循环。您应该能够在您最喜欢的搜索引擎上查找有关循环的大量资源

标签: c# visual-studio-2012 repeat


【解决方案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...
}

【讨论】:

    猜你喜欢
    • 2017-07-16
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多