如何通过用户输入显示 JOptionPane
How to display JOptionPane through user input
我目前一直在努力弄清楚为什么我的数组值没有显示在对话框中。我正在做一个作业,其详细信息是:
"Write a program named TallestBuildingLookupthat contains an array of 10 TallestBuildingobjects and fill in it with the data given above accordingly. Then use dialog boxes to accept a building name and display the building’s location, height, and stories. If a match is not found, display an error message that includes the invalid name and allow the user to search for a new building name."
我的主要问题是让我的数组值从我的 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);
}
- 不要使用“==”来比较字符串,因为它比较的是一个引用,而不是它自身的值。检查 Java String.equals versus ==
- 使用 MessageDialog 向用户显示值,而不是 InputDialog
- 你应该在显示用户
后打破 for 循环
- 您应该保存在临时引用中找到的对象,以便之后显示它。
Specifically, getting the dialog box to loop to re-enter a name value and recheck the array.
好吧,您需要在某种类型的循环 中请求输入,而您在代码中没有这样做。循环有两种主要形式——for 循环,当你事先知道你想循环多少次时使用(这里你不知道),——和 while loop 或者 do-while loop 当你事先不知道的时候。我建议您使用 do-while 循环,因为您希望至少从用户那里获得一次输入,这意味着循环必须至少 运行 一次,如果不是更多的话,然后一直循环直到输入有效。我会使用一个布尔值,比如说 boolean inputValid = false;
。
其他问题:您的 for 循环已损坏。你在 循环中对匹配与不匹配做出反应,这是不正确的,因为如果你这样做,你将给出为每个不匹配使用错误对话框,这不是您想要的。相反,您想检查循环内的匹配项,如果找到匹配项则设置一个布尔值,也许使用我上面提到的那个,然后在循环完成后,如果没有找到匹配项则显示错误消息,然后重复正如我提到的,do-while 循环。
My main issue is getting my array values to display from my toString() method, and handling the exception when I don't receive a name in the array.
如果您在这个问题上需要具体帮助,您将不得不告诉我们更多关于您显示的数据有什么问题。
我目前一直在努力弄清楚为什么我的数组值没有显示在对话框中。我正在做一个作业,其详细信息是:
"Write a program named TallestBuildingLookupthat contains an array of 10 TallestBuildingobjects and fill in it with the data given above accordingly. Then use dialog boxes to accept a building name and display the building’s location, height, and stories. If a match is not found, display an error message that includes the invalid name and allow the user to search for a new building name."
我的主要问题是让我的数组值从我的 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);
}
- 不要使用“==”来比较字符串,因为它比较的是一个引用,而不是它自身的值。检查 Java String.equals versus ==
- 使用 MessageDialog 向用户显示值,而不是 InputDialog
- 你应该在显示用户 后打破 for 循环
- 您应该保存在临时引用中找到的对象,以便之后显示它。
Specifically, getting the dialog box to loop to re-enter a name value and recheck the array.
好吧,您需要在某种类型的循环 中请求输入,而您在代码中没有这样做。循环有两种主要形式——for 循环,当你事先知道你想循环多少次时使用(这里你不知道),——和 while loop 或者 do-while loop 当你事先不知道的时候。我建议您使用 do-while 循环,因为您希望至少从用户那里获得一次输入,这意味着循环必须至少 运行 一次,如果不是更多的话,然后一直循环直到输入有效。我会使用一个布尔值,比如说 boolean inputValid = false;
。
其他问题:您的 for 循环已损坏。你在 循环中对匹配与不匹配做出反应,这是不正确的,因为如果你这样做,你将给出为每个不匹配使用错误对话框,这不是您想要的。相反,您想检查循环内的匹配项,如果找到匹配项则设置一个布尔值,也许使用我上面提到的那个,然后在循环完成后,如果没有找到匹配项则显示错误消息,然后重复正如我提到的,do-while 循环。
My main issue is getting my array values to display from my toString() method, and handling the exception when I don't receive a name in the array.
如果您在这个问题上需要具体帮助,您将不得不告诉我们更多关于您显示的数据有什么问题。