栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

这是一种实现方法:

String tempList; //Create string variable named tempListint lineCount = 0; //Create integer variable named lineCountString 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 textsString 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);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455304.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号