[英]How to display JOptionPane through user input
目前,我一直试图找出为什么我的Array值没有显示在对话框中的原因。 我正在做一个作业,其详细信息是:
“编写一个名为TallestBuildingLookup的程序,该程序包含10个TallestBuilding对象的数组,并相应地使用上面提供的数据进行填充。然后使用对话框接受建筑物名称并显示建筑物的位置,高度和楼层。如果找不到匹配项,显示一条错误消息,其中包含无效的名称,并允许用户搜索新的建筑物名称。”
我的主要问题是从我的toString()方法获取要显示的数组值,并在我在数组中未收到名称时处理异常。 具体来说,使对话框循环以重新输入名称值并重新检查数组。 任何帮助将不胜感激。
import javax.swing.*;
public class TallestBuildingLookup {
static class TallestBuilding{
private String name;
private String city;
private int height;
private int stories;
public TallestBuilding(String name, String city, int height, int stories) {
this.name = name;
this.city = city;
this.height = height;
this.stories = stories;
}
public String getName(){
return this.name;
}
public String toString(){
return this.name + " of " + this.city + ", "+ this.stories + "stories/" + this.height + " feet high." ;
}
}
public static void main(String[] args){
TallestBuilding[] tallestbuilding = new TallestBuilding[10];
tallestbuilding[0] = new TallestBuilding("One World Trade Center", "New York", 1776, 104);
tallestbuilding[1] = new TallestBuilding("Willis Tower", "Chicago", 1451, 108);
tallestbuilding[2] = new TallestBuilding("Empire State", "New York", 1250, 102);
tallestbuilding[3] = new TallestBuilding("Bank of America Tower", "New York", 1200, 55);
tallestbuilding[4] = new TallestBuilding("Aon Center", "Chicago", 1136, 83 );
tallestbuilding[5] = new TallestBuilding("John Hancock Center", "Chicago", 1127, 100);
tallestbuilding[6] = new TallestBuilding("Wells Fargo Plaza", "Houston", 992,71 );
tallestbuilding[7] = new TallestBuilding("Comcast Center", "Philidelphia", 974, 57 );
tallestbuilding[8] = new TallestBuilding("Columbia Center", "Seattle", 967, 76);
tallestbuilding[9] = new TallestBuilding("Key Tower", "Clevland", 947, 57);
String entry = JOptionPane.showInputDialog("Enter a builing name");
String name = (String) entry;
System.out.println(name);
for (int i=0; i<10; i++){
if(name == tallestbuilding[i].getName() ){
JOptionPane.showInputDialog(null, tallestbuilding[i] );
}
else{
JOptionPane.showInputDialog("Sorry - no "+ name + " was found.");
}
}
}
}
尝试这个:
TallestBuilding tallestBuilding = null;
for (int i=0; i<10; i++){
if(name.equals(tallestbuilding[i].getName())){
tallestBuilding = tallestbuilding[i];
break;
}
}
if(tallestBuilding == null) {
JOptionPane.showInputDialog("Sorry - no "+ name + " was found.");
} else {
JOptionPane.showMessageDialog(null, tallestBuilding);
}
具体来说,使对话框循环以重新输入名称值并重新检查数组。
好吧,您需要在某种类型的循环内请求输入,而这是您在代码中没有做的事情。 循环主要有两种:一种是for循环 ,当您事先知道要循环多少次(在这里不知道)时使用;一种是while循环或do-while循环 。事先不知道。 我建议您使用do-while循环,因为您希望至少从用户那里获得一次输入,这意味着该循环必须至少运行一次(如果没有更多的话),然后继续循环直到输入有效为止。 我会使用布尔值,例如boolean inputValid = false;
为了这。
另一个问题:您的for循环已损坏。 您会在循环内对匹配与不匹配做出反应,这是不正确的,因为如果这样做,您将为用户提供针对每个不匹配的错误对话框,这不是您想要的。 相反,您要检查循环中的匹配项,如果找到匹配项,则设置一个布尔值,也许使用上面提到的那个,然后在循环完成后,如果找不到匹配项,则显示错误消息,然后重复执行正如我所提到的,做循环。
我的主要问题是从我的toString()方法获取要显示的数组值,并在我在数组中未收到名称时处理异常。
如果您需要在此问题上的特定帮助,则必须告诉我们更多有关所显示数据出问题的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.