我正在努力弄清楚如何在JOptionPane中顯示我的數組內容。該陣列包括一個項目,價格,數量和優先級。例如,我希望它看起來像這樣: 「蘋果2.99 2 1」 「穀物3.99 3 2」 我目前有陣列輸出到控制檯,因爲我無法正確顯示陣列。以下是我現在所擁有的內容:我非常感謝所有幫助!在JOptionPane中顯示數組
import java.text.NumberFormat;
import javax.swing.JOptionPane;
public class ShopList {
public static void main(String[] args)
{
String enterName = JOptionPane.showInputDialog(null, "Username: ");
for (int i = 0; i < 2; i++){
index = (int) (25 * Math.random());
String[] options = {"Apples", "Applesauce", "Cereal", "Exit"};
String input = (String)JOptionPane.showInputDialog(null,
"Select an Item",
"Welcome " + enterName + "!",JOptionPane.QUESTION_MESSAGE,null,options,"Apples");
String itemPrice = JOptionPane.showInputDialog("Enter Price");
double itemp = Double.parseDouble(itemPrice);
String[] itemQuantity = {"1", "2", "3", "4", "5"};
String itemq = (String)JOptionPane.showInputDialog(null,"Enter Quantity", "Welcome", JOptionPane.QUESTION_MESSAGE, null, itemQuantity, "1");
String itemsPriority = JOptionPane.showInputDialog("Enter Priority");
int itempry = Integer.parseInt(itemsPriority);
ShoppingList shoppingList = new ShoppingList(input,itemp, itemq, itempry);
shoppingList.show();
}
}
class ShoppingList
{
String itemNames;
double itemPrice;
String itemQuantity;
int itemsPriority;
public ShoppingList()
{
}
public ShoppingList (String name, double price, String quantity, int priority)
{
itemNames = name;
itemPrice = price;
itemQuantity = quantity;
itemsPriority = priority;
}
public void setitemNames(String name)
{
itemNames = name;
}
public String getitemNames()
{
return itemNames;
}
public void setitemPrice(double price)
{
itemPrice = price;
}
public double getitemPrice()
{
return itemPrice;
}
/*public void setitemQuantity(int quantity)
{
itemQuantity = quantity;
}
public int getitemQuantity()
{
return itemQuantity;
}*/
public void setitemsPriority(int priority)
{
itemsPriority = priority;
}
public int getitemsPriority()
{
return itemsPriority;
}
public void show()
{
System.out.println(itemNames);
System.out.println(itemPrice);
System.out.println(itemQuantity);
System.out.println(itemsPriority);
}
}
你想如何顯示它? –
你想要顯示的數組在哪裏? –
我想要顯示shoppingList數組,如果這是有道理的? – user1785556