2017-10-08 31 views
0

好吧。這是我的第一頁與訪問器和mutators我如何將數據輸入到用戶的數組中?我正在使用訪問器和增變器

public class TimeCard { 
    private int employeeNum; 
    private String[] clockInTimes = new String[14]; 
    private String[] clockOutTimes = new String[14]; 
    private float[] decimalClockIn = new float[14]; 
    private float[] decimalClockOut = new float[14]; 
    private float[] timeElapsed = new float[14]; 

public String[] getClockInTimes() 
    { 
     return clockInTimes; 
    } 

    public void setClockInTimes(String[] value) 
    { 
     clockInTimes = value; 
    } 
} 

我的第二類acessessing set/get數組。 我如何要求每個數組下標0-13的用戶輸入?

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 


Scanner reader = new Scanner(System.in); 

    TimeCard josue = new TimeCard(); 

    System.out.println("Enter Monday Clock In Times:"); 
    //not sure if this is right? 
    josue.setClockInTimes[0](reader.next()); 
    } 
} 

順便說一句,我需要這樣做,因爲老師想這樣。我只是不確定如何獲得用戶輸入並將其放入使用對象類的數組中。

+0

你可能想要做一些類似於你的setter獲取索引*和*值的東西。目前,setter將覆蓋整個數組。 – EvanM

+0

那看起來會怎樣?我有點迷路。 –

+0

嘗試將掃描儀放入for循環並將數組索引設置爲for循環變量。 –

回答

0

我可能會改變你的制定者採取這樣的指數和值,所以開始的東西:

public void setClockInTime(int day, String clockInTime) { 
    this.clockInTimes[day] = clockInTime; // note you don't need to write "this," but it's clearer that this is a member field 
} 

然後在你的主要方法:

for (int i=0;i<14;i++) { 
    String input = <get input> 
    josue.setCliockInTime(i, input); 
} 

現在你可以一次設置一個值,並且應該讓您填充所有的字段。

相關問題