显示库存数组从最小到最大的数量时出错(Java)

显示库存数组从最小到最大的数量时出错(Java),java,arrays,loops,Java,Arrays,Loops,好吧,基本上我做了一个自动售货机程序。当用户购买等于或大于其成本的物品时,该物品的库存将消耗1。该物品最多可购买三次,直至库存耗尽。在这一点上,JButton将消失,物品无法点击或购买。为了增加复杂性,我添加了一个“检查总库存”按钮。当点击时,程序会打印出从最小到最大的库存量 int inventory[]={stock, stock2, stock3, stock4, stock5, stock6}; int temp; boolean fixed= fal

好吧,基本上我做了一个自动售货机程序。当用户购买等于或大于其成本的物品时,该物品的库存将消耗1。该物品最多可购买三次,直至库存耗尽。在这一点上,JButton将消失,物品无法点击或购买。为了增加复杂性,我添加了一个“检查总库存”按钮。当点击时,程序会打印出从最小到最大的库存量

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

Inventory of Liton: 0
Inventory of Fanta: 1
Inventory of Pepsi: 2 
Inventory of Gum: 3
Inventory of Seeds:3
Inventory of Chocolate: 3
    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]);   
    }
}                                              
为此,我使用了以下循环: 私有无效检查InventoryActionPerformed(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]);   
    }
}                                              
等等

    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]);   
    }
}                                              
我不知道这些错误告诉了我什么。但我做了一些事情来尝试纠正这一点。首先,我运行了调试功能,但没有找到任何东西。我仔细检查了每个int-stock、int-stock2、int-stock3等是否为循环上方的每个部分正确定义,等等

    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]);   
    }
}                                              
这是我在自动售货机上可以买到的每一部分的代码。基本上,每一项都是复制和粘贴的,除了我改变价格,图像被称为:如果需要,我还可以粘贴更多的代码。Lipton和Fanta:

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


}                                     
int-stock=3;
私有void liptonActionPerformed(java.awt.event.ActionEvent evt){
最终JPanel面板=新JPanel();
DecimalFormat dollarFormat=新的DecimalFormat($,$0.00”);
字符串输入字符串;
双输入,改变;
inputstring=JOptionPane.showInputDialog(“请输入您的付款:$”;
input=Double.parseDouble(inputstring);
变更=输入-1;
如果(输入1)
{
showInputDialog(“您购买了一杯立顿茶。这是您的零钱:”+(dollarFormat.format(change));
股票--;
ImageIcon liptonIcon=新的ImageIcon(“usethislipton.png”);
Image liptonImage=liptonIcon.getImage();
Image modifiedliptonImage=liptonImage.getScaledInstance(100150,java.awt.Image.SCALE\u-SMOOTH);
liptonIcon=新图像图标(修改的iptonimage);
showMessageDialog(面板,“立顿茶”,“这是您的饮料!享受!”,JOptionPane.INFORMATION\u消息,立顿图标);
}
else if(输入=1)
{
showInputDialog(“您购买了一杯立顿茶。”);
股票--;
ImageIcon liptonIcon=新的ImageIcon(“usethislipton.png”);
Image liptonImage=liptonIcon.getImage();
Image modifiedliptonImage=liptonImage.getScaledInstance(100150,java.awt.Image.SCALE\u-SMOOTH);
liptonIcon=新图像图标(修改的iptonimage);
showMessageDialog(面板,“立顿茶”,“这是您的饮料!享受”,JOptionPane.INFORMATION\u消息,立顿图标);
}
如果(股票=1)
{    
showMessageDialog(面板,“只剩下1杯立顿茶!建议重新进货”,“警告”,
JOptionPane。警告消息);
} 
否则如果(库存==0)
{
showMessageDialog(面板“对不起,立顿茶缺货了”,“”,
JOptionPane。警告消息);
lipton.setVisible(假);
}
}                                      
int-stock2=3;
私有void fantaActionPerformed(java.awt.event.ActionEvent evt){
最终JPanel面板=新JPanel();
DecimalFormat dollarFormat=新的DecimalFormat($,$0.00”);
字符串输入字符串;
双输入,改变;
inputstring=JOptionPane.showInputDialog(“请输入您的付款:$”;
input=Double.parseDouble(inputstring);
变化=(输入-1.25);
如果(输入1.25)
{
showInputDialog(“您已经购买了一个Fanta。这是您的更改”+(dollarFormat.format(更改));
股票2--;
ImageIcon fantaIcon=新的ImageIcon(“finalfanta2.png”);
Image fantaImage=fantaIcon.getImage();
Image-modifiedfantaImage=fantaImage.getScaledInstance(100150,java.awt.Image.SCALE\u-SMOOTH);
fantaIcon=新图像图标(修改后的图像);
showMessageDialog(面板“Fanta”,“这是您的饮料!享受”,JOptionPane.INFORMATION\u MESSAGE,fantaIcon);
}
否则如果(输入==1.25)
{
showInputDialog(“您已经购买了一个Fanta。”);
股票2--;
ImageIcon fantaIcon=新的ImageIcon(“finalfanta2.png”);
Image fantaImage=fantaIcon.getImage();
Image-modifiedfantaImage=fantaImage.getScaledInstance(100150,java.awt.Image.SCALE\u-SMOOTH);
fantaIcon=新图像图标(修改后的图像);
showMessageDialog(面板“Fanta”,“这是您的饮料!享受”,JOptionPane.INFORMATION\u MESSAGE,fantaIcon);
}
if(stock2==1)
{    
showMessageDialog(面板,“仅剩下1个扇塔!建议重新进货”,“警告”,
JOptionPane。警告消息);
}
else if(stock2==0)
{
showMessageDialog(面板“对不起,Fanta缺货了”,“”,
JOptionPane。警告消息);
fanta.setVisible(假);
}
}                                     

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

    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]);   
    }
}                                              
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(inti=0;i库存[i+1]){//此处
温度=库存[i+1];
存货[i+1]=存货[i];
存货[i]=临时性;
固定=假;
}
}
解决此问题时,下一步是检查以下代码:

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

for(int i=0;i您正在循环所有数组并获得下一个比较位置,当您在数组末尾并转到下一个位置时,您将获得java.lang.ArrayIndexOutOfBoundsException:6

    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]);   
    }
}                                              
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(inti=0;i库存[i+1]){//此处
温度=英寸