当前位置: 首页 > 面试题库 >

如何确定从Java中的文本文件读入的最大值和最小值

葛永丰
2023-03-14
问题内容

我正在上课的作业,正在寻找一些有用的指示,而不是完整的解决方案。基本上,我必须编写一个Java程序,该程序读取一个文本文件并逐行列出信息,列出行号,最后打印出最大值和最小值以及与每个值相关的年份。文本文件包含一年和该年的温度。因此,它列出了类似“
1900
50.9”的内容。我不是要使用数组或扫描仪,这是分配的一部分。我已经能够成功地使程序逐行打印并逐行打印出相应的温度。有人告诉我,我确实使用了while循环。现在,我唯一的问题是以某种方式可以区分所有温度的方式访问文本文件,最大值和最小值,以及每个年份发生的年份。到目前为止,我一直没有寻求帮助,因为我希望自己自行解决,但这项工作不再值得任何功劳到后期罚款。任何帮助将不胜感激,因为我仍然想解决这个问题。谢谢。

这就是我所拥有的。

public class main {

/**
 * @param args the command line arguments
 */

public static void main(String[] args) throws Exception {


File temps = new File ("temps.txt"); //Creates path to temps.txt file
FileReader textReader = new FileReader (temps); //Input information from temps.txt file into file reader
BufferedReader kb = new BufferedReader (textReader); //Use buffered reader to hold temps.txt file info from the file reader




String tempList; //Create string variable named tempList
int lineCount = 0; //Create integer variable named lineCount
String sep = ": Temp "; //Create string variable named sep (short for separation) and set it equal to the literal string ":"
String space = " "; //Create string variable named space and set it equal to an actual space between texts


System.out.println("The following is the provided information from the file input. ");
while ((tempList = kb.readLine()) !=null) { //while loop stating that as long as the text file still has values to read (is not null), continue to execute


    System.out.println("Line " + lineCount++ + ": Year " + tempList.replace(space, sep)); //Prints out the line number (lineCount++), the info from the temps.txt file with a ":" between the year and the number (tempList.replace (space,sep)

}




}

}

到目前为止的输出是这样的:

Line 0: Year 1900: Temp 50.9
Line 1: Year 1901: Temp 49
Line 2: Year 1902: Temp 49.7
Line 3: Year 1903: Temp 49.5
Line 4: Year 1904: Temp 47.1
Line 5: Year 1905: Temp 49.1

等等…

Line 99: Year 1999: Temp 52.7
BUILD SUCCESSFUL (total time: 0 seconds)

问题答案:

这是一种实现html" target="_blank">方法:

String tempList; //Create string variable named tempList
int lineCount = 0; //Create integer variable named lineCount
String sep = ": Temp "; //Create string variable named sep (short for separation) and set it equal to the literal string ":"
String space = " "; //Create string variable named space and set it equal to an actual space between texts

String maxValueYear = "";
String minValueYear = "";
double maxValue = 0;
double minValue = Double.MAX_VALUE;
System.out.println("The following is the provided information from the file input. ");
while ((tempList = kb.readLine()) !=null) { //while loop stating that as long as the text file still has values to read (is not null), continue to execute

    String year = tempList.substring(0, tempList.indexOf(space));
    double temp = Double.valueOf(tempList.substring(tempList.indexOf(space), tempList.length()));

    if (temp > maxValue) {
        maxValue = temp;
        maxValueYear = year;
    }
    if (temp < minValue) {
        minValue = temp;
        minValueYear = year;
    }
    System.out.println("Line " + lineCount++ + ": Year " + tempList.replace(space, sep)); //Prints out the line number (lineCount++), the info from the temps.txt file with a ":" between the year and the number (tempList.replace (space,sep)

}

System.out.println("The minimum temp occured in year " + minValueYear + " and was " + minValue);
System.out.println("The maximum temp occured in year " + maxValueYear + " and was " + maxValue);


 类似资料:
  • 我有一个家庭作业,要求我使用输入对话框要求最终用户输入一个数字,将该字符串解析为int,然后使用确认对话框询问用户是否要输入另一个数字。如果单击“是”,程序将循环,一旦单击“否”,它将获取所有输入的数字,并确定最小值和最大值。如何跟踪所有输入的数字,而不声明多个变量来保存它们(因为从技术上讲,用户可以输入无限数量的数字)? 我对编程很陌生。我一直在搜索我的教科书,我浏览了堆栈溢出来找到我的具体问题

  • 我的代码要求用户输入动物物种的值,然后将其显示给他们。我只需要最后添加一个部分,它也会告诉用户最濒危的动物(输入的数字最低)。我已经环顾了一些地方,并使用x进行了triec

  • 问题内容: 我的代码没有给出错误,但是没有显示最小值和最大值。代码是: 我是否需要system.out.println()来显示它,否则返回应该起作用吗? 问题答案: 您正在调用方法,但不使用返回的值。

  • 如何在csv文件中查找每列的最小值和最大值(albhabet值除外)。 我所做的是迭代每一行,并将每一列存储在一个hashmap中 然后我算了一下 但是当大数据到来时,在hashmap中保存那么多数据并不是更好,然后找到最小值和最大值。我如何用其他方式找到最小值/最大值。 最新消息 没有得到预期的结果。 解决方案:计算csv文件中列的最小值和最大值

  • 我试图找到每一行CSV文件的MIN和MAX值,并将它们附加到列表中的下一个位置,位置5和6。我已经设法计算了平均值,将其附加到第四个位置,并将其从最高到最低输出。然而,我正在努力解决如何找到每行的MAX和MIN值,以便我可以做同样的操作——从最高到最低。原始CSV的格式是:弗雷德,56,78,99,每个用户在新行上。 任何帮助都将不胜感激。

  • 本文向大家介绍如何确定R数据帧列中具有最小值和最大值的行?,包括了如何确定R数据帧列中具有最小值和最大值的行?的使用技巧和注意事项,需要的朋友参考一下 在数据分析中,我们经常需要确定最小值和最大值,因为这些值有助于我们理解所考虑的列或变量的限制。可以通过在单个方括号中使用which.max表示最大值,并使用which.min表示最小值来提取行。 示例 请看以下数据帧- 输出结果 确定具有特定列的最