一周中的天数高低温平均排列

时间:2013-11-26 05:36:26

标签: java

我对此感到非常沮丧。我需要从任何文件中读取并回写一个文件,包括当天的高温和低温以及当天的平均值......

1        62

2        54

7        55

6        77

1        55 

这假设继续无限的未知。我需要把这个文件写成这个

dow         high           low           average
1            78            66            70

2            87            77            81

3            70             80           75

我无法弄清楚我哪里出错了。

import java.io.File;
import java.util.Formatter;
import java.util.Scanner;

public class dowsdowsdows {

    public static void main(String [] args) {

        try {

            Scanner scanner = new Scanner(new File("Dowinputnumbers.txt"));
            Formatter formatter = new Formatter(new File("Dowoutputnumbers.txt"));

            int [] dows;
            int [] hightemps;
            int [] lowtemps;
            int [] count = null;

            while (scanner.hasNext()) {

                int dow = scanner.nextInt();
                int temp = scanner.nextInt();
                dows = new int [8];

                hightemps = new int [8];

                lowtemps = new int [8];

                formatter.format("%d %d\n", dows, hightemps, lowtemps);
                {
                    for (int i = 0 ; i < 7 ; i++) {
                        int j = i + 1;
                        int d = (int) dows[i] / count[i];
                        formatter.format(j + " " + " " + " " + d);

                    }

                    break;

                }

            }
            formatter.flush();
            formatter.close();

        }
        catch (Exception e) {
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我看到了两件让你更轻松的事情。

首先 - 使用容器(因为你不是用纯C编写的,使用“功能”Java它可以让你的代码变得简单)。我将选择Map进行阅读。并为写作结果 - 地图&gt;。 int - for dow,List - 供参考。 (这是一个不好的做法,在'while'循环中逐步分配内存)

第二 - 你应该更清楚地了解你想做什么。 Wright这个词(英语或俄语或西班牙语)的algorythm,然后写代码。

答案 1 :(得分:0)

您可以使用以下代码从文件计算最高,最低温度:

鉴于Dowinputnumbers.txt包含:

1 62
2 54
7 55
6 77
1 55

然后Temperatures.java:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;
import java.util.NoSuchElementException;

public class Temperatures
{
    public static final int    DOWS  = 7;
    public static final int    NOVAL = 999;
    public static final String DI    = "Dowinputnumbers.txt";
    public static final String DO    = "Dowoutputnumbers.txt";

    public static boolean isValid(int dow)
    {
        return 1 <= dow && 7 >= dow;
    }

    public static void main(String[] args)
    {
        /**
         * we need array starting from 1 ([1..DOWS]) 
         * so just use 1 plus size array 
         */
        int[] hightemps = new int[DOWS + 1];
        int[] lowtemps  = new int[DOWS + 1];
        int[] counts    = new int[DOWS + 1];
        int[] sum       = new int[DOWS + 1];
        int   dow;
        int   temp;

        /**
         * init highest and lowest temps to "no value" values
         */
        for (int i = 1; i < DOWS + 1; i++)
        {
            hightemps[i] = -NOVAL;
            lowtemps[i]  = NOVAL;
        }

        try
        {
            Scanner scanner = new Scanner(
                    new File(DI));
            Formatter formatter = new Formatter(
                    new File(DO));

            while (scanner.hasNextInt())
            {
                try
                {
                    dow  = scanner.nextInt();
                    temp = scanner.nextInt();
                    if (isValid(dow))
                    {
                        counts[dow]++;
                        sum[dow] += temp;
                        if (hightemps[dow] < temp)
                        {
                            hightemps[dow] = temp;
                        }
                        if (lowtemps[dow] > temp)
                        {
                            lowtemps[dow] = temp;
                        }
                    }
                }
                catch (NoSuchElementException e)
                {
                    /**
                     * input has ended
                     */
                    System.out.println("No data left?!.. :(");
                }
            }

            /**
             * now we have collected all temperatures
             */
            formatter.format("dow high low average\n");
            for (dow = 0; dow < DOWS; dow++)
            {
                /**
                 * if we have temps for dow
                 */
                if (0 < counts[dow])
                {
                    formatter.format(
                            "%d %d %d %.2f\n",
                            dow              ,
                            hightemps[dow]   ,
                            lowtemps[dow]    ,
                            ((double) sum[dow]) / counts[dow]);
                }
            }
            formatter.flush();
            formatter.close();
        }
        catch (FileNotFoundException e)
        {
            System.out.println("File not found!");
        }
    }
}