导出具有多个值的变量?

时间:2014-02-05 22:38:46

标签: java file text export

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。

我希望文本文件像1629一样显示hrs1(16)和min1(29)。但是当我输入hrs1 + min1时,它只输出'0.0'而不是'hrs1 + min1'。我只是想把它输出“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

1 个答案:

答案 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。您以为您正在使用循环中声明的locals变量,但它们从不在范围内。