所以我在理解如何开始 findAbbreviation(atomic#)
方法时遇到了问题。每次我尝试在另一种方法中使用 table[]
时,都会出现非静态变量问题。请分享有关如何启动此方法的任何提示,该方法将返回对带有缩写的元素的引用。
public class PeriodicTable {
private final static int MAX_ELEMENTS = 150;
private PeriodicElement[] table;
private int actualSize;
public PeriodicTable () throws IOException {
table = new PeriodicElement[MAX_ELEMENTS];
Scanner input = new Scanner(new File("/user/tvnguyen7/data/periodictable.dat"));
int index = 0;
while(input.hasNext() && index < MAX_ELEMENTS) {
int aN = input.nextInt();
String abbr = input.next();
String name = input.next();
double aW = input.nextDouble();
table[index] = new PeriodicElement(aN, abbr,name, aW);
index++;
}
input.close();
actualSize = index;
}
public void printTable () throws IOException {
final int MAX_ELEMENTS = 118;
Scanner input = new Scanner(new File("/user/tvnguyen7/data/periodictable.dat"));
int[] atomicNumbers = new int[MAX_ELEMENTS];
double[] atomicWeights = new double[MAX_ELEMENTS];
for(int i=0;i<atomicNumbers.length;i++) {
atomicNumbers[i]= input.nextInt();
String abbreviation = input.next();
String name = input.next();
atomicWeights[i] = input.nextDouble();
System.out.printf("%4d %-4s %-25s %.2f\n" , atomicNumbers[i], abbreviation, name, atomicWeights[i]);
}
input.close();
最佳答案
您尚未给出 findAbbreviation
方法的完整签名,但我猜您正在尝试使其静态。
使用以下方法:
public String findAbbreviation(int atomicNumber)
{
// code...
}
另外,只是一个提示,在构造函数中包含逻辑并不是一个好习惯。 谢谢!
关于Java 不知道从哪里开始使用这个方法来获取元素周期表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42824486/