【问题标题】:I'm looking to set the user input equal to the corresponding array in the list?我希望将用户输入设置为等于列表中的相应数组?
【发布时间】:2021-07-09 15:21:07
【问题描述】:

当有人为每个月的降水量输入双倍时,最后几个打印语句会显示某个月份的最少降水量以及他们提供的输入。我如何让月份出现。另外,如何在最终打印语句中将小数转换为英尺和英寸。谢谢!

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");
  
    }
}

【问题讨论】:

  • 请添加您正在使用的语言标签。
  • 你的 for 循环中应该有括号,否则只会执行后面的第一行。
  • 它们的语法是 for( int i1 = 0; i1 &lt; 12; i1++){ ...} 你已经把你的 { 放在 for 循环之前。

标签: java arrays arraylist


【解决方案1】:

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

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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    相关资源
    最近更新 更多