Java索引名 String nameOfTools=”“; 对于(int index=0;index
我希望对话框显示“您需要(工具名称)的数量”,而不是索引编号如何执行此操作?然后您需要创建一个Java索引名 String nameOfTools=”“; 对于(int index=0;index,java,Java,我希望对话框显示“您需要(工具名称)的数量”,而不是索引编号如何执行此操作?然后您需要创建一个ArrayList来存储工具名称,如下所示: String nameOfTools = ""; for( int index = 0; index < userToolType; index++ ) { nameOfTools = JOptionPane.showInputDialog( "Please enter name of tool " + ( index + 1 )
ArrayList
来存储工具名称,如下所示:
String nameOfTools = "";
for( int index = 0; index < userToolType; index++ )
{
nameOfTools = JOptionPane.showInputDialog( "Please enter name of tool " + ( index + 1 ) );
//check blank
if ( nameOfTools.equalsIgnoreCase("") )
{
JOptionPane.showMessageDialog(null, "You entered blank");
System.exit( 0 );
}
}
//Ask the number to buy for each tool
String numberOfTools;
double toolsNumber = 0;
String amountOfTools;
double moneyForTools;
double totalMoneyToPay = 0;
for ( int index = 0; index < userToolType; index++ )
{
numberOfTools = JOptionPane.showInputDialog( "How many you need to buy " + ( index + 1 ) );
toolsNumber = Double.parseDouble( numberOfTools );
ArrayList toolsNames=new ArrayList();//获取所有名称
对于(int index=0;index
我猜您需要将工具存储在一个数组中,因为此时您似乎正在覆盖nameOfTools
。创建一个名称数组并使用array[index]
@주성호 请考虑接受一个答案,你今天已经问了3个问题,还没有接受任何一个问题!请学习java,例如阅读。
ArrayList<String> toolsNames = new ArrayList<String>(); // to take all names
for( int index = 0; index < userToolType; index++ ){
String input = JOptionPane.showInputDialog( "Please enter name of tool " + ( index + 1 ) );
if ( input.equals("") ){ //check blank
JOptionPane.showMessageDialog(null, "You entered blank");
System.exit( 0 );
}
toolsNames.add(input); // if it's not blank
}
//Ask the number to buy for each tool
String numberOfTools;
double toolsNumber = 0;
String amountOfTools;
double moneyForTools;
double totalMoneyToPay = 0;
for ( int index = 0; index < userToolType; index++ ){
numberOfTools = JOptionPane.showInputDialog( "How many you need to buy from " + toolsNames.get(index));
toolsNumber = Double.parseDouble( numberOfTools );
}