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

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

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

  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. public class Rain2 {
  4. public static void main(String[] args) {
  5. Scanner scanner= new Scanner(System.in);
  6. Double a[] =new Double[12];
  7. int year = 0;
  8. String city = "";
  9. String EachMonth[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  10. double lowest = 50.0;
  11. double highest = 0.0;
  12. double sum = 0;
  13. double average = 0;
  14. double totalPrecipitation = sum;
  15. System.out.println("Enter city name");
  16. city = scanner.next();
  17. do{
  18. System.out.println("Enter four digit number between 1900 and 2025 ");// prompt user for height in feet
  19. year = scanner.nextInt();
  20. if(year < 1900 || year > 2025)
  21. {
  22. System.out.println("Error invalid input enter number between 190 and 2025"); //must be positive whole number of tickets
  23. }
  24. } while (year < 1900 || year > 2025);
  25. System.out.println("Enter the precipitation for the following months ");
  26. while(true)
  27. {
  28. System.out.print("January : ");
  29. a[0] = scanner.nextDouble();
  30. if (a[0] > 50.0 || a[0] < 0.0)
  31. System.out.println("Please enter value in the range [0.0-50.0]");
  32. else break;
  33. }
  34. while(true)
  35. {
  36. System.out.print("February: ");
  37. a[1] = scanner.nextDouble();
  38. if (a[1] > 50.0 || a[1] < 0.0)
  39. System.out.println("Please enter value in the range [0.0-50.0]");
  40. else break;
  41. }
  42. while(true)
  43. {
  44. System.out.print("March: ");
  45. a[2] = scanner.nextDouble();
  46. if (a[2] > 50.0 || a[2] < 0.0)
  47. System.out.println("Please enter value in the range [0.0-50.0]");
  48. else break;
  49. }
  50. while(true)
  51. {
  52. System.out.print("April: ");
  53. a[3] = scanner.nextDouble();
  54. if (a[3] > 50.0 || a[3] < 0.0)
  55. System.out.println("Please enter value in the range [0.0-50.0]");
  56. else break;
  57. }
  58. while(true)
  59. {
  60. System.out.print("May: ");
  61. a[4] = scanner.nextDouble();
  62. if (a[4] > 50.0 || a[4] < 0.0)
  63. System.out.println("Please enter value in the range [0.0-50.0]");
  64. else break;
  65. }
  66. while(true)
  67. {
  68. System.out.print("June: ");
  69. a[5] = scanner.nextDouble();
  70. if (a[5] > 50.0 || a[5] < 0.0)
  71. System.out.println("Please enter value in the range [0.0-50.0]");
  72. else break;
  73. }
  74. while(true)
  75. {
  76. System.out.print("July: ");
  77. a[6] = scanner.nextDouble();
  78. if (a[6] > 50.0 || a[6] < 0.0)
  79. System.out.println("Please enter value in the range [0.0-50.0]");
  80. else break;
  81. }
  82. while(true)
  83. {
  84. System.out.print("August: ");
  85. a[7] = scanner.nextDouble();
  86. if (a[7] > 50.0 || a[7] < 0.0)
  87. System.out.println("Please enter value in the range [0.0-50.0]");
  88. else break;
  89. }
  90. while(true)
  91. {
  92. System.out.print("September: ");
  93. a[8] = scanner.nextDouble();
  94. if (a[8] > 50.0 || a[8] < 0.0)
  95. System.out.println("Please enter value in the range [0.0-50.0]");
  96. else break;
  97. }
  98. while(true)
  99. {
  100. System.out.print("October: ");
  101. a[9] = scanner.nextDouble();
  102. if (a[9] > 50.0 || a[9] < 0.0)
  103. System.out.println("Please enter value in the range [0.0-50.0]");
  104. else break;
  105. }
  106. while(true)
  107. {
  108. System.out.print("November: ");
  109. a[10] = scanner.nextDouble();
  110. if (a[10] > 50.0 || a[10] < 0.0)
  111. System.out.println("Please enter value in the range [0.0-50.0]");
  112. else break;
  113. }
  114. while(true)
  115. {
  116. System.out.print("December: ");
  117. a[11] = scanner.nextDouble();
  118. if (a[11] > 50.0 || a[11] < 0.0)
  119. System.out.println("Please enter value in the range [0.0-50.0]");
  120. else break;
  121. }
  122. Double d1=0.0;
  123. {int i = 0;
  124. for (int i1 = 0; i1 < 12; i1++)
  125. if (a[i1] > highest)
  126. highest = a[i1];
  127. if (a[i] < lowest)
  128. lowest = a[i];
  129. sum = sum + a[i];
  130. 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]);
  131. totalPrecipitation = sum;}
  132. 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;
  133. System.out.println("Precipitation Statistics for " + city + " in " + year + " is:");
  134. System.out.println(EachMonth[0] +":" + a[0]);
  135. System.out.println(EachMonth[1] +":" + a[1]);
  136. System.out.println(EachMonth[2] +":" + a[2]);
  137. System.out.println(EachMonth[3] +":" + a[3]);
  138. System.out.println(EachMonth[4] +":" + a[4]);
  139. System.out.println(EachMonth[5] +":" + a[5]);
  140. System.out.println(EachMonth[6] +":" + a[6]);
  141. System.out.println(EachMonth[7] +":" + a[7]);
  142. System.out.println(EachMonth[8] +":" + a[8]);
  143. System.out.println(EachMonth[9] +":" + a[9]);
  144. System.out.println(EachMonth[10] +":" + a[10]);
  145. System.out.println(EachMonth[11] +":" + a[11]);
  146. System.out.println("The total precipitation for the year was: " + totalPrecipitation + " feet " + "and " + totalPrecipitation + " inches");
  147. System.out.println("The average monthly precipitation for the year was: " + average +" inches");
  148. System.out.println("The month with the least amount of precipitation in inches was in " + EachMonth[0] + " with " + lowest + " inches");
  149. System.out.println("The month with the most amount of precipitation in inches was in " + EachMonth[0] " with " + highest + " inches");
  150. }
  151. }
