Java 为什么我的数据在编译时会混淆?

Java 为什么我的数据在编译时会混淆?,java,arrays,user-interface,Java,Arrays,User Interface,有几件事我想得到一些帮助。首先也是最重要的是,当我编译时,我的产品编号和价格会混淆。为什么?其次,为什么产品类型总是返回null?我也想合并所有的消息框,但每次尝试都失败了。如果有人能把我引向正确的方向,我将不胜感激。这是我的密码: MAIN package inventory4; import java.util.Scanner; import java.util.Arrays; //Needed to include data for arrays import javax.swing.JO

有几件事我想得到一些帮助。首先也是最重要的是,当我编译时,我的产品编号和价格会混淆。为什么?其次,为什么产品类型总是返回null?我也想合并所有的消息框,但每次尝试都失败了。如果有人能把我引向正确的方向,我将不胜感激。这是我的密码:

MAIN

package inventory4;
import java.util.Scanner;
import java.util.Arrays; //Needed to include data for arrays
import javax.swing.JOptionPane; //JOptionPane import tool

public class RunApp
{
 public static void main(String[] args)
   {

  Scanner input = new Scanner(System.in);

  ItemDetails theItem = new ItemDetails();

  int number;
  String Name = "";
  String Type = "";

  String sNumber = JOptionPane.showInputDialog(null,"How many items are to be put into inventory count?:  ");
  number = Integer.parseInt(sNumber);

  ItemDetails[] inv = new ItemDetails[number];


  for (int count = 0; count < inv.length; ++count)
  {
     Name = JOptionPane.showInputDialog(null,"What is item " + (count + 1) + "'s name?");

     theItem.setName(Name);

     Type = JOptionPane.showInputDialog(null,"Enter " + Name + "'s product type");

     String spNumber = JOptionPane.showInputDialog(null,"Enter " + Name + "'s product number");
     double pNumber = Double.parseDouble(spNumber);
     theItem.setpNumber(pNumber);

     String sUnits = JOptionPane.showInputDialog(null,"How many " + Name + "s are there in inventory?");

     double Units = Double.parseDouble(sUnits);
     theItem.setUnits(Units);

     String sPrice = JOptionPane.showInputDialog(null,Name + "'s cost");
     double Price = Double.parseDouble(sPrice);
     theItem.setPrice(Price);


      inv[count] = new ItemDetails(Name, Price, Units, pNumber);

  }




  for (int i = 0; i < inv.length; ++i)
  {

     JOptionPane.showMessageDialog(null, "Product Name: " + inv[i].getName());
     //Why can't I use this instead of having multiple boxes?:
     //JOptionPane.showMessageDialog(null, "Product Name: \nProduct Type: \nProduct Number: \nUnits in Stock: \nPrice Per Unit: " + inv[i].getName() +  inv[i].getUniqueType() + inv[i].getpNumber() + inv[i].getUnits(), + inv[i].getPrice());

     JOptionPane.showMessageDialog(null, "Product Type: " +  inv[i].getUniqueType());

     JOptionPane.showMessageDialog(null, "Product Number: " + inv[i].getpNumber());

     JOptionPane.showMessageDialog(null, "Amount of Units in Stock: " + inv[i].getUnits());

     JOptionPane.showMessageDialog(null, "Price per Unit: " + inv[i].getPrice());

 JOptionPane.showMessageDialog(null, String.format("Total cost for %s in stock: $%.2f", inv[i].getName(), inv[i].calculateTotalPrice()));

     JOptionPane.showMessageDialog(null,String.format("Restocking fee for %s is $%.2f", inv[i].getName(), inv[i].calculateRestock()));

     String combinedData = inv[i].toString();

     if(i == (inv.length -1)){

        String lastItem = String.format("\nTotal Cost for all items entered: $%.2f\n", Items.getCombinedCost(inv));

        combinedData = combinedData + lastItem; //combine total value to the end of the last object output
        JOptionPane.showMessageDialog(null, combinedData);  //Show Message

     }else{
        JOptionPane.showMessageDialog(null, combinedData); //Show Message
     }


    } //end for


  } //end main


} //end class
应该是:

//setter
public void setUniqueType(String type) {
    UniqueType = type;
}
inv[count] = new ItemDetails(Name, pNumber, Units,Price );//look at the order 
inv[count].setUniqueType(Type);//you have not set it.

应该是:

//setter
public void setUniqueType(String type) {
    UniqueType = type;
}
inv[count] = new ItemDetails(Name, pNumber, Units,Price );//look at the order 
inv[count].setUniqueType(Type);//you have not set it.
应该是:

//setter
public void setUniqueType(String type) {
    UniqueType = type;
}
inv[count] = new ItemDetails(Name, pNumber, Units,Price );//look at the order 
inv[count].setUniqueType(Type);//you have not set it.

应该是:

//setter
public void setUniqueType(String type) {
    UniqueType = type;
}
inv[count] = new ItemDetails(Name, pNumber, Units,Price );//look at the order 
inv[count].setUniqueType(Type);//you have not set it.
  • 首先也是最重要的一点是,当我编译时,我的产品编号和价格混淆了。为什么?

    您正在创建一个新的
    ItemDetails
    对象,调用

    new ItemDetails(Name, Price, Units, pNumber);
    
    但是
    ItemDetails
    的构造函数是

    ItemDetails(String productName, double productNumber, double unitsInStock, double unitPrice)
    
    i、 例如,它首先考虑产品编号,最后考虑价格,而不是相反

  • 其次,为什么产品类型总是返回null?

    你从来没有真正设置你的类型,你的set和get方法都做同样的事情!setter正在返回一个值应该是一个警告

    public String setUniqueType()
    {
          return UniqueType;
    }
    
    public String getUniqueType()
    {
          return UniqueType;
    }
    
    这就是它应该是的

    public void setUniqueType(String type)
    {
          this.UniqueType = type;
    }
    
  • 首先也是最重要的一点是,当我编译时,我的产品编号和价格混淆了。为什么?

    您正在创建一个新的
    ItemDetails
    对象,调用

    new ItemDetails(Name, Price, Units, pNumber);
    
    但是
    ItemDetails
    的构造函数是

    ItemDetails(String productName, double productNumber, double unitsInStock, double unitPrice)
    
    i、 例如,它首先考虑产品编号,最后考虑价格,而不是相反

  • 其次,为什么产品类型总是返回null?

    你从来没有真正设置你的类型,你的set和get方法都做同样的事情!setter正在返回一个值应该是一个警告

    public String setUniqueType()
    {
          return UniqueType;
    }
    
    public String getUniqueType()
    {
          return UniqueType;
    }
    
    这就是它应该是的

    public void setUniqueType(String type)
    {
          this.UniqueType = type;
    }
    
  • 我还想合并所有的消息框

    例如,请参见

    我还想合并所有的消息框


    例如,请参见。

    另请参见。另请参见。多亏了您,我解决了第一个问题,但我仍然坚持使用唯一类型。详细说明?@g3n3rallyl0st既然你已将答案标记为已接受,我想你已经明白了。上面John的回答也解释了这一点。多亏了你,我解决了第一个问题,但我仍然坚持使用独特的类型。详细说明?@g3n3rallyl0st既然你已将答案标记为已接受,我想你已经明白了。上面约翰的回答也解释了这一点。