用java(intellij)进行周期表搜索
其目标是使元素周期表搜索引擎。向用户提问以缩小结果范围,或者询问用户是否知道要输入的元素的名称。一旦输入或找到元素,它将提供有关元素的信息。我只有一些细节和一些元素来让代码工作,更多的元素和细节将被添加,比如结构和原子量 每当发现结果时,它不会给出信息,只会给出一个空白屏幕。我想把它作为toString放在课堂上,然后在我的主调底部调用它就足够了,但我不知道我缺少了什么。这似乎是一个简单的系统输出命令,但也不起作用 非常感谢您的帮助用java(intellij)进行周期表搜索,java,swing,if-statement,arraylist,joptionpane,Java,Swing,If Statement,Arraylist,Joptionpane,其目标是使元素周期表搜索引擎。向用户提问以缩小结果范围,或者询问用户是否知道要输入的元素的名称。一旦输入或找到元素,它将提供有关元素的信息。我只有一些细节和一些元素来让代码工作,更多的元素和细节将被添加,比如结构和原子量 每当发现结果时,它不会给出信息,只会给出一个空白屏幕。我想把它作为toString放在课堂上,然后在我的主调底部调用它就足够了,但我不知道我缺少了什么。这似乎是一个简单的系统输出命令,但也不起作用 非常感谢您的帮助 import java.util.ArrayList; imp
import java.util.ArrayList;
import javax.swing.*;
public class Main {
public static void main(String args[]) {
ArrayList<Element> e = new ArrayList<Element>();
String choice, element = "", searchBy, symbol, chemGroupBlock, list = "", str = "";
e.add(new Element("Hydrogen", "Nonmetal", "H", 1));
e.add(new Element("Helium", "Nobel Gas", "He", 2));
e.add(new Element("Lithium", "Alkali Metal", "Li", 3));
choice = JOptionPane.showInputDialog(null,
"Do you know the name of the element you're looking for?"
+ "(Yes/No)", "Welcome to the Chem Table!", 3);
if (choice.equalsIgnoreCase("yes")) {
element = JOptionPane.showInputDialog(null,
"Enter the name of the Element.. ",
"Welcome to the Chem Table!", JOptionPane.PLAIN_MESSAGE);
}else if (choice.equalsIgnoreCase("no")) {
searchBy = JOptionPane.showInputDialog(null,
"Search by: 'chemical group block' or 'symbol'?", "Welcome to the Chem Table", 3);
if (searchBy.equalsIgnoreCase("chemGroupBlock")) {
chemGroupBlock = JOptionPane.showInputDialog(null,
"Enter Chemical Group Block (Nonmetal/Nobel Gases/Alkalies)",
"Welcome to The Chem Table", JOptionPane.PLAIN_MESSAGE);
for (int i = 0; i < e.size(); i++){
if (e.get(i).getchemGroupBlock().equals(chemGroupBlock)){
list = list + e.get(i).getName() + "\n";
}
}
element = JOptionPane.showInputDialog(null, list,
JOptionPane.PLAIN_MESSAGE);
} else if (searchBy.equalsIgnoreCase("symbol")) {
symbol = JOptionPane.showInputDialog(null,
"Enter the element symbol:(He/NaCl/Xe)",
"Welcome to the Chem Table",
JOptionPane.PLAIN_MESSAGE);
for (int i = 0; i < e.size(); i++) {
if (e.get(i).getSymbol().equals(symbol)) {
list = list + e.get(i).getName() + "\n";
}
}
element = JOptionPane.showInputDialog(null, list,
JOptionPane.PLAIN_MESSAGE);
}
}
for (int i = 0; i < e.size(); i++) {
if (e.get(i).getName().equals(element)) {
str = e.get(i).toString();
}
}
JOptionPane.showMessageDialog(null, str, "Element Results", 1);
}
}
我认为问题在于这种情况:
if(e.get(i).getName().equals(element))
是否输入了元素的确切区分大小写的名称?例如,“氢”将不会被发现,但“氢”将工作良好
您应将此条件更改为:
if(e.get(i).getName().equalsIgnoreCase(元素))
代码的其他部分也存在其他类似的问题。我建议你对你的逻辑做一个全面的回顾。请密切注意何时调用
equals
以及它是否应为equalsIgnoreCase
当用户输入元素名称时,只要元素名称正确,搜索就会成功。如上所述,搜索现在区分大小写,因此您可以使用.equalsIgnoreCase
对于按符号或化学组搜索,有冗余输入对话框,在输入字段中打印-1(它是JOptionPane.PLAIN_MESSAGE
)的值:
最好使用“选项对话框”,用户只需单击按钮即可选择选项,而无需键入
接下来,当您按符号搜索时,您将提供数据中不可用的选项(例如,NaCl
和Xe
)
因此,要解决这些问题,您可能需要按如下方式更新代码:
int result = JOptionPane.showConfirmDialog(null,
"Do you know the name of the element you're looking for?",
"Welcome to the Chem Table!", JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
element = JOptionPane.showInputDialog(
null, "Enter the name of the Element.. ",
"Welcome to the Chem Table!", JOptionPane.PLAIN_MESSAGE);
} else if (result == JOptionPane.NO_OPTION) {
String[] byOptions = {"chemical group block", "symbol"};
result = JOptionPane.showOptionDialog(
null,
"Search by: 'chemical group block' or 'symbol'?",
"Welcome to the Chem Table",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, byOptions, byOptions[0]);
if (result == 0) {
String[] byGroup = {"Nonmetal", "Nobel Gases", "Alkalies"};
result = JOptionPane.showOptionDialog(
null,
"Select Chemical Group",
"Welcome to The Chem Table",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, byGroup, byGroup[0]);
for (Element el : e) {
if (el.getchemGroupBlock().equalsIgnoreCase(byGroup[result])) {
element = el.getName();
break;
}
}
} else { // search by symbol
symbol = JOptionPane.showInputDialog(
null, "Enter the element symbol:(H/He/Li)",
"Welcome to the Chem Table",
JOptionPane.PLAIN_MESSAGE);
for (Element el : e) {
if (el.getSymbol().equalsIgnoreCase(symbol)) {
element = el.getName();
break;
}
}
if (element.isEmpty()) {
JOptionPane.showMessageDialog(
null, "Symbol" + symbol + " not found!", "Not found!", JOptionPane.WARNING_MESSAGE);
return;
}
}
}
element = JOptionPane.showInputDialog(null, list, JOptionPane.PLAIN_MESSAGE);
int result = JOptionPane.showConfirmDialog(null,
"Do you know the name of the element you're looking for?",
"Welcome to the Chem Table!", JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
element = JOptionPane.showInputDialog(
null, "Enter the name of the Element.. ",
"Welcome to the Chem Table!", JOptionPane.PLAIN_MESSAGE);
} else if (result == JOptionPane.NO_OPTION) {
String[] byOptions = {"chemical group block", "symbol"};
result = JOptionPane.showOptionDialog(
null,
"Search by: 'chemical group block' or 'symbol'?",
"Welcome to the Chem Table",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, byOptions, byOptions[0]);
if (result == 0) {
String[] byGroup = {"Nonmetal", "Nobel Gases", "Alkalies"};
result = JOptionPane.showOptionDialog(
null,
"Select Chemical Group",
"Welcome to The Chem Table",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, byGroup, byGroup[0]);
for (Element el : e) {
if (el.getchemGroupBlock().equalsIgnoreCase(byGroup[result])) {
element = el.getName();
break;
}
}
} else { // search by symbol
symbol = JOptionPane.showInputDialog(
null, "Enter the element symbol:(H/He/Li)",
"Welcome to the Chem Table",
JOptionPane.PLAIN_MESSAGE);
for (Element el : e) {
if (el.getSymbol().equalsIgnoreCase(symbol)) {
element = el.getName();
break;
}
}
if (element.isEmpty()) {
JOptionPane.showMessageDialog(
null, "Symbol" + symbol + " not found!", "Not found!", JOptionPane.WARNING_MESSAGE);
return;
}
}
}