0

このプログラムで助けが必要です。プログラム全体を完了して設定しました。プログラムが問題なくコンパイルされていても、printfプログラムを実行するたびにエラーが発生するため、ステートメントに問題があります。IllegalFormatConversionException

これはprintf問題です:

System.out.printf("%s%20d%10d%10.1f", "Average", categoryAvg, pressureAvg, speedMPHAvg); 

コード:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Hurricanes2
{
    public static void main(String[] args) throws IOException
    {
        int arrayLength = 59;
        int [] year = new int[arrayLength];
        String [] month = new String[arrayLength];
        int [] pressure = new int[arrayLength];
        int [] speed = new int[arrayLength];
        String [] name = new String[arrayLength];
        double [] speedMPH = new double[arrayLength];
        int [] category = new int[arrayLength];
        int c1 = 0;
        int c2 = 0;
        int c3 = 0;
        int c4 = 0;
        int c5 = 0;
        int categoryMin = 0;
        int categoryMax = 0;
        int speedMin = 0;
        int speedMax = 0;
        int pressureMin = 0;
        int pressureMax = 0;
        int categorySum = 0;
        int speedMPHSum = 0;
        int pressureSum = 0;
        double categoryAvg = 0;
        double speedMPHAvg = 0;
        double pressureAvg = 0;

        File fileName = new File("hurcdata2.txt");
        Scanner inFile = new Scanner(fileName);

        int index = 0;
        while (inFile.hasNext())
        {
            year[index] = inFile.nextInt();
            month[index] = inFile.next();
            pressure[index] = inFile.nextInt();
            speed[index] = inFile.nextInt();
            name[index] = inFile.next();
            index++;
        }

        inFile.close();

        for(index = 0; index < arrayLength; index++)
        {
           speedMPH [index] = (speed[index]) * 1.15;
            if(speedMPH[index] >= 74 && speedMPH[index] <= 95)
           {
                category[index] = 1;
            c1++;
           }
            else if(speedMPH[index] >= 96 && speedMPH[index] <= 110)
           {
                category[index] = 2;
            c2++;
           }
           else if(speedMPH[index] >= 111 && speedMPH[index] <= 130)
           {
                category[index] = 3;
            c3++;
           }
                else if(speedMPH[index] >= 131 && speedMPH[index] <= 155)
           {
                category[index] = 4;
            c4++;
           }
           else
           {
                category[index] = 5;
            c5++;
           }
        }

        for(index = 0; index < arrayLength; index++)
        {
            categoryMin = category[index];
            if(category[index] < categoryMin)
            {
                category[index] = categoryMin;
            }

            categoryMax = category[index];
            if(category[index] > categoryMax)
            {
                category[index] = categoryMax;
            }

            speedMin = speed[index];
            if(speed[index] < speedMin)
            {
                speed[index] = speedMin;
            }

            speedMax = speed[index];
            if(speed[index] > speedMax)
            {
                speed[index] = speedMax;
            }

            pressureMin = pressure[index];
            if(pressure[index] < pressureMin)
            {
                pressure[index] = pressureMin;
            }

            pressureMax = pressure[index];
            if(pressure[index] > pressureMax)
            {
                pressure[index] = pressureMax;
            }

            categorySum += category[index];
            speedMPHSum += speedMPH[index];
            pressureSum += pressure[index];
        }
        categoryAvg = (double)categorySum / arrayLength;
        speedMPHAvg = (double)speedMPHSum / arrayLength;
        pressureAvg = (double)pressureSum / arrayLength;

        System.out.printf("%40s", "Hurricanes 1980 - 2006");
        System.out.println();
        System.out.println();
        System.out.printf("%s%10s%10s%10s%10s", "Year", "Hurricane", "Category", "Pressure (mb)", "Wind Speed (mph)");
        System.out.println();
        System.out.println("=====================================================================");
        for(index = 0; index < arrayLength; index++)
        {
            System.out.printf("%d%10s%10d%10d%10f", year[index], name[index], category[index], pressure[index], speedMPH[index]);
            System.out.println();
        }
    System.out.println("=====================================================================");
    System.out.println();

    System.out.printf("%s%20d%10d%10.1f", "Average", categoryAvg, pressureAvg, speedMPHAvg);
    System.out.println();
    System.out.printf("%s%20d%10d%10.1f", "Maximum", categoryMax, pressureMax, speedMax);
    System.out.println();
    System.out.printf("%s%20d%10d%10.1f", "Minimum", categoryMin, pressureMin, speedMin);
    System.out.println();

    System.out.println("Number of Category 1 Hurricanes: " + c1);
    System.out.println("Number of Category 2 Hurricanes: " + c2);
    System.out.println("Number of Category 3 Hurricanes: " + c3);
    System.out.println("Number of Category 4 Hurricanes: " + c4);
    System.out.println("Number of Category 5 Hurricanes: " + c5);


    }//end main()
}//end Hurricanes2
4

1 に答える 1

0

ではprintfおよび、notなどdの整数型のみが対象byteshortintlongdoubleです。

categoryAvgpressureAvgはであるため、代わりに をdouble使用してください。f

System.out.printf("%s%20f%10f%10.1f", "Average", categoryAvg, pressureAvg, speedMPHAvg);

同様に もfではありませんint

したがって、speedMaxspeedMinはであるため、代わりにint使用する必要があります。d

System.out.printf("%s%20d%10d%10d", "Maximum", categoryMax, pressureMax, speedMax);
System.out.printf("%s%20d%10d%10d", "Minimum", categoryMin, pressureMin, speedMin);

(または変数の型を適切に変更します)。

于 2013-10-13T17:24:44.020 に答える