2016-11-19 69 views
1

我正在写一个类的程序,需要一些关于如何让它工作的建议。我有一个程序要求用户输入物品的价格。一旦他们输入第一个物品的价格,那么它会要求他们提供下一个价格。问题是我需要一个do-while循环。我正在尝试获取每个while循环实例后输入项目的总价格。我知道我可以使用数组或列表,但分配不需要这样做。 任何建议都会有所帮助。虽然循环练习/ w用户输入

以下是do while循环的代码。

public static void Main() 
    { 


     double cost; 
     int Items; 
     string counter; 
     string value; 
     string more; 
     double newtotal; 
     int loopcounter = 0; 

     //item cost 
     Console.WriteLine("Welcome to the Online Store"); 
     Console.WriteLine("How many items are you buying?"); 
     counter = Console.ReadLine(); 
     Items = Convert.ToInt32(counter); 

     do 
     { 
      // buying items 

      Console.WriteLine("Enter the cost of your item one by one"); 
      value = Console.ReadLine(); 
      cost = Convert.ToDouble(value); 
      newtotal = +cost; 
      loopcounter++; 
     } 

     while (loopcounter < Items); 
+0

旁注,它应该是'newtotal + = cost',你现在的方式是将总成本设为成本,而不是增加到变量 – pinkfloydx33

回答

0

newtotal = + cost后写下面的代码;语句做成while:Console.WriteLine(“您的总金额:{0}”,newtotal);

0

newtotal = + cost;是错误的 改为输入newtotal + = cost;

移动

 Console.WriteLine("Enter the cost of your item one by one"); 

出从环路,因为你不必告诉买家一次又一次进入一个接一个......

好保持它