提示: 接受糖果名称(例如“巧克力覆盖的蓝莓”),每磅价格和数量在平均月份内卖出的磅数,并且只有在商品是畅销商品时才显示该商品的数据。畅销产品是每月销售超过2000磅的产品。 b。一个程序,不断接受糖果数据,直到输入标记值并显示高价,畅销商品列表。练习2a定义了最畅销的商品。高价位的商品是以每磅10美元或以上的价格出售的商品。无法使用数组或foreach循环遍历数据列表并仅打印出某些值
这里是操作一个好的设计的例子:
High-priced, Best-selling Candy
Fudge $12.50 4500 lbs
Vanilla Creme $13.75 2200 lbs.
Fudge, 12.50, 4500 Jawbreakers, 6.50, 5500 Chocolate, 14.00, 790 Butterscotch, 9.50, 4500 Vanilla Creme, 13.75, 2200
Item that sold most pounds: Jawbreakers
,但我遇到的问题是,我的老师是不是让我用for循环,或阵列。我不想定义同一个变量的多个实例,因为它是有限的一定数量......这样做最有效的方法是什么?
start
// Declarations
num QUIT = "Y";
final String HEADING = "High Priced, Best Selling Candy" + "\n" + "\n";
final String HSPS = candyName + " " + candyPrice + " " + candySold + " ";
final String MOSTSOLD = "Item that sold the most pounds is "
while <> QUIT;
enterCandy();
printHighPriceBestSelling();
printSoldMostPounds();
endwhile;
stop
entercandy()
String candyName = "poop";
double candyPrice = 0.0;
double candyWeight = 0.0;
int candySold = 0;
output "Please enter name of candy.";
input candyName;
output "Please enter candy price.";
input candyPrice;
output "Please enter pounds sold.";
input candySold;
printHighPriceBestSelling()
if(candySold > 2000 && candyPrice > 10)
{
output HEADING;
output HSPS;
}
else
{
output "There were/are no best selling, high priced candy!"
}
printSoldMostPounds();
//There is no basis for comparison.
这样做只有两种方法。创建大量不同的,任意的和预定义的变量,以便循环填充,直到它们被覆盖。让我们说10或者创建一个数组。我相信有嵌套的if/switch/while循环的过度复杂的方法,但为什么要教我们/迫使我们使用丑陋的低效方式?
output "MOSTSOLD ";
谢谢兄弟!以前从未听说过这些!我会给他们一个好看的! – user1251814 2012-03-12 04:46:48
没问题;就像一个供参考,如果有人在StackOverflow上回答你的问题(或提供非常有用的指导),“接受”这个答案是习惯和体贴的。这种“接受”增加了该成员的声誉指标。 – skiller3 2012-03-12 05:10:16