我被要求編寫一個程序,輸入員工的身份證號碼,時間和時間。數據將被寫入.txt文件。我明白,即時通訊應該使用FileWriter,但我可能會問,如果它可能標籤的信息?我喜歡讓我的.txt文件是這樣的:FileWriter和在java中計算基於12小時的時間?
Name Time in Time out Total hours worked Salary
Name1 08:00 05:00 9 4000
Name2 09:00 04:00 7 3000
此外,我將如何計算在12小時工作的總小時數? 這裏是我的源代碼:
import java.util.Scanner;
import java.io.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
class AYANYAN
{static String ans ;
static int empNumber ;
static String timeIn, timeOut;
static Scanner s = new Scanner(System.in);
static String name[] = { "Ayan Ramirez", "Jenifer Sumbi", "Gen Estrada" , "Tugba Cakir", "Lennox Schatje Huisden"};
public static void main(String args[])throws IOException
{
FileWriter fWriter = new FileWriter("EmpData.txt");
for (int i = 0; i < name.length; i++)
{
fWriter.write(name[i] + "\n");
}
fWriter.close();
start() ;
}
public static void control()
{
System.out.print ("\n\nPlease select one of the following: \nA. Sign Off\nB. Enter Time in\nC. Enter Time out\nD. Exit Program\n") ;
ans = s.next() ;
if (ans.equalsIgnoreCase("A")){
signOff() ;
} else if (ans.equalsIgnoreCase("B")) {
timeIn() ;
} else if (ans.equalsIgnoreCase("C")) {
timeOut() ;
} else if (ans.equalsIgnoreCase("D")){
System.out.print ("\nBYE!\n\n\nProgram made by: Ayan Ramirez\n") ;
} else {
System.out.print ("ERROR!") ;
System.exit(0);
}
}
public static void start()
{
System.out.print ("Please enter your id number: ") ;
empNumber = s.nextInt();
if (empNumber == 12345){
System.out.print ("\nSigned in as: " + name[0]);
control() ;
} else if (empNumber == 12346){
System.out.print ("\nSigned in as: " + name[1]);
control() ;
}else if (empNumber == 12347){
System.out.print ("\nSigned in as: " + name[2]);
control() ;
}else if (empNumber == 12348){
System.out.print ("\nSigned in as: " + name[3]);
control() ;
} else if (empNumber == 12349){
System.out.print ("\nSigned in as: " + name[4]);
control() ;
}
else {
System.out.print ("\nNTRUDER ALERT!\n\nPLEASE ENTER THE CORRECT ID NUMBER!") ;
start() ;
}
}
public static void signOff()
{
System.out.print ("Signing off...\n") ;
start() ;
}
public static String timeIn()
{
System.out.print ("Please enter time in: ") ;
timeIn = s.next() ;
control() ;
return (timeIn) ;
}
public static String timeOut()
{
System.out.print ("Please enter time out: ") ;
timeOut = s.next() ;
control() ;
return (timeOut) ;
}
}
有許多事情錯這個程序的流程。嘗試跟隨程序的作用:從'main'方法開始,然後逐步跟蹤程序流程,並考慮它是否在做你認爲應該做的事情。 – Jesper