2012-03-12 78 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 /磅。输入后可以立即打印每个条目。

但是,您的示例输出意味着您必须选取与该描述相矛盾的单一最畅销的糖果。哪个是对的?