【问题标题】:Errors Displaying Smallest to Largest amount of Inventory Array (Java)显示最小到最大库存数组 (Java) 的错误
【发布时间】: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);
        }


}                                     

【问题讨论】:

    标签: java arrays loops


    【解决方案1】:

    您正在循环所有数组并获取下一个位置进行比较,当您位于数组末尾并转到下一个位置时,您会得到 java.lang.ArrayIndexOutOfBoundsException: 6

    for (int i=0; i<inventory.length-1; i++){
                if(inventory[i]> inventory[i+1]){ //here                   
                    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]);   
        }
    

    你犯了同样的错误(检查 i + x)

    你用的是哪个版本的java?

    【讨论】:

    • 好的,谢谢!过去一天左右我一直在考虑,抱歉延迟回复。我一直在尝试解决您正在讨论的第一个问题,但我仍在考虑如何解决它...至于库存[i],我已修复,因此 Fanat 说“”+库存[1+1], Pepsi 库存 [i+2] 等。我不小心跳过了第一个位置,这就是 Fana 和 Lipton 库存在打印时发生变化的原因。我正在使用 Netbeans:版本 8.2
    【解决方案2】:

    好的,看这里的这段代码:

    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]);   
        }
    

    变量“i”将从 0 变为inventory.length-1(含)。例如,以最极端的情况

    i = inventory.length()-1
    

    现在,让我们看看你的程序会做什么。

    它首先从打印开始

    The inventory of Liptop is: inventory[inventory.length-1]
    

    这里没有错。但是,下一行是您的程序开始失败的地方。

    i+2 将是inventory.length+1!显然,这超出了库存的数组索引。

    【讨论】:

    • 嘿克里斯!感谢您的答复。对不起,我对你的回答有点困惑。所以发布后,我注意到我在 for 循环下的 i + X 调用了错误的位置,所以我改变了 Fanta [inventory i+1] 而不是 i +2。我想如果我没看错你的帖子,我是这样做的:
    • 我不知道如何把它放在代码中,所以很抱歉它的浓缩。无论如何,这仍然会引发 ArrayOutofboundsexception:6。我可能只是误解了您的意思,请您详细讨论一下我们为什么要更改 i
    • ` for (int i=inventory.length-1; i
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2011-03-03
    相关资源
    最近更新 更多