gpt4 book ai didi

java - 显示一年中的最低平均价格,以及该价格的周数以及该价格发生的月份名称

转载 作者:行者123 更新时间:2023-12-02 10:01:23 27 4
gpt4 key购买 nike

问题是将文件加载到并行数组中,然后显示一年中的最低价格和最高价格,以及该价格的周数以及该价格发生的月份名称。我能够打印出最低和最高价格,但我一直困惑于如何显示该价格的月份和日期请提供任何帮助..

这是我的代码

package `javaapplication98`;

import java.io.File;

import `java.io.FileNotFoundException;`
import java.util.Scanner;



public class JavaApplication98

{


public static void main(String[] args) throws FileNotFoundException

{
File file= new File("D:\\Gas.txt");
Scanner inputfile= new Scanner(file);

String [] months= new String[35];
String [] days= new String[35];
int [] years= new int[35];
double [] prices= new double[35];
int index=0;
double max=prices[0];


while(inputfile.hasNext())
{
months[index]=inputfile.next();
days[index]=inputfile.next();
years[index]=inputfile.nextInt();
prices[index++]=inputfile.nextDouble();
}
for (int x=1; x<prices.length; x++)
{
if (prices[x]>max)
max=prices[x];
}
System.out.println(max);
}

}

最佳答案

使用另一个变量来存储 pricesmax 的当前索引。然后,最终当你从数组中找到最大值时,你就会得到它的索引。

您可以使用与 max 存储的索引相同的索引来访问月份和日期。

关于java - 显示一年中的最低平均价格,以及该价格的周数以及该价格发生的月份名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55604019/

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