我不确定如何同时显示一个字符串和一个整数数组,以便它显示特定月份的数组的最大值
Global:
static double[] set2014 = new double[6];
static String[] months = new String[6];
最大值的计算方法如下:
public static void max(){
initialise();
double max = set2014[0];
for(int i = 1; i < set2014.length; i++){
if(set2014[i] > max){
max = set2014[i];
}
}
System.out.println("------------------");
System.out.println("Largest figure is " + max);
}
例如,输出将是: 最大的数字是: 204566 年 3 月
最佳答案
维护两个由索引链接的数组就是我所说的对象拒绝。考虑创建一个包含月份和值的类。
public interface MonthValue { //class or interface, I just didn't want to type out the simple implementation
String getMonth();
double getDouble();
}
//set2014 now needs to contain MonthValues
MonthValue max = set2014[0];
for(int i = 1; i < set2014.length; i++){
MonthValue current = set2014[i];
if(current.getValue() > max.getValue()){
max = current;
}
}
System.out.println("Largest figure is " + max.getValue());
System.out.println("In month " + max.getMonth());
但要按原样回答你的问题:
您可以改为跟踪索引:
int maxMonthIndex = 0;
for(int i = 1; i < set2014.length; i++){
if(set2014[i] > set2014[maxMonthIndex]){
maxMonthIndex = i;
}
}
System.out.println("Largest figure is " + set2014[maxMonthIndex]);
System.out.println("In month " + months[maxMonthIndex]);
顺便说一下,set
是一个不好的数组名称。集合是无序的集合,数组是有顺序的。
关于java - 一次显示 String 和 Int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40787720/