【发布时间】:2020-07-21 04:34:39
【问题描述】:
好吧,基本上我做了一个自动售货机程序。当用户购买的物品等于或大于其成本时,该物品的库存将减少 1。该物品最多可以购买 3 次,直到库存用完。此时,JButton 将消失,无法点击或购买该项目。为了增加复杂性,我添加了一个“检查总库存”按钮。单击时应该发生的是程序打印出最小到最大的库存量。
例如:
Inventory of Liton: 0
Inventory of Fanta: 1
Inventory of Pepsi: 2
Inventory of Gum: 3
Inventory of Seeds:3
Inventory of Chocolate: 3
为此,我使用了以下循环: 私人无效 CheckInventoryActionPerformed(java.awt.event.ActionEvent evt) {
int inventory[]={stock, stock2, stock3, stock4, stock5, stock6};
int temp;
boolean fixed= false;
while(fixed==false){
fixed=true;
for (int i=0; i<inventory.length-1; i++){
if(inventory[i]> inventory[i+1]){
temp= inventory[i+1];
inventory [i+1]= inventory[i];
inventory[i]= temp;
fixed= false;
}
}
}
for (int i=0; i<inventory.length; i++){
System.out.println("The inventory of Lipton is:" + inventory[i]);
System.out.println("The inventory of Fanta is:" + inventory[i+2]);
System.out.println("The inventory of Pepsi is:" +inventory[i+3]);
System.out.println("The inventory of Gum is:" +inventory[i+4]);
System.out.println("The inventory of Seeds is:" +inventory[i+5]);
System.out.println("The inventory of Choco is:" +inventory[i+6]);
}
}
虽然有一些问题...我想知道是否有人可以帮助我,因为我不知道问题是什么,尽管尝试了很多次来解决它。当我只点击芬达并购买一台时,(所以芬达库存应该是2),当我没有点击立顿时,立顿的库存会减少。 “巧克力的库存:和库存数量”也不打印。还有很多其他的错误比如
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6
at finalvending.finalvendingclass.CheckInventoryActionPerformed(finalvendingclass.java:623)
at finalvending.finalvendingclass.access$600(finalvendingclass.java:18)
at finalvending.finalvendingclass$7.actionPerformed(finalvendingclass.java:129)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
等等。
我不知道这些错误告诉我什么。但是我已经做了一些事情来尝试解决这个问题。首先,我运行了调试功能,但没有找到任何东西。我已经仔细检查了每个 int stock、int stock2、int stock3 等是否为循环上方的每个部分等正确定义。
这是我用于您可以在自动售货机中购买的每个部分的代码。基本上每个项目都是复制和粘贴,除了我更改价格和调用图像:如果需要,我还可以粘贴更多代码。立顿和芬达:
int stock=3;
private void liptonActionPerformed(java.awt.event.ActionEvent evt) {
final JPanel panel = new JPanel();
DecimalFormat dollarFormat = new DecimalFormat(" $#,##0.00");
String inputstring;
double input, change;
inputstring= JOptionPane.showInputDialog("Please enter your payment: $");
input= Double.parseDouble(inputstring);
change= input-1;
if (input<1)
{
JOptionPane.showMessageDialog(panel, " You do not have enough money to purchase this item", "INSUFFICIENT FUNDS",
JOptionPane.WARNING_MESSAGE);
}
else if (input>1)
{
JOptionPane.showInputDialog("You have purchased a Lipton Tea. Here is your change:" +(dollarFormat.format(change)));
stock--;
ImageIcon liptonIcon= new ImageIcon("usethislipton.png");
Image liptonImage= liptonIcon.getImage();
Image modifiedliptonImage= liptonImage.getScaledInstance(100,150, java.awt.Image.SCALE_SMOOTH);
liptonIcon= new ImageIcon (modifiedliptonImage);
JOptionPane.showMessageDialog(panel, "Lipton Tea", "Here is your drink! Enjoy!", JOptionPane.INFORMATION_MESSAGE, liptonIcon);
}
else if (input==1)
{
JOptionPane.showInputDialog("You have purchased a Lipton Tea.");
stock--;
ImageIcon liptonIcon= new ImageIcon("usethislipton.png");
Image liptonImage= liptonIcon.getImage();
Image modifiedliptonImage= liptonImage.getScaledInstance(100,150, java.awt.Image.SCALE_SMOOTH);
liptonIcon= new ImageIcon (modifiedliptonImage);
JOptionPane.showMessageDialog(panel, "Lipton Tea", "Here is your drink! Enjoy", JOptionPane.INFORMATION_MESSAGE, liptonIcon);
}
if (stock==1)
{
JOptionPane.showMessageDialog(panel, " There is only 1 Lipton Tea remaining! Restock recommnended", "WARNING",
JOptionPane.WARNING_MESSAGE);
}
else if (stock==0)
{
JOptionPane.showMessageDialog(panel, "Sorry, Lipton Tea is out of stock", "",
JOptionPane.WARNING_MESSAGE);
lipton.setVisible(false);
}
}
int stock2=3;
private void fantaActionPerformed(java.awt.event.ActionEvent evt) {
final JPanel panel = new JPanel();
DecimalFormat dollarFormat = new DecimalFormat(" $#,##0.00");
String inputstring;
double input, change;
inputstring= JOptionPane.showInputDialog("Please enter your payment: $");
input= Double.parseDouble(inputstring);
change= (input-1.25);
if (input<1.25)
{
JOptionPane.showMessageDialog(panel, " You do not have enough money to purchase this item", "INSUFFICIENT FUNDS",
JOptionPane.WARNING_MESSAGE);
}
else if (input>1.25)
{
JOptionPane.showInputDialog("You have purchased a Fanta. Here is your change" +(dollarFormat.format(change)));
stock2--;
ImageIcon fantaIcon= new ImageIcon("finalfanta2.png");
Image fantaImage= fantaIcon.getImage();
Image modifiedfantaImage= fantaImage.getScaledInstance(100,150, java.awt.Image.SCALE_SMOOTH);
fantaIcon= new ImageIcon (modifiedfantaImage);
JOptionPane.showMessageDialog(panel, "Fanta", "Here is your drink! Enjoy", JOptionPane.INFORMATION_MESSAGE, fantaIcon);
}
else if (input==1.25)
{
JOptionPane.showInputDialog("You have purchased a Fanta.");
stock2--;
ImageIcon fantaIcon= new ImageIcon("finalfanta2.png");
Image fantaImage= fantaIcon.getImage();
Image modifiedfantaImage= fantaImage.getScaledInstance(100,150, java.awt.Image.SCALE_SMOOTH);
fantaIcon= new ImageIcon (modifiedfantaImage);
JOptionPane.showMessageDialog(panel, "Fanta", "Here is your drink! Enjoy", JOptionPane.INFORMATION_MESSAGE, fantaIcon);
}
if (stock2==1)
{
JOptionPane.showMessageDialog(panel, " There is only 1 Fanta remaining! Restock recommnended", "WARNING",
JOptionPane.WARNING_MESSAGE);
}
else if (stock2==0)
{
JOptionPane.showMessageDialog(panel, "Sorry, Fanta is out of stock", "",
JOptionPane.WARNING_MESSAGE);
fanta.setVisible(false);
}
}
【问题讨论】: