我可以使用inputmany()进行输入,但是当我为atomicNum输入0时,要让程序停止并运行outputmany()。但事实并非如此。
这是我的代码。某处是错误的,但我不知道。
package workshop3thu;
import java.util.Scanner;
public class Workshop3thu {
public static void inputmany(Workshop3thu[] a, int n) {//n<= 10
Scanner sc = new Scanner(System.in);
int xatomicNum;
String xsymbol;
String xfullname;
float xweight;
int i = 0;
while (i < 10) {
System.out.print("Enter atomic number : ");
xatomicNum = sc.nextInt();
if (xatomicNum == 0) break;
else {
System.out.print("Enter symbol : ");
xsymbol = sc.next();
System.out.print("Enter fullname : ");
xfullname = sc.next();
System.out.print("Enter weight : ");
xweight = sc.nextFloat();
}
}
}
public static void outputmany(Workshop3thu[] a, int n) {
for(int i = 0; i <= n - 1; i++) {
System.out.println(a[i]);
}
}
public static void main(String[] args) {
Workshop3thu[] arr;
Scanner sc = new Scanner(System.in);
System.out.println("Atomic Information");
System.out.println("================== ");
arr = new Workshop3thu[10];
Workshop3thu ob = new Workshop3thu();
inputmany(arr, 10);
outputmany(arr, 10);
}
}
我认为outputmany
功能不起作用,因为参数arr
从未设置为任何值。