错误显示库存数组的最小到最大金额(Java)

huangapple 未分类评论50阅读模式
英文:

Errors Displaying Smallest to Largest amount of Inventory Array (Java)

问题

以下是您提供的内容的翻译:

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

例如:

Liton的库存:0
Fanta的库存:1
Pepsi的库存:2
Gum的库存:3
Seeds的库存:3
Chocolate的库存:3

为了实现这一点,这是我使用的循环:

private void CheckInventoryActionPerformed(java.awt.event.ActionEvent evt) {

    int inventory[] = {stock, stock2, stock3, stock4, stock5, stock6};

    int temp;
    boolean fixed = false;

    while (!fixed) {
        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("Lipton的库存:" + inventory[i]);
        System.out.println("Fanta的库存:" + inventory[i + 2]);
        System.out.println("Pepsi的库存:" + inventory[i + 3]);
        System.out.println("Gum的库存:" + inventory[i + 4]);
        System.out.println("Seeds的库存:" + inventory[i + 5]);
        System.out.println("Chocolate的库存:" + inventory[i + 6]);
    }
}

然而,存在一些问题...我想知道是否有人能帮助我,因为尽管尝试了很多次修复,但我不知道问题在哪里。当我只点击Fanta并购买一个时(因此Fanta库存应该是2),Lipton的库存减少了,而我没有点击它。“Chocolate的库存:和库存数量”也没有打印出来。还有许多其他错误,例如:

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等,在循环之前的每个部分都正确定义了,等等。

以下是我在自动售货机中的每个购买部分使用的代码。基本上,每个项目都是复制粘贴的,只需更改价格和调用的图像:如果需要更多代码,我也可以粘贴更多代码。Lipton和Fanta:

int stock = 3;
private void liptonActionPerformed(java.awt.event.ActionEvent evt) {
    // ... 这里是与Lipton相关的操作 ...
}

int stock2 = 3;
private void fantaActionPerformed(java.awt.event.ActionEvent evt) {
    // ... 这里是与Fanta相关的操作 ...
}

希望这些翻译对您有所帮助。如果您还有其他问题,请随时问我。

英文:

Alright so basically I made a vending machine program. When the user buys an item equal to or greater than its cost, the inventory of the item will deplete by 1. The item van be bought up to three times, until the stock has ran out. At that point, the JButton will dissapear and the item can not be clicked on or bought. To add complexiity, I added a "Check Total Inventory" button. What is supposed to happen when clicked on, is the program prints out smallest to greatest the amount of stock.

For instance:

Inventory of Liton: 0
Inventory of Fanta: 1
Inventory of Pepsi: 2 
Inventory of Gum: 3
Inventory of Seeds:3
Inventory of Chocolate: 3

To do that here is the loop I used:
private void 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&lt;inventory.length-1; i++){
            if(inventory[i]&gt; inventory[i+1]){                    
                temp= inventory[i+1];                    
                inventory [i+1]= inventory[i];                   
                inventory[i]= temp;
                fixed= false;

            }
        }
    }

    for (int i=0; i&lt;inventory.length; i++){
            System.out.println(&quot;The inventory of Lipton is:&quot; + inventory[i]);
           System.out.println(&quot;The inventory of Fanta is:&quot; + inventory[i+2]);
                System.out.println(&quot;The inventory of Pepsi is:&quot; +inventory[i+3]);
                System.out.println(&quot;The inventory of Gum is:&quot; +inventory[i+4]);
                System.out.println(&quot;The inventory of Seeds is:&quot; +inventory[i+5]);
                System.out.println(&quot;The inventory of Choco is:&quot; +inventory[i+6]);   
    }
}                                              

There are a few issues though... I was wondering, if someone could please help me because I do not kow what the problem is, depsite numerous attempts to fix it. When I click only on Fanta and purchase one, (so Fanta inventory should be 2), the inventory for Lipton decreases when I did not click on it. "The inventory of Chocolate: and the number of inventory" does not also print. There are also a lot of other errors such as

    Exception in thread &quot;AWT-EventQueue-0&quot; 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)

etc.

I do not what these errors are telling me. But I have done a few things to try to remedy this. First, I have running the debug feature to which nothing was found. I have double checked each int stock, int stock2, int stock3, etc. is properly defined for each section above the loop, etc.

Here is the code I use for each section of what you can buy in the vending machine. Basically each item is copy and paste, except I change the price, and image being called: I can also paste more code if anymore is needed. Lipton and Fanta:

