gpt4 book ai didi

java - 从最低到最高打印数组而不排序

转载 作者:行者123 更新时间:2023-12-01 10:33:37 25 4
gpt4 key购买 nike

首先我想说我经验不足,如果有人回答这个问题,我很抱歉。我有一段时间一直试图寻找答案,但一直未能找到。

我正在开发一个项目,用户将数字输入到数组中。这些数字代表不同日期的温度。天显然是数组中的位置。我需要找到一种方法来打印从最低到最高的温度而不对数组进行排序。

因此,如果用户输入 [56, 45, 67, 41, 59, 70],则意味着位置 0(第 1 天)为 56 度,位置 2(第 3 天)为 67 度。我需要保持数组的位置相同,以便在打印出来时,日期仍保留在临时值中。

编辑:我已附上到目前为止我的项目中的代码。 HighestOrdered 方法是我不知道该做什么或从哪里开始的方法。对于我上面所说的 HighestOrdered 方法,我需要让它打印出当天的温度(数组中的位置),但我不知道该怎么做。

这是我到目前为止的代码:

public class Weather {

public static void main(String[] args) {
// TODO Auto-generated method stub

int [] high = new int[30];
int [] low = new int[30];

Init (high);
Init(low);


LoadData(high,low);
Report(high, low);

FindAvg(high,low);
Lowest(high, low);
Highest(high,low);
}
public static void Init(int A[])
{
for(int i = 0; i < A.length; i++)
{
A[i] = 510;
}
}

public static void Report(int[] H, int[] L)
{
System.out.println("Day High Low");

for(int i = 0; i < H.length; i++)
{
System.out.println(i + " " + H[i] + " " + L[i]);
}
}
public static void LoadData(int[] H, int[] L)
{

int day = 0;
while(day < 30)
{
try {
int high = Integer.parseInt(JOptionPane.showInputDialog("please enter the high"));
H[day] = high;
} catch (NumberFormatException e) {
}
try {
int low = Integer.parseInt(JOptionPane.showInputDialog(" Please enter the low"));
L[day] = low;
} catch (NumberFormatException e) {
}
day++;

}
}
public static void FindAvg(int[] H, int[] L){

int sumHigh = 0;
int avgHigh;

int sumLow = 0;
int avgLow;

for(int i : H)
sumHigh += i;
avgHigh = sumHigh/H.length;

for(int i : L)
sumLow += i;
avgLow = sumLow/L.length;

System.out.println("The average for the high is: " + avgHigh);
System.out.println("The average for the low is: " + avgLow);
}
public static void Highest(int[] H, int[] L)
{
int highestHigh = -1000;
int dayHigh = 0;

int highestLow = -1000;
int dayLow = 0;

for(int i = 0; i < H.length; i++)
{
if(H[i] > highestHigh && H[i] != 510)
{
highestHigh = H[i];
dayHigh = i;
}
}
System.out.println("\n" + "The highest high is: " + highestHigh + " degrees." + "\n" +
"This temperature was recorded on day: " + dayHigh);

for(int i = 0; i < L.length; i++)
{
if(L[i] > highestLow && L[i] != 510)
{
highestLow = L[i];
dayLow = i;
}
}
System.out.println("\n" + "The highest low is: " + highestLow + " degrees." + "\n" +
"This temperature was recorded on day: " + dayLow);
}

public static void Lowest(int[] H, int[] L)
{

int lowestHigh = 1000;
int dayHigh = 0;

int lowestLow = 1000;
int dayLow = 0;

for(int i = 0; i < H.length; i++)
{
if(H[i] < lowestHigh)
{
lowestHigh = H[i];
dayHigh = i;
}
}
System.out.println("\n" + "The lowest high is: " + lowestHigh + " degrees." + "\n" +
"This temperature was recorded on day: " + dayHigh);

for(int i = 0; i < L.length; i++)
{
if(L[i] < lowestLow)
{
lowestLow = L[i];
dayLow = i;
}
}
System.out.println("\n" + "The lowest low is: " + lowestLow + " degrees." + "\n" +
"This temperature was recorded on day: " + dayLow);
}

public void HighestOrdered(int[] H)
{

}
}

最佳答案

这是一个开始。

从你的数组中,创建一个排序的映射,例如

Map<Integer,Integer> mymap = new TreeMap<Integer,Integer> .

您将使用 temp 作为键,使用 day 作为值。例如,根据您的示例数据,

myMap.put(56,1);
myMap.put(45,2);

(注意 - 在实际代码中,您将迭代数组以放置值。)

然后您可以迭代 myMap 中的键和值(或条目)。

关于java - 从最低到最高打印数组而不排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34940588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com