在Java(intellij)中进行元素周期表搜索

问题描述 投票:-1回答:2

目标是制作元素周期表搜索引擎。询问用户问题以缩小结果范围,或者用户是否知道刚输入的元素的名称。一旦输入或找到了元素,它将提供有关该元素的信息。我只有几个细节和几个元素来使代码正常工作,还会添加更多元素和细节,例如结构和原子量。

无论何时找到结果,它都不会提供信息,而只会显示空白屏幕。我以为将它放在toString这个类中,然后在我的主体底部调用它就足够了,但是我无法弄清丢失的内容。似乎是一个简单的系统输出打印命令,但这也不起作用。

非常感谢您的帮助。

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);


 }
}

这里是元素的类

public class Element {
private String name;
private String chemGroupBlock;
private String symbol;
private int atomicNumber;



public Element(String name, String chemGroupBlock, String symbol, int atomicNumber) {

    this.name = name;
    this.chemGroupBlock = chemGroupBlock;
    this.symbol = symbol;
    this.atomicNumber = atomicNumber;
}

public String getName(){
    return name;
}

public String getchemGroupBlock(){
    return chemGroupBlock;
}

public String getSymbol(){

    return symbol;
}

public int getatomicNumber(){
    return atomicNumber;
}

public String toString() {
    return "Element: " + name + "\nChemical Group Block: " + chemGroupBlock + "\nSymbol: " + symbol + "\nAtomic Number" + atomicNumber;
   }
}
java swing if-statement arraylist joptionpane
2个回答
0
投票
if (e.get(i).getName().equals(element))

您是否输入了元素的区分大小写的确切名称?例如,找不到“氢”,但“氢”可以正常工作。

您应将此条件更改为:

if(e.get(i).getName().equalsIgnoreCase(element))

代码的其他部分也有其他类似的问题。我建议您对自己的逻辑进行全面审查。致电equals时应格外注意,是否应设为equalsIgnoreCase


0
投票
关于按符号或化学组进行搜索,有一个多余的输入对话框,在输入字段中打印-1(它是JOptionPane.PLAIN_MESSAGE的值:]

element = JOptionPane.showInputDialog(null, list, JOptionPane.PLAIN_MESSAGE);

最好使用“选项对话框”,用户只需单击一个按钮即可选择一个选项,而不用键入它。

接下来,当您按符号搜索时,将提供数据中不可用的选项(例如NaClXe)。

因此,要解决这些问题,您可能需要按如下所示更新代码:

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; } } }

© www.soinside.com 2019 - 2024. All rights reserved.