我正在使用 JOptionPane,我不确定为什么当我尝试输入第一个“?”的项目 ID 时它不起作用。它在我的代码中询问
Im using JOptionPane and I'm not sure why its not working when I try to enter the Item Id for the first "?" it asks in my code
一家公司生产 10 件商品。编写 java 程序在数组中存储 10 件商品及其价格。如果客户订购了一件商品,您的程序会检查它是否有效,如果有效打印价格
import javax.swing.JOptionPane;
public class ParallelArray3 {
public static void main(String[] args){
final int Num =10;
int [] Item= {101, 110, 210, 220, 300, 310, 316, 355, 405, 410};
double [] Price= {0.29, 1.23, 3.50, 0.89, 6.79, 3.12, 4.32, 3.6, 8.3, 5.4};
String ItemId = null;
while ((ItemId = JOptionPane.showInputDialog(null, "Please enter your Item ID number: ")) != null)
{
boolean correct = false;
for (int x = 0; x < Item.length; ++x)
{
if(ItemId.equals(Item[x]))
{
JOptionPane.showInputDialog(null, "Your Item is: " + Item[x] + "\n" + "the price is: " + Price[x], JOptionPane.INFORMATION_MESSAGE);
correct = true;
break;
}
}
if(! correct)
{
JOptionPane.showMessageDialog(null, "item ID not found, try again.", "Not found", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
预期结果将显示项目编号和价格
实际结果是说每一项#都是无效的
因为你比较的是String和Int
if(ItemId.equals(Item[x]))
{
应该改为
if(Integer.parseInt(ItemId)==(Item[x]))
{
最终代码变为
import javax.swing.JOptionPane;
public class ParallelArray3 {
public static void main(String[] args) {
final int Num =10;
int [] Item= {101, 110, 210, 220, 300, 310, 316, 355, 405, 410};
double [] Price= {0.29, 1.23, 3.50, 0.89, 6.79, 3.12, 4.32, 3.6, 8.3, 5.4};
String ItemId = null;
while ((ItemId = JOptionPane.showInputDialog(null, "Please enter your Item ID number: ")) != null)
{
System.out.println(ItemId);
boolean correct = false;
for (int x = 0; x < Item.length; x++)
{
if(Integer.parseInt(ItemId)==(Item[x]))
{
JOptionPane.showInputDialog(null, "Your Item is: " + Item[x] + "\n" + "the price is: " + Price[x], JOptionPane.INFORMATION_MESSAGE);
correct = true;
break;
}
}
if(!correct)
{
JOptionPane.showMessageDialog(null, "item ID not found, try again.", "Not found", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
一家公司生产 10 件商品。编写 java 程序在数组中存储 10 件商品及其价格。如果客户订购了一件商品,您的程序会检查它是否有效,如果有效打印价格
import javax.swing.JOptionPane;
public class ParallelArray3 {
public static void main(String[] args){
final int Num =10;
int [] Item= {101, 110, 210, 220, 300, 310, 316, 355, 405, 410};
double [] Price= {0.29, 1.23, 3.50, 0.89, 6.79, 3.12, 4.32, 3.6, 8.3, 5.4};
String ItemId = null;
while ((ItemId = JOptionPane.showInputDialog(null, "Please enter your Item ID number: ")) != null)
{
boolean correct = false;
for (int x = 0; x < Item.length; ++x)
{
if(ItemId.equals(Item[x]))
{
JOptionPane.showInputDialog(null, "Your Item is: " + Item[x] + "\n" + "the price is: " + Price[x], JOptionPane.INFORMATION_MESSAGE);
correct = true;
break;
}
}
if(! correct)
{
JOptionPane.showMessageDialog(null, "item ID not found, try again.", "Not found", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
预期结果将显示项目编号和价格
实际结果是说每一项#都是无效的
因为你比较的是String和Int
if(ItemId.equals(Item[x]))
{
应该改为
if(Integer.parseInt(ItemId)==(Item[x]))
{
最终代码变为
import javax.swing.JOptionPane;
public class ParallelArray3 {
public static void main(String[] args) {
final int Num =10;
int [] Item= {101, 110, 210, 220, 300, 310, 316, 355, 405, 410};
double [] Price= {0.29, 1.23, 3.50, 0.89, 6.79, 3.12, 4.32, 3.6, 8.3, 5.4};
String ItemId = null;
while ((ItemId = JOptionPane.showInputDialog(null, "Please enter your Item ID number: ")) != null)
{
System.out.println(ItemId);
boolean correct = false;
for (int x = 0; x < Item.length; x++)
{
if(Integer.parseInt(ItemId)==(Item[x]))
{
JOptionPane.showInputDialog(null, "Your Item is: " + Item[x] + "\n" + "the price is: " + Price[x], JOptionPane.INFORMATION_MESSAGE);
correct = true;
break;
}
}
if(!correct)
{
JOptionPane.showMessageDialog(null, "item ID not found, try again.", "Not found", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}