我正在学习 Java,并且我已经在这个练习上工作了 2 天。我已经在互联网上寻找答案,但我似乎找不到最好的解决我的问题的方法是: 1. 如何存储字符串输入(如 fName、mName //或者我这样做对吗?)到一个二维数组。我见过一些示例程序,其语法如下:
fName = fName.getText();
lName = lname.getText();
但它们似乎不适合我正在尝试编码的内容,我也无法绕过它,因为我只是一个初学者。
- 在所有循环之后如何打印所有输入。就像用户在“输入学生人数”中输入“4”一样,会打印4个学生信息。
这是我最初的非工作代码:
import java.io.*;
public class Main {
public static void main(String[] args)throws IOException{
BufferedReader datain = new BufferedReader (new InputStreamReader (System.in));
String fName, lName, course, yrLevel, bDate;
int age;
int arr [] [];
int size;
System.out.print("Enter number of students: \n");
size = Integer.parseInt(datain.readLine());
arr = new int [size][];
for(int a = 0; a < arr.length; a++){
System.out.print("Enter first name: \n");
fName = datain.readLine();
/*insert code here to store fName to two dimensional array
like this one? arr [0] = new int [a]; */
System.out.print("Enter last name: ");
lName = datain.readLine();
System.out.print("Enter course: ");
course = datain.readLine();
System.out.print("Enter year level: ");
yrLevel = datain.readLine();
System.out.print("Enter age: ");
age = Integer.parseInt(datain.readLine());
}
/* desired output be like:
Student 1
Name: Bat Man
Year Level: Graduate School
Birthdate: Feb. 20, 2012
Age: 7
Student 2
Name: Super Man
Year Level: Masteral
Birthdate: Jan. 1, 2012
Age: 8
*/
System.out.println("Name: " +arr[0][0] + " " + arr[0] [1]); //just me showing my non-working idea of printing them.
}
}