xa9qqrwz

xa9qqrwz1#

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

  1. public static void main(String[] args) {
  2. Scanner scanner = new Scanner(System.in);
  3. Double monthWiseValues[] = new Double[12];
  4. int year = 0;
  5. String city = "";
  6. String[] eachMonth = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  7. double lowest = 50.0;
  8. int lowestIndex = -1;
  9. double highest = 0.0;
  10. int highestIndex = -1;
  11. double sum = 0;
  12. double average = 0;
  13. double totalPrecipitation = sum;
  14. System.out.println("Enter city name");
  15. city = scanner.next();
  16. do {
  17. System.out.println("Enter four digit number between 1900 and 2025 ");// prompt user for height in feet
  18. year = scanner.nextInt();
  19. if (year < 1900 || year > 2025) {
  20. System.out.println("Error invalid input enter number between 190 and 2025"); //must be positive whole number of tickets
  21. }
  22. } while (year < 1900 || year > 2025);
  23. System.out.println("Enter the precipitation for the following months ");
  24. double inputVal;
  25. for (int monthCount = 0; monthCount < eachMonth.length; monthCount++) {
  26. while (true) {
  27. System.out.print(eachMonth[monthCount].concat(" : "));
  28. inputVal = scanner.nextDouble();
  29. if (inputVal > 50.0 || inputVal < 0.0)
  30. System.out.println("Please enter value in the range [0.0-50.0]");
  31. else {
  32. monthWiseValues[monthCount] = inputVal;
  33. break;
  34. }
  35. }
  36. }
  37. for (int monthIndex = 0; monthIndex < eachMonth.length; monthIndex++) {
  38. if (monthWiseValues[monthIndex] > highest)
  39. highest = monthWiseValues[monthIndex];
  40. highestIndex = monthIndex;
  41. if (monthWiseValues[monthIndex] < lowest)
  42. lowest = monthWiseValues[monthIndex];
  43. lowestIndex = monthIndex;
  44. sum = sum + monthWiseValues[monthIndex];
  45. }
  46. totalPrecipitation = sum;
  47. average = sum / 12;
  48. System.out.println("Precipitation Statistics for " + city + " in " + year + " is:");
  49. for (int monthCount = 0; monthCount < eachMonth.length; monthCount++) {
  50. System.out.println(eachMonth[monthCount] + ":" + monthWiseValues[monthCount]);
  51. }
  52. System.out.println("The total precipitation for the year was: " + getValueInFeetInch(totalPrecipitation));
  53. System.out.println("The average monthly precipitation for the year was: " + getValueInFeetInch(average));
  54. System.out.println("The month with the least amount of precipitation in inches was in " + eachMonth[lowestIndex] + " with " + lowest + " inches");
  55. System.out.println("The month with the most amount of precipitation in inches was in " + eachMonth[highestIndex] + " with " + highest + " inches");
  56. }
  57. private static String getValueInFeetInch(double inch) {
  58. return new StringBuilder(Integer.toString((int)inch / 12))
  59. .append(" feet ")
  60. .append(inch % 12)
  61. .append(" inches.")
  62. .toString();
  63. }
展开查看全部

相关问题