这个项目无法运行。请帮帮我。 http://pastebin.com/FX3JdUqb
IceCreamFlavor printPurchasedIceCreams()
这个方法只是返回ArrayList中的所有冰淇淋,下面是我的代码:
IceCreamShopMain.java:
public class IceCreamShopMain {
public static void main(String[] args) {
System.out.println(IceCreamFlavor.HAZELNUT.getName());
System.out.println("The price of ice-cream is " + IceCreamFlavor.HAZELNUT.getPrice() + " dollars.");
Customer c = new Customer(IceCreamFlavor.WHIPPEDCREAM);
c.setCustomerName("Amy");
Customer c2 = new Customer(IceCreamFlavor.ALMOND, "Mawson");
System.out.println(c.getCustomerName() + "`s purchased items are " + c.printPurchasedIceCreams() + ".");
c.buy(IceCreamFlavor.OREOCOOKIECRUMBLES);
System.out.println(c.printPurchasedIceCreams());
System.out.println(c.icecream.getName());
System.out.println(c2.getCustomerName() + " bought " + c2.icecream.getName() + "-Ice"
+ " which was " + c2.icecream.getPrice() + " dollars.");
Customer c3 = new Customer(IceCreamFlavor.OREOCOOKIECRUMBLES, "Mickey");
c3.buy(IceCreamFlavor.ALMOND);
c3.buy(IceCreamFlavor.WHIPPEDCREAM);
System.out.println(c3.printPurchasedIceCreams());
}
}
Customer.java:
import java.util.*;
public class Customer {
public IceCreamFlavor icecream;
public int price;
private String customerName;
static String hamper;
List<IceCreamFlavor> purchasedIceCreams = new ArrayList<IceCreamFlavor>();
Customer(IceCreamFlavor icecream) {
this.icecream = icecream;
}
Customer(IceCreamFlavor icecream, String customerName) {
this.icecream = icecream;
this.customerName = customerName;
}
// String getName() {
// return icecream.getName();
// }
//
// int getPrice() {
// return icecream.getPrice();
// }
String getCustomerName() {
return this.customerName;
}
void setCustomerName(String customerName) {
this.customerName = customerName;
}
void buy(IceCreamFlavor icecream) {
// not really sure what business do here
// just return the Ice cream.
// this.icecream = icecream;
purchasedIceCreams.add(icecream);
}
IceCreamFlavor printPurchasedIceCreams() {
// not really sure what business do here
// just return the Ice cream.
for(int i = 0; i < purchasedIceCreams.size(); i++) {
purchasedIceCreams.get(i);
return purchasedIceCreams.get(i);
}
}
}
IceCreamFlavor.java:
enum IceCreamFlavor {
HAZELNUT("Hazelnut", 300), WHIPPEDCREAM("WhippedCream", 270), ALMOND("Almond",
370), OREOCOOKIECRUMBLES("OreoCookieCrumbles", 500), MARSHMALLOWS("Marshmallows", 420);
public String name;
public int price;
private IceCreamFlavor(String name, int price) {
this.name = name;
this.price = price;
}
String getName() {
return name;
}
int getPrice() {
return price;
}
}
错误日志:
线程“main”中的异常java.lang.Error:未解析的编译 问题:此方法必须返回IceCreamFlavor类型的结果
在Customer.printPurchasedIceCreams(Customer.java:45)at IceCreamShopMain.main(IceCreamShopMain.java:8)此方法必须返回 IceCreamFlavor类型的结果
答案 0 :(得分:1)
Java总是希望100%确定你在not void函数中返回一些东西。它可能发生在purchasedIceCreams.size() == 0
,在这种情况下,执行不会进入printPurchasedIceCreams()
中的for循环,所以不能保证你返回一些东西,这就是你得到Unresolved编译错误的原因:
IceCreamFlavor printPurchasedIceCreams() {
for(int i = 0; i < purchasedIceCreams.size(); i++) {
purchasedIceCreams.get(i);
return purchasedIceCreams.get(i);
}
}
您可以通过在for循环外部的函数末尾添加return null;
或重新构建函数来快速解决问题。
IceCreamFlavor printPurchasedIceCreams() {
for(int i = 0; i < purchasedIceCreams.size(); i++) {
return purchasedIceCreams.get(i);
}
return null;
}
Here我发现了与您类似的问题,您可能需要查看以了解详情。
附注:这与错误无关,但不正确的做法是方法名称与其功能不匹配。您应该有两个单独的方法:printPurchasedIceCreams()
方法执行所有打印但不返回任何内容,并使用getFirstPurchasedIceCream()
方法返回第一个冰淇淋(或null)。
答案 1 :(得分:-1)
这还行吗?我感激不尽。
List<IceCreamFlavor> printPurchasedIceCreams() {
// not really sure what business do here
for (int i = 0; i < purchasedIceCreams.size(); i++) {
System.out.println(purchasedIceCreams.get(i));
}
return purchasedIceCreams;
}