提示: 接受糖果名稱(例如「巧克力覆蓋的藍莓」),每磅價格和數量在平均月份內賣出的磅數,並且只有在商品是暢銷商品時才顯示該商品的數據。暢銷產品是每月銷售超過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