java—我希望将用户输入设置为列表中对应的数组?

o3imoua4  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(358)

当有人输入每月降水量的两倍时,最后两份打印的报表说,某个月的降水量最少,并且他们提供了输入。我怎样才能让这个月出现呢。另外,如何在最终打印语句中将小数转换为英尺和英寸。非常感谢。

import java.util.Scanner;
import java.util.ArrayList;

public class Rain2 {  

    public static void main(String[] args) {  

        Scanner scanner= new Scanner(System.in);  

        Double a[] =new Double[12];  
        int year = 0;
        String city = "";
        String EachMonth[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        double lowest = 50.0;
        double highest = 0.0;
        double sum = 0;
        double average = 0;
        double totalPrecipitation = sum;

        System.out.println("Enter city name");
        city = scanner.next();

        do{
            System.out.println("Enter four digit number between 1900 and 2025 ");// prompt user for height in feet
            year = scanner.nextInt();
           if(year < 1900 || year > 2025)
             {
                 System.out.println("Error invalid input enter number between 190 and 2025"); //must be positive whole number of tickets
             }
            } while (year < 1900 || year > 2025);

        System.out.println("Enter the precipitation for the following months ");  

        while(true)
        { 
        System.out.print("January : ");  

        a[0] = scanner.nextDouble();  
        if (a[0] > 50.0 || a[0] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        }

        while(true)
        { 
        System.out.print("February: ");  

        a[1] = scanner.nextDouble();  
        if (a[1] > 50.0 || a[1] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        }  

        while(true)
        { 
        System.out.print("March: ");  

        a[2] = scanner.nextDouble();  
        if (a[2] > 50.0 || a[2] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        }  

        while(true)
        { 
        System.out.print("April: ");  

        a[3] = scanner.nextDouble();  
        if (a[3] > 50.0 || a[3] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        } 

        while(true)
        { 
        System.out.print("May: ");  

        a[4] = scanner.nextDouble();  
        if (a[4] > 50.0 || a[4] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        }  

        while(true)
        { 
        System.out.print("June: ");  

        a[5] = scanner.nextDouble();  
        if (a[5] > 50.0 || a[5] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        } 

        while(true)
        { 
        System.out.print("July: ");  

        a[6] = scanner.nextDouble();  
        if (a[6] > 50.0 || a[6] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        }

        while(true)
        { 
        System.out.print("August: ");  

        a[7] = scanner.nextDouble();  
        if (a[7] > 50.0 || a[7] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        }

        while(true)
        { 
        System.out.print("September: ");  

        a[8] = scanner.nextDouble();  
        if (a[8] > 50.0 || a[8] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        }

        while(true)
        { 
        System.out.print("October: ");  

        a[9] = scanner.nextDouble();  
        if (a[9] > 50.0 || a[9] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        }

        while(true)
        { 
        System.out.print("November: ");  

        a[10] = scanner.nextDouble();  
        if (a[10] > 50.0 || a[10] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        }

        while(true)
        { 
        System.out.print("December: ");  

        a[11] = scanner.nextDouble();  
        if (a[11] > 50.0 || a[11] < 0.0)
            System.out.println("Please enter value in the range [0.0-50.0]");
        else break;
        }

        Double d1=0.0;  
        {int i = 0;
        for (int i1 = 0; i1 < 12; i1++) 

        if (a[i1] > highest)
            highest = a[i1];
        if (a[i] < lowest)
            lowest = a[i];
        sum = sum + a[i];

        sum = (a[0] + a[1] + a[2] + a[3] + a[4] + a[4] + a[6] + a[7] + a[8] + a[9] + a[10] + a[11]);
        totalPrecipitation = sum;}

        average = (a[0] + a[1] + a[2] + a[3] + a[4] + a[4] + a[6] + a[7] + a[8] + a[9] + a[10] + a[11])/12;

        System.out.println("Precipitation Statistics for " + city + " in " + year + " is:");
        System.out.println(EachMonth[0] +":" + a[0]);
        System.out.println(EachMonth[1] +":" + a[1]);
        System.out.println(EachMonth[2] +":" + a[2]);
        System.out.println(EachMonth[3] +":" + a[3]);
        System.out.println(EachMonth[4] +":" + a[4]);
        System.out.println(EachMonth[5] +":" + a[5]);
        System.out.println(EachMonth[6] +":" + a[6]);
        System.out.println(EachMonth[7] +":" + a[7]);
        System.out.println(EachMonth[8] +":" + a[8]);
        System.out.println(EachMonth[9] +":" + a[9]);
        System.out.println(EachMonth[10] +":" + a[10]);
        System.out.println(EachMonth[11] +":" + a[11]);
        System.out.println("The total precipitation for the year was: " + totalPrecipitation + " feet " + "and " + totalPrecipitation + " inches");
        System.out.println("The average monthly precipitation for the year was: " + average +" inches");
        System.out.println("The month with the least amount of precipitation in inches was in " +  EachMonth[0] + " with " + lowest + " inches");
        System.out.println("The month with the most amount of precipitation in inches was in " + EachMonth[0] " with " + highest + " inches");

    }
}
xa9qqrwz

xa9qqrwz1#

您可以使用循环替换月份输入的代码。你不需要过度计算,比如你已经计算了循环中的和,也不需要单独计算。尝试使用一个方便易读的变量命名。您的代码中还有一些错误,我已经更正了它们,并为您提供了更好的版本:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    Double monthWiseValues[] = new Double[12];
    int year = 0;
    String city = "";
    String[] eachMonth = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    double lowest = 50.0;
    int lowestIndex = -1;
    double highest = 0.0;
    int highestIndex = -1;
    double sum = 0;
    double average = 0;
    double totalPrecipitation = sum;

    System.out.println("Enter city name");
    city = scanner.next();

    do {
        System.out.println("Enter four digit number between 1900 and 2025 ");// prompt user for height in feet
        year = scanner.nextInt();
        if (year < 1900 || year > 2025) {
            System.out.println("Error invalid input enter number between 190 and 2025"); //must be positive whole number of tickets
        }
    } while (year < 1900 || year > 2025);

    System.out.println("Enter the precipitation for the following months ");

    double inputVal;
    for (int monthCount = 0; monthCount < eachMonth.length; monthCount++) {
        while (true) {
            System.out.print(eachMonth[monthCount].concat(" : "));

            inputVal = scanner.nextDouble();
            if (inputVal > 50.0 || inputVal < 0.0)
                System.out.println("Please enter value in the range [0.0-50.0]");
            else {
                monthWiseValues[monthCount] = inputVal;
                break;
            }
        }
    }

    for (int monthIndex = 0; monthIndex < eachMonth.length; monthIndex++) {
        if (monthWiseValues[monthIndex] > highest)
            highest = monthWiseValues[monthIndex];
            highestIndex = monthIndex;
        if (monthWiseValues[monthIndex] < lowest)
            lowest = monthWiseValues[monthIndex];
            lowestIndex = monthIndex;
        sum = sum + monthWiseValues[monthIndex];
    }

    totalPrecipitation = sum;

    average = sum / 12;

    System.out.println("Precipitation Statistics for " + city + " in " + year + " is:");
    for (int monthCount = 0; monthCount < eachMonth.length; monthCount++) {
        System.out.println(eachMonth[monthCount] + ":" + monthWiseValues[monthCount]);
    }

    System.out.println("The total precipitation for the year was: " + getValueInFeetInch(totalPrecipitation));
    System.out.println("The average monthly precipitation for the year was: " + getValueInFeetInch(average));
    System.out.println("The month with the least amount of precipitation in inches was in " + eachMonth[lowestIndex] + " with " + lowest + " inches");
    System.out.println("The month with the most amount of precipitation in inches was in " + eachMonth[highestIndex] + " with " + highest + " inches");

}

private static String getValueInFeetInch(double inch) {
    return new StringBuilder(Integer.toString((int)inch / 12))
            .append(" feet ")
            .append(inch % 12)
            .append(" inches.")
            .toString();
}

相关问题