3
我是一个尝试使用基本的GUI函数为练习中的前56个元素制作简单问题和回答程序的新手。GUI Loop问题Java
但是,当我运行我的代码时,我得到随机代码运行期间的错误。
一个文本框将出现没有对话只是消息作为文本框的名称,并X按钮退出。有时当我按下X时,它会转到我代码中的下一行,其他时候它会退出。因为它似乎是随机发生的,所以我很难重现这个错误。
我的主要方法是下面贴:
import java.math.*;
import java.text.DecimalFormat;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.util.Random;
//This lab is to study periodc table of elements with simple gui output
public class practiceFiftyEight
{
public static void main(String[] args)
{
String input;
boolean answer;
Random myRan = new Random();
int randInt = 0;
//random num from 0 - 56
String[] arrayElements = {"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Mangnesium","Aluminium","Silicon","Phosphorus","Sulfur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chronium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc",
"Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Cesium","Barium"};
String[] arrayEleAbriv = {"H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As",
"Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba"};
int repeat;
do
{
randInt = myRan.nextInt(56);
JOptionPane.showMessageDialog(null, " sizes of arrays " + arrayElements.length + " " + arrayEleAbriv.length);
JOptionPane.showMessageDialog(null, " What is the symbol of " + arrayElements[randInt]);
input = JOptionPane.showInputDialog(null," Enter element symbol (1 - 56) of");
answer = input.equalsIgnoreCase(arrayEleAbriv[randInt]);
if(answer)
{
JOptionPane.showMessageDialog(null, " Correct " + arrayElements[randInt] + " is represented by " + arrayEleAbriv[randInt]);
}
else
{
JOptionPane.showMessageDialog(null, " WRONG!!!!! " + arrayElements[randInt] + " is represented by " + arrayEleAbriv[randInt] + " !!!!! ");
}
repeat = JOptionPane.showConfirmDialog(null, "Press Yes to repeat, No to quit ", "please confirmm", JOptionPane.YES_NO_OPTION);
}while(repeat == JOptionPane.YES_OPTION);
}
}
镁被拼写为“Mangnesium”。将变量声明为更接近它们的用法可能会更好,例如,使用“输入”和“答案”。我想知道,因为这个类被命名为'practiceFiftyEight',并且每次在循环中打印数组的大小,但是只有56个元素被支持,如果问题与您的程序的早期版本有关没有匹配?我试过了,无法重现任何错误。 – 2014-09-10 16:57:58
你有什么记录吗? – m4rtin 2014-09-10 16:59:15
您还有几个未使用的导入。这将是一个好主意,从文件中读取元素数据,而不是硬编码所有内容,但我意识到这仅仅是一个开始。 – 2014-09-10 16:59:18