我一直在研究一个程序,它将使用户输入元素周期表中某个元素的名称或符号,然后输出有关该元素的一些事实。在这里经过了很多问题之后,我明白了程序正确存储所有数据,以我想要的方式输出数据,并且可以接受名称或符号的输入。我现在遇到的问题是,我插入到循环中的中断实际上并没有从循环中中断,我真的不确定为什么。该程序即使收到正确的输入,也会继续要求输入。另外,如果用户输入的是符号而不是名称,则程序将反复告诉用户其输入无效,然后才最终正确输出(然后重新启动循环而不是按需中断)。我是Java的新手,所以如果有人可以帮助我解决这两个问题,并解释问题发生的原因以及他们如何相当简单地解决问题,我将不胜感激。
import java.util.Scanner;
public class PeriodicTable {
public enum Element {
Hydrogen("H", "Nonmetal", "1.008"),
Helium("He", "Noble Gas", "4.003"),
Lithium("Li", "Alkali Metal", "6.941"),
Beryllium("Be", "Alkaline Earth", "9.012"),
Boron("B", "Semimetal", "10.811"),
Carbon("C", "Nonmetal", "12.011"),
//The rest of the periodic table is here, I just removed it for the sake of this post.
private String symbol;
private String group;
private String weight;
private Element(String symbol, String group, String weight) {
this.symbol = symbol;
this.group = group;
this.weight = weight;
}
}
static Element cName = null;
public static void main(String[] args) {
System.out.println("Enter the name or symbol of an element in the periodic table. ");
do {
Scanner reader = new Scanner(System.in);
String input = reader.nextLine().trim();
for (Element sy : Element.values()) {
if (sy.symbol.equalsIgnoreCase(input)) {
System.out.println("Element: " + sy + " (" + sy.symbol + ")" + "\nGroup: " + sy.group + "\nAtomic Mass: " + sy.weight);
reader.close();
break;
} else {
try {
cName = Element.valueOf(input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase());
System.out.println("Element: " + cName + " (" + cName.symbol + ")" + "\nGroup: " + cName.group + "\nAtomic Mass: " + cName.weight);
reader.close();
break;
} catch(IllegalArgumentException e) {
System.out.println("That name or symbol is not valid. Please try again. ");
continue;
}
}
}
} while (true);
}
}
答案 0 :(得分:1)
问题在于const lambda = (yq - yp) / (xq - xp)
位于const numer = yq - yp
const denom = (xq - xp) ** (p - bi2)
const lambda = (numer * denom) % p
循环内,因此仅会中断for循环。如果要中断break
循环,可以使用标签:
for