int stock=3;
private void liptonActionPerformed(java.awt.event.ActionEvent evt) {                                       
final JPanel panel = new JPanel();

DecimalFormat dollarFormat = new DecimalFormat(&quot; $#,##0.00&quot;);

String inputstring;
double input, change;

inputstring= JOptionPane.showInputDialog(&quot;Please enter your payment: $&quot;);
input= Double.parseDouble(inputstring);
change= input-1;   

    if (input&lt;1)
    {
   JOptionPane.showMessageDialog(panel, &quot; You do not have enough money to purchase this item&quot;, &quot;INSUFFICIENT FUNDS&quot;,
        JOptionPane.WARNING_MESSAGE); 
    }

    else if (input&gt;1)
    {
    JOptionPane.showInputDialog(&quot;You have purchased a Lipton Tea. Here is your change:&quot; +(dollarFormat.format(change)));
    stock--;
    
        
    ImageIcon liptonIcon= new ImageIcon(&quot;usethislipton.png&quot;);
	
	Image liptonImage= liptonIcon.getImage();
	Image modifiedliptonImage= liptonImage.getScaledInstance(100,150, java.awt.Image.SCALE_SMOOTH);
	liptonIcon= new ImageIcon (modifiedliptonImage);
	JOptionPane.showMessageDialog(panel, &quot;Lipton Tea&quot;, &quot;Here is your drink! Enjoy!&quot;, JOptionPane.INFORMATION_MESSAGE, liptonIcon);   
    
    }

    else if (input==1)
    {
    JOptionPane.showInputDialog(&quot;You have purchased a Lipton Tea.&quot;);
    stock--;
    
    ImageIcon liptonIcon= new ImageIcon(&quot;usethislipton.png&quot;);
	
	Image liptonImage= liptonIcon.getImage();
	Image modifiedliptonImage= liptonImage.getScaledInstance(100,150, java.awt.Image.SCALE_SMOOTH);
	liptonIcon= new ImageIcon (modifiedliptonImage);
	JOptionPane.showMessageDialog(panel, &quot;Lipton Tea&quot;, &quot;Here is your drink! Enjoy&quot;, JOptionPane.INFORMATION_MESSAGE, liptonIcon);  
    }
        
        if (stock==1)
        {    
           JOptionPane.showMessageDialog(panel, &quot; There is only 1 Lipton Tea remaining! Restock recommnended&quot;, &quot;WARNING&quot;,
                JOptionPane.WARNING_MESSAGE);       
        } 
        else if (stock==0)
        {
           JOptionPane.showMessageDialog(panel, &quot;Sorry, Lipton Tea is out of stock&quot;, &quot;&quot;,
           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(&quot; $#,##0.00&quot;);
    
String inputstring;

double input, change;

inputstring= JOptionPane.showInputDialog(&quot;Please enter your payment: $&quot;);
input= Double.parseDouble(inputstring);
change= (input-1.25);

if (input&lt;1.25)
{
    JOptionPane.showMessageDialog(panel, &quot; You do not have enough money to purchase this item&quot;, &quot;INSUFFICIENT FUNDS&quot;,
    JOptionPane.WARNING_MESSAGE);
}

else if (input&gt;1.25)
{
    JOptionPane.showInputDialog(&quot;You have purchased a Fanta. Here is your change&quot; +(dollarFormat.format(change)));
       stock2--;
       
    ImageIcon fantaIcon= new ImageIcon(&quot;finalfanta2.png&quot;);
 
 Image fantaImage= fantaIcon.getImage();
 Image modifiedfantaImage= fantaImage.getScaledInstance(100,150, java.awt.Image.SCALE_SMOOTH);
 fantaIcon= new ImageIcon (modifiedfantaImage);
 JOptionPane.showMessageDialog(panel, &quot;Fanta&quot;, &quot;Here is your drink! Enjoy&quot;, JOptionPane.INFORMATION_MESSAGE, fantaIcon);  
       
}
else if (input==1.25)
{
    JOptionPane.showInputDialog(&quot;You have purchased a Fanta.&quot;);
       stock2--;
    
   ImageIcon fantaIcon= new ImageIcon(&quot;finalfanta2.png&quot;);
 
 Image fantaImage= fantaIcon.getImage();
 Image modifiedfantaImage= fantaImage.getScaledInstance(100,150, java.awt.Image.SCALE_SMOOTH);
 fantaIcon= new ImageIcon (modifiedfantaImage);
 JOptionPane.showMessageDialog(panel, &quot;Fanta&quot;, &quot;Here is your drink! Enjoy&quot;, JOptionPane.INFORMATION_MESSAGE, fantaIcon);  
            
 }

    if (stock2==1)
        {    
           JOptionPane.showMessageDialog(panel, &quot; There is only 1 Fanta remaining! Restock recommnended&quot;, &quot;WARNING&quot;,
           JOptionPane.WARNING_MESSAGE);             
        }
       else if (stock2==0)
        {
           JOptionPane.showMessageDialog(panel, &quot;Sorry, Fanta is out of stock&quot;, &quot;&quot;,
           JOptionPane.WARNING_MESSAGE);       

           fanta.setVisible(false);
        }


}                                     

答案1

得分: 0

你正在循环遍历整个数组,并获取下一个位置进行比较。当你在数组末尾并跳转到下一个位置时,会出现java.lang.ArrayIndexOutOfBoundsException: 6的错误。

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

你在做相同的错误(检查 i + x)。

你在使用哪个版本的Java?

英文:

You are looping all array and getting the next position for compare, when you are in the end of array and go to the next position you get the java.lang.ArrayIndexOutOfBoundsException: 6

for (int i=0; i&lt;inventory.length-1; i++){
            if(inventory[i]&gt; inventory[i+1]){ //here                   
                temp= inventory[i+1];                    
                inventory [i+1]= inventory[i];                   
                inventory[i]= temp;
                fixed= false;

            }
        }

when you solve this, next step is check the following code:

for (int i=0; i&lt;inventory.length; i++){
            System.out.println(&quot;The inventory of Lipton is:&quot; + inventory[i]);
           System.out.println(&quot;The inventory of Fanta is:&quot; + inventory[i+2]);
                System.out.println(&quot;The inventory of Pepsi is:&quot; +inventory[i+3]);
                System.out.println(&quot;The inventory of Gum is:&quot; +inventory[i+4]);
                System.out.println(&quot;The inventory of Seeds is:&quot; +inventory[i+5]);
                System.out.println(&quot;The inventory of Choco is:&quot; +inventory[i+6]);   
    }

You are doing the same mistake (check that i + x)

Which version of java are you using?

答案2

得分: 0

好的,以下是翻译好的部分:

好的,看一下这段代码:

    for (int i=0; i<inventory.length; i++){
                System.out.println("Lipton的库存是:" + inventory[i]);
               System.out.println("Fanta的库存是:" + inventory[i+2]);
                    System.out.println("Pepsi的库存是:" +inventory[i+3]);
                    System.out.println("Gum的库存是:" +inventory[i+4]);
                    System.out.println("Seeds的库存是:" +inventory[i+5]);
                    System.out.println("Choco的库存是:" +inventory[i+6]);   
        }
变量 'i' 的值将在 0 到 inventory.length-1 之间,包括两端。举个极端的例子,当

    i = inventory.length()-1

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

它首先会打印出

    Lipton的库存是:inventory[inventory.length-1]

这里没有问题。然而,接下来的一行就是你的程序开始出现问题的地方。

'i+2' 将会变成 'inventory.length+1'!显然,这已经超出了 inventory 数组的索引范围。
英文:

Ok, look at this piece of code here:

for (int i=0; i&lt;inventory.length; i++){
            System.out.println(&quot;The inventory of Lipton is:&quot; + inventory[i]);
           System.out.println(&quot;The inventory of Fanta is:&quot; + inventory[i+2]);
                System.out.println(&quot;The inventory of Pepsi is:&quot; +inventory[i+3]);
                System.out.println(&quot;The inventory of Gum is:&quot; +inventory[i+4]);
                System.out.println(&quot;The inventory of Seeds is:&quot; +inventory[i+5]);
                System.out.println(&quot;The inventory of Choco is:&quot; +inventory[i+6]);   
    }

The variable 'i' will go from 0 to inventory.length-1, inclusive. For instance, take the most extreme case where

i = inventory.length()-1

Now, lets see what your program will do.

It first starts by printing

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

Nothing wrong here. However, the next line is where your programs starts to fail.

i+2 would be inventory.length+1! Obviously, this is out of the array index of inventory.

huangapple
  • 本文由 发表于 2020年4月9日 01:10:55
  • 转载请务必保留本文链接:https://java.coder-hub.com/61106210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定