2014-03-07 36 views
-3

基本上我有一些數組,我想讓用戶改變一些數據,如果他想通過用戶輸入。我想我應該使用掃描儀,但我不完全清楚如何做到這一點。如何通過用戶輸入更改數組中的對象?

的數組存儲在一個類中調用數據庫

private Student[] arrayStudents; 
public DepotDatabase() { 
    arrayStudents = new Driver[4]; 
    arrayStudents[0] = new Student("First", "Second", 1234, 1234); // sample student 
    arrayStudents[1] = new Student("First 1", "Second2" , 4444, 4444); // sample student 

這是陣列中的數據被稱爲在另一個類叫學生。

public Student(String firstName, String lastName, int username, int password) { 
    this.firstName = firstName; 
    this.lastName = lastName; 
    this.username = username; 
    this.password = password; 
} 

,並在另一個名爲類SetUpCourse我想,讓用戶改變一個學生通過用戶輸入的名稱,也如更改用戶名arrayStudent [0]arrayStudent [ 1]密碼

我想我會有這樣的事情在SetUpWorkSchedule

Scanner scan = new Scanner(System.in); 
System.out.println("Please enter a password"); 
setPassword() = scan.nextLine(); 
database.printArrayStudent(); 

現在要打印出數組,我有一個名爲printArrayStudent的方法,並打印出所有的學生。

+0

這不是一個問題。嘗試過某些東西后,請回復一個具體問題。 – aliteralmind

+0

很簡單,將* setter *和* getter *方法添加到'Student'類。 'arrayStudents [0] .setUsername(「....」)'......等等。 – Azad

+0

我改變了這個問題,我也提出了更清晰的問題。我希望這可以幫助您再次查看問題,並希望得到幫助。 – user3383541

回答

0

我覺得你Student類應該是這樣的:

public class Student { 
    private String firstName; 
    private String lastName; 
    private String userName; 
    private String password; 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    //and do for other global variables lastName,userNAme,Password.. 
    //parametric constructor 
    public Student(String firstName, String lastName, int username, int password) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.username = username; 
     this.password = password; 
    } 
    //and default constructor 
} 

然後你就可以改變對學生的任何信息,在評論中提到這樣的:

arrayStudents[0].setUserName("username"); 
arrayStudents[0].setFirstName("firstname"); 
arrayStudents[0].setLastName("lastname"); 
//...... 
相關問題