我需要显示以下信息
Enter size of array: 3
Enter student name: jane doe
Enter student id number: 14234567
Enter student name: john doe
Enter student id number: 12345678
Enter student name: peter griffin
Enter student id number: 14238251
Array Contents
jane doe 14234567
john doe 12345678
peter griffin
14238251
我必须使用以下方法
向用户询问他们希望数组的大小,然后创建一个类型的数组 指定大小的学生。
public static Student[] createArray()
通过要求用户输入每个对象的名称和ID号来填充数组 存储在数组中
public static void populateArray(Student[] array)
显示数组的内容
public static void displayArray(Student[] array)
这是我到目前为止所拥有的
class Student {
//private instances
private String name = "unassigned";
private long idNumber = 0;
//Default constructor
public Student() {
name = "Not Given";
idNumber = 0;
}
//Constructs a new Student with passed name and age parameters.
public Student(String studentName, long studentIdNumber) {
name = studentName;
idNumber = studentIdNumber;
}
//Returns the name of this student.
public String getName( ) {
return name;
}
// Returns the idNumber of this person.
public long getIdNumber( ) {
return idNumber;
}
//Sets the name of this student.
public void setName(String studentName ) {
name = studentName;
}
//Sets the idNumber of this student.
public void setIdNumber(long studentIdNumber ) {
idNumber = studentIdNumber;
}
} // end class
和
import java.util.Scanner;
public class StudentTest {
static int array;
//create method createArray
public static Student[] createArray() {
Scanner int_input = new Scanner(System.in);
//user enters size of array
System.out.print("Enter Size of Array: ");
array = int_input.nextInt();
//create array
Student[] array = new Student[0];
return array;//return array
}//end method
//create method populateArray
public static void populateArray(Student[] array) {
try (Scanner string_input = new Scanner(System.in);
Scanner long_input = new Scanner(System.in);)
{
for (int i = 0; i < 3; i++) {
Student arrays = new Student();
//set name
System.out.println("Enter Student Name: ");
arrays.setName(string_input.nextLine());
//set ID number
System.out.println("Enter Student ID Number: ");
arrays.setIdNumber(long_input.nextLong());
System.out.println();
}//end for loop
}//end try loop
}//end method
//create method display Array
public static void displayArray(Student[] array){
System.out.println("Array Contents");
System.out.println();
}//end method
public static void main(String [] args){
//display createArray method
System.out.println(createArray());
//display populateArray method
System.out.println();
//display array contents
System.out.println();
}//end main method
}//end class
我的问题是如何让populateArray方法允许我输入数据然后显示它?并获取displayArray方法显示?我尝试了很多不同的东西,但我无法弄清楚
答案 0 :(得分:0)
你必须根据你的逻辑修复一些事情。从main
开始:
public static void main(String [] args) {
// create array of size specified by user
Student[] students = createArray();
// populate this array with data from user
populateArray(students);
// finally, display array contents
displayArray(students);
}//end main method
在createArray
创建并返回正确大小的数组:
// read user input as arraySize
return new Student[arraySize];
在populateArray
中,确保使用来自用户的数据修改传递的数组,即:
for (int i = 0; i < 3; i++) {
Student student = new Student(); // new student
//set name
System.out.println("Enter Student Name: ");
student.setName(scanner.nextLine());
//set ID number
System.out.println("Enter Student ID Number: ");
student.setIdNumber(scanner.nextLong());
inputArray[i] = student; // put new student into array passed to the method
}//end for loop
最后,在displayArray
中,只需迭代项目并以您喜欢的格式打印它们:
public static void displayArray(Student[] students){
for (Student s : students) {
System.out.println(String.format("Name = %s, id = %d", s.getName(), s.getIdNumber());
}
}//end method
当然,你真的不需要有3种不同的方法来实现这个简单的功能,但它应该可以工作。