java - Java在读取文件后打印文件的行
问题描述
我只是一个初学者 java 程序员,我不知道如何在读取文件后打印行。
我有一个像这样的文本文件
- 约旦之家 87.4
- 安东尼梅森 76.5
- 奥斯汀图马西 90.5
- 西普里安罗德 66.9
- 达雷尔潘 75.2
我只是想知道如何打印成绩高于 70.30 的行
这是我的代码
public class Question1
{
public static void main (String [] args) throws IOException
{
// Create a Scanner object attached to the keyboard
Scanner in = new Scanner (System.in);
// Write your code here!!!!
System.out.print ("Enter the input filename: " );
String filename = in.nextLine();
// Open the datafile
File file = new File(filename);
// set the delimited to be a ,
Scanner inFile = new Scanner(file);
double avgGrade = 0;
double totalAvgGrade = 0;
int numStu = 0;
String firstName = null;
String lastName = null;
while (inFile.hasNext())
{
firstName = inFile.next();
lastName = inFile.next();
String gradeAvg = inFile.next();
avgGrade = Double.parseDouble(gradeAvg);
totalAvgGrade += avgGrade;
numStu++;
}
double average = totalAvgGrade/numStu;
System.out.printf("%s %.2f \n","Average grade is", average);
System.out.println("Students with grade > average ");
System.out.printf("%s %10s %10s", "First", "Last", "Score");
if ( avgGrade > average)
{
System.out.printf("\n %s %10s %.2f" ,firstName , lastName , avgGrade);
}
}
}
谢谢你
解决方案
我只会使用一个简单的 if 语句。似乎您使用的代码比需要的多。虽然循环在这种情况下效果不佳,但我想你可以使用它们!
If(grades > 70)
{
System.out.println(*code code code*);
}
推荐阅读
- maven - Maven项目中自动化框架组件的结构
- batch-file - 如何记录任务列表的计数
- vertica - 如何使重复的vertica中现有的表列值唯一?
- android - 我的 videoview 说“无法播放视频”,但 webview 可以播放吗?
- asp.net-mvc - 等待的呼叫在单元测试中丢失上下文
- javascript - 用户交互超级事件
- javascript - js如何破坏函数的默认值?
- python - 制作成对的文件和列以在循环中使用?
- selenium - 无法通过 selenium 获取 ul 标签下的所有 li 项
- arrays - 解析数组时如何简化 if..else 语句