完全不知道如何做到这一点。我想要做的是在输出中放置最小值或最大值最小的城市。我的理解是,你不能在一个方法中抛出一个字符串和另一个数据类型。我怎么能把名字和最低温度匹配起来呢?
假设我想要3个城市:
我想把数组设为3:
然后我将添加以下城市,(亚特兰大,纽约,里士满)
城市温度分别为(42.2,98.8,-12.4)
最小值为-12.4
Max是98.8
我有,我如何将存储在数组[2]中的Richmond's String和存储在数组[2]中的temperature's double连接起来?非常感谢任何帮助。
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.lang.Math;
public class Ex9
{
public static void main(String[] args)
{
String message ="";
double min = 0, max = 0, avg = 0;
int counter = 1;
int numberOfCities = Integer.parseInt(JOptionPane.showInputDialog(null, "How many cities would you like to enter?"));
String[] nameOfCities = new String[numberOfCities];
double[] temperatureOfCities = new double[numberOfCities];
for (int i = 0; i < nameOfCities.length; i++)
{
nameOfCities[i] = JOptionPane.showInputDialog(null, "Please enter the name of city " +counter+" :");
temperatureOfCities[i] = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the current temperature of the city " + counter +" :"));
message += "City name " + nameOfCities[i] + ".n"
+ "Temperature of city " + temperatureOfCities[i] + " is degreesn";
counter++;
}//end numberOfCities loop
if(
JOptionPane.showMessageDialog(null, message + "nThe average temperature is " +findAvg(temperatureOfCities)+ "n[Name of city] has the lowest temperature, which is " + findMin(temperatureOfCities) + "n[Name of city] has the highest temperature, which is " + findMax(temperatureOfCities));
}//end main
public static double findAvg(double[] temperatureOfCities)
{
double sum =0;
for(int i=0;i<temperatureOfCities.length;i++)
{
sum += temperatureOfCities[i];
}
sum = sum/temperatureOfCities.length;
return sum;
}//end findAvg
public static double findMin(double[] temperatureOfCities)
{
double min=0;
for(int i =0; i <temperatureOfCities.length;i++)
{
if (temperatureOfCities[i] <= temperatureOfCities[0])
{
min = temperatureOfCities[i];
}
}//end for loop
return min;
}//end findMin
public static double findMax(double[] temperatureOfCities)
{
double max=0;
for(int i =0; i <temperatureOfCities.length;i++)
{
if (temperatureOfCities[i] >= temperatureOfCities[0])
{
max = temperatureOfCities[i];
}
}//end for loop
return max;
}//end findMax
}//end program
这里有两种主要方法:
1)过程式方法——只传递两个数组而不是一个数组。如果它们保持同步,那就没有问题——只要为它们使用相同的索引就可以了。
2)面向对象的方法-定义一个类TemperatureReading与双温度和字符串cityName。然后你可以创建一个temperataturereading[]数组并传递它,这样数据自然就关联起来了。
更改findMin, findAvg和findMax方法以返回一个复合测量对象。
class Measurement {
final double temperature;
final String cityName;
Measurment(String cityName, double temperature)
{
this.temperature = temperature;
this.cityName = cityName;
}
}
更新后的方法看起来像这样:
public static Measurement findMax(String[] nameOfCities, double[] temperatureOfCities) {
double maxTemp=0;
String maxName=null;
for(int i =0; i <temperatureOfCities.length;i++)
{
if (temperatureOfCities[i] >= temperatureOfCities[0])
{
maxTemp = temperatureOfCities[i];
maxName = nameOfCities[i];
}
} //end for loop
return new Measurement(maxTemp, maxName);
}
现在您可以这样使用结果:
Measurement maxMeasurement = findMax(nameOfCities, temperatureOfCities);
System.out.println(maxMeasurement.cityName + "has a temperature of " + maxMeasurement.temperature);