2014-02-05 82 views
1
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 
+0

你能發表更多的代碼嗎?如hrTotal和minTotal –

+0

的聲明當然可以。我剛剛發佈了它。 – MrAwesome8

+0

是否要打印總計或個人小時/分鐘? –

回答

1

主要有兩大修改,使此代碼工作。首先,PrintWriter必須在循環之外聲明並實例化。 println的調用必須發生在循環內部。其次,double必須先轉換爲String,然後才能正確打印和子串。

Scanner in = new Scanner(System.in); 

    System.out.print("How many times? "); 
    int numOfTimes = in.nextInt(); 

    double hrTotal = 0; 
    double minTotal = 0; 

    PrintWriter p = new PrintWriter("Times.txt"); 

    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; 
     String hrs = String.valueOf(hrs1); 
     String min = String.valueOf(min1); 
     p.println(hrs.substring(0, hrs.indexOf(".")) + min.substring(0,min.indexOf("."))); 
    } 
    p.close(); 

在此之前,更改代碼中使用的實際運算中添加兩個雙打hrs1mins1,它是通過你已經在類的頂部聲明的靜態變量。

static double hrs1; 
static double min1; 

你應該刪除這些靜態雙打,因爲他們真的從來沒有使用過。由於println的調用在循環之外,因此導致靜態雙精度被引用,而不是在循環內聲明並分配的hrs1mins1。你以爲你使用循環中聲明的本地變量,但它們從來沒有在範圍內。

+0

謝謝!非常感謝!!!!一旦我得到15個代表,我將投票... – MrAwesome8

+0

@ user3250237很高興我能幫上忙。我還介紹了你最初的嘗試出了什麼問題的解釋。 –

+0

ooohh,謝謝你的解釋!現在有道理。謝謝您的幫助! – MrAwesome8