gpt4 book ai didi

java - 如何将 newItem 设置为 JOptionPane?

转载 作者:行者123 更新时间:2023-11-29 12:11:53 24 4
gpt4 key购买 nike

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() 从用户获取三个输入

然后使用输入的值构造一个新的 InventoryItem 对象并返回给调用者和对该新 InventoryItem 的对象引用。

大约 10-12 行。如何设置 newItem 以返回我的 JOptionPane 输入屏幕?

最佳答案

如果没有 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

关于java - 如何将 newItem 设置为 JOptionPane?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33224125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com