2015-10-19 42 views
1
public static InventoryItem addNewItem(){ 

    InventoryItem newItem; 
    JOptionPane.showInputDialog(null," Enter new product name.", 
        " by Marquis Watkins", JOptionPane.QUESTION_MESSAGE) 
    JOptionPane.showInputDialog(null," Enter product price." , 
        " by Marquis Watkins", JOptionPane.QUESTION_MESSAGE); 
    JOptionPane.showInputDialog(null,"Enter quantity of product.", 
        " by Marquis Watkins", JOptionPane.QUESTION_MESSAGE); 

    return newItem; 
} 

此方法使用JOptionPane.showInputDialog()如何将newItem设置为JOptionPane?

用户得到三个输入,然后使用这些值输入到构造新的对象InventoryItem并返回到调用者和对象引用到该新InventoryItem。

约10-12 lines.How我应该设置newItem返回我的JOptionPane输入屏幕?

+2

'showInputDialog()'返回一个'String',这就是用户输入的内容。目前您正在丢弃这些值。你也不是在任何地方构建'newItem'。 – Cinnam

+0

所以我不应该使用showInputDialog()? –

+1

你可以,问题是你没有存储它返回的值。 – Cinnam

回答

1

没有InventoryItem的代码我们无法知道,但是像这样的东西会让你走上正确的轨道。作为由@Cinnam注释中,你需要存储的返回值:

public static InventoryItem addNewItem() { 

    String name = JOptionPane.showInputDialog(null," Enter new product name."," by Marquis Watkins", JOptionPane.QUESTION_MESSAGE); 
    String price = JOptionPane.showInputDialog(null," Enter product price." ," by Marquis Watkins", JOptionPane.QUESTION_MESSAGE); 
    String quantity = JOptionPane.showInputDialog(null,"Enter quantity of product."," by Marquis Watkins", JOptionPane.QUESTION_MESSAGE); 

    return new InventoryItem(name, price, quantity); 
} 

在这里,我认为你可以构建从3串的InventoryItem

+0

谢谢我为InventoryItem制作了这个类,这些是我使用的变量 –

+0

@JoeSmith如果这回答了您的问题,请考虑upvoting和/或接受答案(绿色复选标记)。 – user1803551

+0

不知道,因为它没有运行我的预期 –