在Java(IntelliJ)中进行周期表搜索。

huangapple go评论67阅读模式
英文:

Making a periodic table search in java (intellij)

问题

代码翻译如下:

目标是创建一个元素周期表搜索引擎用户被询问问题以缩小搜索结果范围或者如果用户知道元素的名称可以直接输入一旦输入或找到元素它将提供有关元素的信息目前只有少量细节和少量元素用于让代码运行以后会添加更多元素和细节例如结构和原子质量

每当找到结果时它不会提供信息只会显示一个空白屏幕我尝试将其放入类中的toString方法中然后在主函数的底部调用它但我无法找出缺少了什么这似乎是一个简单的System.out.print命令但它也不起作用

非常感谢任何帮助

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,
                "您是否知道要查找的元素名称?(是/否)",
                "欢迎使用化学表!", 3);

        if (choice.equalsIgnoreCase("是")) {
            element = JOptionPane.showInputDialog(null,
                    "请输入元素的名称...",
                    "欢迎使用化学表!", JOptionPane.PLAIN_MESSAGE);
        } else if (choice.equalsIgnoreCase("否")) {
            searchBy = JOptionPane.showInputDialog(null,
                    "按照:'化学族族块' 还是 '符号' 进行搜索?",
                    "欢迎使用化学表", 3);

            if (searchBy.equalsIgnoreCase("化学族族块")) {
                chemGroupBlock = JOptionPane.showInputDialog(null,
                        "请输入化学族族块(非金属/惰性气体/碱金属)",
                        "欢迎使用化学表", 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 = JOptionPane.showInputDialog(null,
                        "请输入元素符号:(He/NaCl/Xe)",
                        "欢迎使用化学表",
                        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, "元素结果", 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 "元素:" + name + "\n化学族族块:" + chemGroupBlock + "\n符号:" + symbol + "\n原子序数:" + atomicNumber;
    }
}
英文:

The objective is to make a periodic table of elements search engine. The user is asked questions to narrow down the results or if the user knows the name of the element it just be entered in. Once entered in or the element is found it's going to give information about the element. I only have a few details and a few elements to get the code working and more elements and details will be added such as structure and atomic weight.

Whenever the result is found it doesn't give the information it just gives a blank screen. I thought to put it in the class as toString and then calling it at the bottom of my main was enough but I can't figure out what I'm missing. It seems like a simple system out print command but that doesn't work either.

Any help is greatly appreciated.

import java.util.ArrayList;
import javax.swing.*;
public class Main {
public static void main(String args[]) {
ArrayList&lt;Element&gt; e = new ArrayList&lt;Element&gt;();
String choice, element = &quot;&quot;, searchBy, symbol, chemGroupBlock, list = &quot;&quot;, str = &quot;&quot;;
e.add(new Element(&quot;Hydrogen&quot;, &quot;Nonmetal&quot;, &quot;H&quot;, 1));
e.add(new Element(&quot;Helium&quot;, &quot;Nobel Gas&quot;, &quot;He&quot;, 2));
e.add(new Element(&quot;Lithium&quot;, &quot;Alkali Metal&quot;, &quot;Li&quot;, 3));
choice = JOptionPane.showInputDialog(null,
&quot;Do you know the name of the element you&#39;re looking for?&quot;
+ &quot;(Yes/No)&quot;, &quot;Welcome to the Chem Table!&quot;, 3);
if (choice.equalsIgnoreCase(&quot;yes&quot;)) {
element = JOptionPane.showInputDialog(null,
&quot;Enter the name of the Element.. &quot;,
&quot;Welcome to the Chem Table!&quot;, JOptionPane.PLAIN_MESSAGE);
}else if (choice.equalsIgnoreCase(&quot;no&quot;)) {
searchBy = JOptionPane.showInputDialog(null,
&quot;Search by: &#39;chemical group block&#39; or &#39;symbol&#39;?&quot;, &quot;Welcome to the Chem Table&quot;, 3);
if (searchBy.equalsIgnoreCase(&quot;chemGroupBlock&quot;)) {
chemGroupBlock = JOptionPane.showInputDialog(null,
&quot;Enter Chemical Group Block (Nonmetal/Nobel Gases/Alkalies)&quot;,
&quot;Welcome to The Chem Table&quot;, JOptionPane.PLAIN_MESSAGE);
for (int i = 0; i &lt; e.size(); i++){
if (e.get(i).getchemGroupBlock().equals(chemGroupBlock)){
list = list + e.get(i).getName() + &quot;\n&quot;;
}
}
element = JOptionPane.showInputDialog(null, list,
JOptionPane.PLAIN_MESSAGE);
} else if (searchBy.equalsIgnoreCase(&quot;symbol&quot;)) {
symbol = JOptionPane.showInputDialog(null,
&quot;Enter the element symbol:(He/NaCl/Xe)&quot;,
&quot;Welcome to the Chem Table&quot;,
JOptionPane.PLAIN_MESSAGE);
for (int i = 0; i &lt; e.size(); i++) {
if (e.get(i).getSymbol().equals(symbol)) {
list = list + e.get(i).getName() + &quot;\n&quot;;
}
}
element = JOptionPane.showInputDialog(null, list,
JOptionPane.PLAIN_MESSAGE);
}
}
for (int i = 0; i &lt; e.size(); i++) {
if (e.get(i).getName().equals(element)) {
str = e.get(i).toString();
}
}
JOptionPane.showMessageDialog(null, str, &quot;Element Results&quot;, 1);
}
}

Here is the class for elements

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 &quot;Element: &quot; + name + &quot;\nChemical Group Block: &quot; + chemGroupBlock + &quot;\nSymbol: &quot; + symbol + &quot;\nAtomic Number&quot; + atomicNumber;
}
}

答案1

得分: 1

我认为问题出在这个条件上:

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

你输入的元素名称是不是完全符合大小写?例如,"hyrdogen" 将无法找到,但 "Hydrogen" 可以正常工作。

你应该将这个条件改为:

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

在代码的其他部分也存在类似的问题。我建议你全面检查一下你的逻辑。特别注意何时调用 equals,以及是否应该使用 equalsIgnoreCase

英文:

I think the problem is this condition:

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

Did you enter the exact case-sensitive name of the element? For example "hyrdogen" will not be found, but "Hydrogen" will work fine.

You should change this condition to:

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

There are other similar problems in other parts of the code as well. I suggest you do a full review of your logic. Pay close attention to when you call equals and whether it should be equalsIgnoreCase

答案2

得分: 1

用户输入元素名称时,只有当元素名称正确无误时,搜索才会成功。如上所述,搜索区分大小写,因此您可以使用.equalsIgnoreCase

至于通过符号或化学组搜索,存在多余的输入对话框,该对话框在输入字段中显示-1(这是JOptionPane.PLAIN_MESSAGE的值):

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

最好使用“选项对话框”,用户只需点击按钮即可选择选项,而无需键入内容。

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

为了解决这些问题,您可以按以下方式更新代码:

int result = JOptionPane.showConfirmDialog(null,
				"您是否知道您要查找的元素的名称?", 
				"欢迎使用化学表!", JOptionPane.YES_NO_CANCEL_OPTION);

if (result == JOptionPane.YES_OPTION) {
	element = JOptionPane.showInputDialog(
        null, "请输入元素的名称... ",
		"欢迎使用化学表!", JOptionPane.PLAIN_MESSAGE);
} else if (result == JOptionPane.NO_OPTION) {
	String[] byOptions = {"化学组",  "元素符号"};
	result = JOptionPane.showOptionDialog(
				null, 
				"按照:'化学组' 还是 '元素符号' 进行搜索?",
				"欢迎使用化学表", 
				JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
				null, byOptions, byOptions[0]);

	if (result == 0) {
		String[] byGroup = {"非金属", "惰性气体", "碱金属"};
		result = JOptionPane.showOptionDialog(
					null,
					"选择化学组", 
					"欢迎使用化学表",
					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 { // 按照符号搜索
		symbol = JOptionPane.showInputDialog(
					null, "请输入元素符号:(H/He/Li)",
					"欢迎使用化学表",
					JOptionPane.PLAIN_MESSAGE);
		for (Element el : e) {
			if (el.getSymbol().equalsIgnoreCase(symbol)) {
				element = el.getName();
				break;
			}
		}
		if (element.isEmpty()) {
			JOptionPane.showMessageDialog(
				null, "未找到符号" + symbol + "!", "未找到!", JOptionPane.WARNING_MESSAGE);
			return;
		}
	}
}
英文:

When user inputs a name of the element, the search succeeds providing that the element's name is correct. As suggested above, the search is case sensitive now, so you may use .equalsIgnoreCase.

As for the search by symbol or chemicalgroup, there is redundant input dialog which prints -1 in the input field (it is value of JOptionPane.PLAIN_MESSAGE):

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

It would be better to use "option dialogs" where user could just click a button to select an option instead of typing it in.

Next, when you search by symbol, you offer options unavailable in your data (e.g., NaCl and Xe).

So, to resolve these issues you may want to update the code as follows:

int result = JOptionPane.showConfirmDialog(null,
&quot;Do you know the name of the element you&#39;re looking for?&quot;, 
&quot;Welcome to the Chem Table!&quot;, JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
element = JOptionPane.showInputDialog(
null, &quot;Enter the name of the Element.. &quot;,
&quot;Welcome to the Chem Table!&quot;, JOptionPane.PLAIN_MESSAGE);
} else if (result == JOptionPane.NO_OPTION) {
String[] byOptions = {&quot;chemical group block&quot;,  &quot;symbol&quot;};
result = JOptionPane.showOptionDialog(
null, 
&quot;Search by: &#39;chemical group block&#39; or &#39;symbol&#39;?&quot;,
&quot;Welcome to the Chem Table&quot;, 
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, byOptions, byOptions[0]);
if (result == 0) {
String[] byGroup = {&quot;Nonmetal&quot;, &quot;Nobel Gases&quot;, &quot;Alkalies&quot;};
result = JOptionPane.showOptionDialog(
null,
&quot;Select Chemical Group&quot;, 
&quot;Welcome to The Chem Table&quot;,
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, &quot;Enter the element symbol:(H/He/Li)&quot;,
&quot;Welcome to the Chem Table&quot;,
JOptionPane.PLAIN_MESSAGE);
for (Element el : e) {
if (el.getSymbol().equalsIgnoreCase(symbol)) {
element = el.getName();
break;
}
}
if (element.isEmpty()) {
JOptionPane.showMessageDialog(
null, &quot;Symbol&quot; + symbol + &quot; not found!&quot;, &quot;Not found!&quot;, JOptionPane.WARNING_MESSAGE);
return;
}
}
}

huangapple
  • 本文由 发表于 2020年5月4日 01:55:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61579172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定