while (choice != 6) {
System.out.println(" ");
System.out.println("Rainfall Analysis Menu");
System.out.println("1. Display total rainfall.");
System.out.println("2. Display average daily rainfall.");
System.out.println("3. Display day and amount of greatest rainfall.");
System.out.println("4. Display day and amount of least rainfall.");
System.out.println("5. Display number of days a flood alert was issued.");
System.out.println("6. Quit");
System.out.print("Enter your choice: ");
choice = keyboard.nextDouble();
else if (choice == 4) {
if (rain1 < rain2 && rain1 < rain3 && rain1 < rain4 && rain1 < rain5 && rain1 < rain6 && rain1 < rain7 && rain1 < rain8 && rain1 < rain9 && rain1 < rain10) {
System.out.print("Day 1 had the lowest rainfall with " + rain1 + "inches.");
} else if (rain2 < rain1 && rain2 < rain3 && rain2 < rain4 && rain2 < rain5 && rain2 < rain6 && rain2 < rain7 && rain2 < rain8 && rain2 < rain8 && rain2 < rain9 && rain2 < rain10) {
System.out.print("Day 2 had the lowest rainfall with " + rain2 + " inches.");
} else if (rain3 < rain1 && rain3 < rain2 && rain3 < rain4 && rain3 < rain5 && rain3 < rain6 && rain3 < rain7 && rain3 < rain8 && rain3 < rain9 && rain3 < rain10) {
System.out.print("Day 3 had the lowest rainfall with " + rain3 + " inches.");
} else if (rain4 < rain1 && rain4 < rain2 && rain4 <rain3 && rain4 < rain5 && rain4 < rain6 && rain4 < rain7 && rain4 < rain8 && rain4 < rain8 && rain4 < rain9 && rain4 < rain10) {
System.out.print("Day 4 had the lowest rainfall with " + rain4 + " inches.");
} else if (rain5 < rain1 && rain5 < rain2 && rain5 < rain3 && rain5 < rain4 && rain5 < rain6 && rain5 < rain7 && rain5 < rain8 && rain5 < rain9 && rain5 < rain10) {
System.out.print("Day 5 had the lowest rainfall with " + rain5 + " inches.");
} else if (rain6 < rain1 && rain6 < rain2 && rain6 < rain3 && rain6 < rain4 && rain6 < rain5 && rain6 < rain7 && rain6 < rain8 && rain6 < rain9 && rain6 < rain10) {
System.out.print("Day 6 had the lowest rainfall with " + rain6 + " inches.");
} else if (rain7 < rain1 && rain7 < rain2 && rain7 < rain3 && rain7 < rain4 && rain7 < rain5 && rain7 < rain6 && rain7 < rain8 && rain7 < rain9 && rain7 < rain10) {
System.out.print("Day 7 had the lowest rainfall with " + rain7 + " inches.");
} else if (rain8 < rain1 && rain8 < rain2 && rain8 < rain3 && rain8 < rain4 && rain8 < rain5 && rain8 < rain6 && rain8 < rain7 && rain8 < rain9 && rain8 < rain10) {
System.out.print("Day 8 had the lowest rainfall with " + rain8 + " inches.");
} else if (rain9 < rain1 && rain9 < rain2 && rain9 < rain3 && rain9 < rain4 && rain9 < rain5 && rain9 < rain6 && rain9 < rain7 && rain9 < rain8 && rain9 < rain10) {
System.out.print("Day 9 had the lowest rainfall with " + rain9 + " inches.");
} else if (rain10 < rain1 && rain10 < rain2 && rain10 < rain3 && rain10 < rain4 && rain10 < rain5 && rain10 < rain6 && rain10 < rain7 && rain10 < rain8 && rain10 < rain9) {
System.out.print("Day 10 had the lowest rainfall with " + rain10 + " inches.");
}
我有一个程序,提示用户输入他们想要分析的天数,然后输入那些天(1-10天)的降雨量。如果用户想要找到最低的降雨量,他们将4号输入到应该打印的节目中“Day x的降雨量最低,为x英寸。我想出了上面的解决方案,但想要一种方法来简化它它简单而短。
答案 0 :(得分:0)
将所有变量存储在数组中:
int[] rain = new int[]{1, 2, 15, 4,6 ...}
// note that 1,2,15 (ecc..) are example values to use instead of rain1, rain2 ...
然后找到数组中的最小值及其索引:
int min = Arrays.stream(rain).min().getAsInt();
int index = Arrays.asList(rain).indexOf(min);
// you can also write yourself some simple functions to get the those values
然后打印出来:
System.out.println("Day " + index + "had the lowest rainfall with " + min + " inches.");
答案 1 :(得分:0)
如何对TreeMap进行排序,使其自身排序:
TreeMap rainfall = new TreeMap<Integer, Integer>(); // First integer is rainfall amount and second integer is day number
rainfall.put(2, 1);
rainfall.put(8, 2);
rainfall.put(4, 3);
//etc
System.out.println("Day " + rainfall.get(1) + "had the lowest rainfall with " + rainfall.firstKey() + " inches.");
答案 2 :(得分:0)
使用数据结构来保存您的数据,因此您不必手动硬编码所有可能性。您可以使用包装类来压缩代码,以保存当天和降雨量的信息:
public class RainfallData{
private int day;
private int rainfall;
public RainfallData(int day, int rainfall){
this.day = day;
this.rainfall = rainfall;
}
//getters not shown
}
然后使用arraylist等数据结构来保存降雨记录:
ArrayList<RainfallData> records = new ArrayList<>();
RainfallData minData = records.get(0);
for(RainfallData r : records)
if(r.getRainfall() < minData)
minData = r;
System.out.print("Day " + minData.getDay() + " had the lowest rainfall with " + minData.getRainfall() + " inches.");
通过这样的动态设置,天数的变化或属性数量的变化对当前代码的影响最小。