一个困扰我的问题,望高手能帮我解答!
三个java文件分别如下:
1、Item.java:
package com.prefect.ecommerce;
import java.util.*;
public class Item implements Comparable
{
private String id;
private String name;
private double retail;
private int quantity;
private double price;
Item(String idIn, String nameIn, String retailIn, String quanIn)
{
id=idIn;
name=nameIn;
retail=Double.parseDouble(retailIn);
quantity=Integer.parseInt(quanIn);
if(quantity>400)
price=retail*.5D;
else if(quantity>200)
price=retail*.6D;
else
price=retail*7D;
price=Math.floor(price*100+ .5)/100;
}
public int compareTo(Object obj)
{
Item temp=(Item)obj;
if(this.price<temp.price)
return 1;
else if(this.price>temp.price)
return -1;
return 0;
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
public double getRetail()
{
return retail;
}
public int getQuantity()
{
return quantity;
}
public double getPrice()
{
return price;
}
}
2、Storefront.java:
package com.prefect.ecommerce;
import java.util.*;
public class Storefront
{
private LinkedList catalog=new LinkedList();
public void addItem(String id, String name, String price, String quant)
{
Item it=new Item(id,name,price,quant);
catalog.add(it);
}
public Item getItem(int i)
{
return (Item)catalog.get(i);
}
public int getSize()
{
return catalog.size();
}
public void sort()
{
Collections.sort(catalog);
}
}
3、GiftShop.java:
import com.prefect.ecommerce.*;
public class GiftShop
{
public static void main(String[] args)
{
Storefront store=new Storefront();
store.addItem("C01","MUG","9.99","150");
store.addItem("C02","LG MUG","12.99","82");
store.addItem("C03","MOUSEPAD","10.49","800");
store.addItem("C04","T SHIRT","16.99","90");
store.sort();
for(int i=0;i<store.getSize();i++)
{
Item show=(Item)store.getItem(i);
System.out.println("Item ID: "+show.getId());
System.out.println("Name: "+show.getName());
System.out.println("Retail: "+show.getRetail());
System.out.println("Price: "+show.getPrice());
System.out.println("Quantity: "+show.getQuantity());
}
}
}
分别编译,编译顺序1,2,3。
就会出现错误。
我试着分别导入1,2编译后生成的类Item.class和Storefront.class,即:
3、GiftShop.java变成:
import com.prefect.ecommerce.Item;
import com.prefect.ecommerce.Storefront;
public class GiftShop
{
public static void main(String[] args)
{
Storefront store=new Storefront();
store.addItem("C01","MUG","9.99","150");
store.addItem("C02","LG MUG","12.99","82");
store.addItem("C03","MOUSEPAD","10.49","800");
store.addItem("C04","T SHIRT","16.99","90");
store.sort();
for(int i=0;i<store.getSize();i++)
{
Item show=(Item)store.getItem(i);
System.out.println("Item ID: "+show.getId());
System.out.println("Name: "+show.getName());
System.out.println("Retail: "+show.getRetail());
System.out.println("Price: "+show.getPrice());
System.out.println("Quantity: "+show.getQuantity());
}
}
}
这样改后居然可以正确执行。
想了好久都想不通,望高手解答。感谢不已!!
问题点数:20、回复次数:7Top
1 楼infowain(infowain)回复于 2006-07-04 18:30:07 得分 0
你用什么编译的? javac?
Top
2 楼reweialaye()回复于 2006-07-04 18:33:34 得分 0
SDK、JBuild和JCreator都试过。Top
3 楼reweialaye()回复于 2006-07-04 22:14:24 得分 0
希望有人可以帮忙~!Top
4 楼foxxiao111()回复于 2006-07-04 23:07:33 得分 0
应该是编译工具的问题
你在dos窗口编译看,我试过没问题Top
5 楼trumplet(检查)回复于 2006-07-04 23:16:15 得分 0
可能和类路径的设置有关Top
6 楼Pigwen(Pigwen)回复于 2006-07-04 23:24:07 得分 0
倘若GiftShop.java位于目录A,要把Storefront.java和Item.java放到A\com\prefect\ecommerce下,再编译GiftShop.java就行了.
编译Storefront.java的时候老是说使用了不安全的操作,不过程序仍然可以运行,没细看你的代码,是不是有错误,你自己再看看.Top
7 楼trumplet(检查)回复于 2006-07-05 07:26:03 得分 0
你使用的是jdk1.5, 那是个警告, 如果要消除这个结果,该这句:
private LinkedList<Item> catalog=new LinkedList<Item>();Top




