【发布时间】:2015-08-18 09:53:57
【问题描述】:
我想创建一个循环来询问价格,然后在每次迭代发生时将该价格存储在数组的新对象中。
double itemPrice = 0;
double totalPrice = 0;
const double OVER_FIFTY_DISCOUNT = .1;
double priceSentinal = 0;
Console.WriteLine("Please input the price of your item. Enter '0' if you have no more items.");
itemPrice = Convert.ToDouble(Console.ReadLine());
while (itemPrice != priceSentinal)
{
Console.WriteLine("Please input the price of your item.");
itemPrice = Convert.ToDouble(Console.ReadLine());
double[] originalPrices = itemPrice.ToDoubleArray();
if (itemPrice >= 50)
{
itemPrice = itemPrice * OVER_FIFTY_DISCOUNT;
}
totalPrice = totalPrice + itemPrice;
double[] allPrices = itemPrice.ToDoubleArray(); // how do I place every iteration of itemPrice
Console.WriteLine("Your total Price is " + totalPrice); //into a new array value?
}
//How do I print every object in an array onto the screen?
//How do I add all of the objects within an array together and print them onto the screen?
Console.WriteLine("The prices of your items before discounts are " + originalPrices[]);
Console.WriteLine("The total price of your items before discounts is " originalPrices[]);
Console.WriteLine("The prices of your items after discounts are " + allPrices[]);
Console.WriteLine("The total price of your items after discounts is " + allPrices[]);
我不知道如何为循环的每次迭代添加一个新对象到双精度数组。我也不知道如何将数组中的每个对象打印到屏幕上,也不知道如何添加数组中的所有对象并将它们打印到屏幕上。有人可以帮我修复我的代码吗?
【问题讨论】:
-
而不是使用数组使用 ArrayList
-
还有一个更大的问题:如果一个 var 类型的 double 用作累加器加上另一个 int var 用作计数器可以完成这项工作,为什么还要使用数组。最好的问候,
-
ArrayList???不,它几乎已被弃用。使用 List
或 List !
标签: c# arrays loops while-loop