import java.util.*;
import java.text.*;
import java.io.*;
public class AvgTime {
static double hrs1;
static double min1;
public static void main(String[] args) throws InterruptedException, FileNotFoundException {
Scanner in = new Scanner(System.in);
System.out.print("How many times? ");
int numOfTimes = in.nextInt();
double hrTotal = 0;
double minTotal = 0;
for (int i = 1; i <= numOfTimes; i++){
System.out.println("\nEnter time in military time notation: ");
System.out.print("Hour ");
double hrs1 = in.nextDouble();
System.out.print("Minute ");
double min1 = in.nextDouble();
hrTotal += hrs1;
minTotal += min1;
}
//calculate average
double avgHr1 = hrTotal/numOfTimes;
double timeMin1 = Math.round(minTotal/numOfTimes);
DecimalFormat df = new DecimalFormat("###");
String hours = df.format(avgHr1);
String minutes = df.format(timeMin1);
String time = hours+":"+minutes+":"+00;
String mt = hours+minutes;
SimpleDateFormat fmtMil = new SimpleDateFormat("HH:mm:ss");
Date inDate = fmtMil.parse(time, new ParsePosition(0));
SimpleDateFormat fmtAMPM = new SimpleDateFormat("hh:mm:ss aa");
StringBuffer outDate = fmtAMPM.format(inDate, new StringBuffer(), new FieldPosition(0));
System.out.println("\nThe average time is " + outDate+"\n"+mt+" in military time.\n\n");
String copy = "Copying...";
Thread.sleep(550);
for(int i = 0; i < copy.length(); i++) {
System.out.print(copy.charAt(i));
try{ Thread.sleep(105); }catch(Exception e){}
}
PrintWriter p = new PrintWriter("Times.txt");
p.println(hrs1+min1);
p.close();
System.out.println("\nDone\n\n\n");
Thread.sleep(550);
}
}
我希望它導出到用戶輸入的時間文件。例如,如果用戶將numOfTimes設置爲2,那麼對於hrs1和min1我將有2個不同的值。導出具有多個值的變量?
說,他們進入:1629和2018年
我想要的文本文件,以顯示HRS1(16)和分1(29)就像1629,但而不是「HRS1 +分1」,當我把hrs1 + min1它只是輸出'0.0'。我只是想要它出口「1629」和「2018」 。
這是爲什麼?如何解決?
輸出
How many times? 2
Enter time in military time notation:
Hour 16
Minute 29
Enter time in military time notation:
Hour 20
Minute 18
應該怎樣導出:
1629
2018
你能發表更多的代碼嗎?如hrTotal和minTotal –
的聲明當然可以。我剛剛發佈了它。 – MrAwesome8
是否要打印總計或個人小時/分鐘? –