2012-03-12 79 views
0

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

回答

0

我假設,除了數組,你是老師不允許你使用任何標準的集合對象。

你總是可以建立自己的LinkedList輸入糖果訂單 - 這很醜陋,但它會工作。一個單一的「鏈接」鏈是這樣的

public class CandyOrderLink { 
private String candyName; 
private Double candyPrice; 
private Double orderAmount; 
private CandyOrderLink nextOrderLink; 

public CandyOrderLink(String candyName, Double candyPrice, Double orderAmount) { 
    this.candyName = candyName; 
    this.candyPrice = candyPrice; 
    this.orderAmount = orderAmount; 
} 

public CandyOrderLink getNextLink() { 
    return nextOrder; 
} 

public void setNextLink(CandyOrderLink nextOrderLink) { 
    this.nextOrderLink= nextOrderLink; 
} 

public String getCandyName() { 
    return candyName; 
} 
public Double getCandyPrice() { 
    return candyPrice; 
} 
public Double getOrderAmount() { 
    return orderAmount; 
} 
} 

不知道如果我很抓分配的點,而是使用列表數據結構來跟蹤所有訂單將工作的。只需爲每個條目(candyName,price,amount)建立鏈接並將該鏈接設置爲上一個鏈接。在輸入結束時,通過在每個鏈接上重複調用getNextLink()並打印信息(如果適用)來遍歷列表。這是維基百科上關於鏈表的文章:http://en.wikipedia.org/wiki/Linked_list

+0

謝謝兄弟!以前從未聽說過這些!我會給他們一個好看的! – user1251814 2012-03-12 04:46:48

+1

沒問題;就像一個供參考,如果有人在StackOverflow上回答你的問題(或提供非常有用的指導),「接受」這個答案是習慣和體貼的。這種「接受」增加了該成員的聲譽指標。 – skiller3 2012-03-12 05:10:16

0

從問題的描述,我認爲沒有必要存儲進入,這樣可以排序的數據。 ab均陳述糖果的簡單條件:大於2,000磅和至少$ 10 /磅。輸入後可以立即打印每個條目。

但是,您的示例輸出意味着您必須選取與該描述相矛盾的單一最暢銷的糖果。哪個是對的?