【发布时间】:2014-06-17 07:05:20
【问题描述】:
我为我的期末课程编写了一个自动售货机程序,但我不断收到堆栈溢出错误。 我相信它与继承有关,但不确定,我试过在没有帮助的情况下改变它。
非常感谢你们的任何帮助。谢谢。
这是我的程序:
package vendingmachine;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
public class VendingMachine {
public static void main(String[] args) {
VendMachine freestyle = new VendMachine();
int selectedOption = JOptionPane.showConfirmDialog(null, "Would you like to use the vending machine?", "Vending Machine",
JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.NO_OPTION) {
System.exit(selectedOption);
} else {
freestyle.offerChoice();
}
}
}
class VendMachine {
Snacks temp = new Snacks();
private float moneyPaid = 0;
int maxBevCount = 50;
int maxSnackCount = 100;
float itemPrice = .65f;
private final String coke = "Coke Zero";
private final String sprite = "Sprite Zero";
private final String water = "Dasani";
private float moneyReturned = 0;
private int ask;
NumberFormat formating = NumberFormat.getCurrencyInstance();
public void offerChoice() {
ask = Integer.parseInt(JOptionPane.showInputDialog(null, "Would you like a Snack: 1 or Beverage: 2 ", "Vending Machine",
JOptionPane.INFORMATION_MESSAGE));
if (ask == 1) {
temp.askUser();
} else {
int askBevType = Integer.parseInt(JOptionPane.showInputDialog(null, "What type of beverage would you like? " + coke + ": 1 " + sprite
+ ": 2 " + water + ": 3 " + "Quit: 4 ", "Vending Machine", JOptionPane.INFORMATION_MESSAGE));
switch (askBevType) {
case 1: {
JOptionPane.showMessageDialog(null, "you chose " + coke + " the price is: " + formating.format(itemPrice));
addMoney();
break;
}
case 2: {
JOptionPane.showMessageDialog(null, "you chose " + sprite + " the price is: " + formating.format(itemPrice));
addMoney();
break;
}
case 3: {
JOptionPane.showMessageDialog(null, "you chose " + water + " the price is: " + formating.format(itemPrice));
addMoney();
break;
}
case 4: {
System.exit(0);
}
default: {
break;
}
}
}
}
public float addMoney() {
moneyPaid = Float.parseFloat(JOptionPane.showInputDialog(null, " insert your money", "Vending Machine", JOptionPane.INFORMATION_MESSAGE));
while (true) {
if (moneyPaid == itemPrice) {
vend();
}
if (moneyPaid < itemPrice) {
JOptionPane.showMessageDialog(null, "that's not enough... please insert more money!", "Vending Machine", JOptionPane.ERROR_MESSAGE);
float moreCashIn = Float.parseFloat(JOptionPane.showInputDialog(null, "The items cost " + formating.format(itemPrice)
+ " insert your money.", "Vending Machine", JOptionPane.INFORMATION_MESSAGE));
if (moreCashIn >= itemPrice) {
vend();
}
} else if (moneyPaid > itemPrice) {
moneyReturned = moneyPaid - itemPrice;
vend();
JOptionPane.showMessageDialog(null, "And here is your change of " + formating.format(moneyReturned), "Vending Machine",
JOptionPane.INFORMATION_MESSAGE);
}
return moneyPaid;
}
}
public void vend() {
JOptionPane.showMessageDialog(null, "Vending........Here you go. Have a nice day!", "Vending Machine", JOptionPane.INFORMATION_MESSAGE);
restock();
}
public void restock() {
if (ask == 2) {
maxBevCount -= 1;
JOptionPane.showMessageDialog(null, "Beverage Stock in the machine is now " + maxBevCount, "Vending Machine",
JOptionPane.INFORMATION_MESSAGE);
maxBevCount += 1;
JOptionPane.showMessageDialog(null, "Machine is now restocked with " + maxBevCount + " Beverages ", "Vending Machine",
JOptionPane.INFORMATION_MESSAGE);
} else {
maxSnackCount -= 1;
JOptionPane.showMessageDialog(null, "Stock in the machine is now " + maxSnackCount, "Vending Machine", JOptionPane.INFORMATION_MESSAGE);
maxSnackCount += 1;
JOptionPane.showMessageDialog(null, "Machine is now restocked with " + maxSnackCount + " items", "Vending Machine",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
class Snacks extends VendMachine {
int askSnackType;
EmAndM emAndM = new EmAndM("M&M's", 180, 1.80f);
Snickers snickers = new Snickers("Snickers", 280, 2.50f);
Gum gum = new Gum("Gum", 110, 1.25f);
Popcorn popcorn = new Popcorn("Popcorn", 190, 2.30f);
Crackers crackers = new Crackers("Crackers", 130, 1.90f);
Chips chips = new Chips("Chips", 140, 1.70f);
public void askUser() {
askSnackType = Integer.parseInt(JOptionPane.showInputDialog(null, "Would you like some- " + emAndM.name + ": 1 " + snickers.name + ": 2 "
+ gum.name + ": 3 " + popcorn.name + ": 4 " + crackers.name + ": 5 " + chips.name + ": 6 " + " Quit: 7 ", "Vending Machine",
JOptionPane.INFORMATION_MESSAGE));
switch (askSnackType) {
case 1: {
JOptionPane.showMessageDialog(null,
"You chose " + emAndM.name + " , Calories: " + emAndM.calories + " the price is: " + formating.format(emAndM.price));
addMoney();
break;
}
case 2: {
JOptionPane.showMessageDialog(null,
"You chose " + snickers.name + " , Calories: " + snickers.calories + " the price is: " + formating.format(snickers.price));
addMoney();
break;
}
case 3: {
JOptionPane.showMessageDialog(null,
"You chose " + gum.name + " , Calories: " + gum.calories + " the price is: " + formating.format(gum.price));
addMoney();
break;
}
case 4: {
JOptionPane.showMessageDialog(null,
"You chose " + popcorn.name + " , Calories: " + popcorn.calories + " the price is: " + formating.format(popcorn.price));
addMoney();
break;
}
case 5: {
JOptionPane.showMessageDialog(null,
"You chose " + crackers.name + " , Calories: " + crackers.calories + " the price is: " + formating.format(crackers.price));
addMoney();
break;
}
case 6: {
JOptionPane.showMessageDialog(null,
"You chose " + chips.name + " , Calories: " + chips.calories + " the price is: " + formating.format(chips.price));
addMoney();
break;
}
case 7: {
System.exit(0);
}
default: {
break;
}
}
}
class SugarySnacks {}
class EmAndM extends Snacks {
String name;
int calories;
float price;
EmAndM(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
class Snickers extends Snacks {
String name;
int calories;
float price;
Snickers(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
class Gum extends Snacks {
String name;
int calories;
float price;
Gum(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
class SaltySnacks {}
class Popcorn extends Snacks {
String name;
int calories;
float price;
Popcorn(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
class Crackers extends Snacks {
String name;
int calories;
float price;
Crackers(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
class Chips extends Snacks {
String name;
int calories;
float price;
Chips(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
}
【问题讨论】:
-
Stacktrace 将列出导致溢出的方法。
-
您需要更好地格式化您发布的代码。我不是在为你调试你的程序,但我会给你一个提示。 “堆栈”是已调用的一系列方法。每次调用一个方法时,它都会被添加到堆栈的顶部,当它完成时,它会被移除。如果方法 A 调用另一个方法 B,则 B 将被放置在堆栈中 A 的顶部。如果 B 调用了另一个方法 C,那么它将被放置在 B 的顶部。当堆栈上的调用过多时,会发生溢出。所以看看你是否能知道方法在哪里被调用(添加)但永远不会结束(删除)。
-
我强烈建议使用一致的缩进来使代码更易于阅读。
-
class SugarySnacks {}喜欢这个 -
Gum 扩展了 Snacks,Snacks 扩展了 VendMachine。 Snack 是一种自动售货机(但不是 VendingMachine;它是与 VendMachine 不同的类)。
标签: java