过去3个小时我一直在做家庭作业,我没有取得很大的进步。我想知道是否有人可以帮我推动我朝着正确的方向前进。
分配是从文件中读取数据并将其转换为更易读取的内容。数据文件如下所示:
0.30 0.30 0.40
161
3333 70 60 50
4444 50 50 50
5555 80 90 80
0
162
1212 90 85 92
6666 60 80 90
7777 90 90 90
8888 95 87 93
9999 75 77 73
0
263
2222 90 65 75
8989 60 40 60
9090 70 80 30
0
该文件包含5种不同类型的数字 这三位数字是课程编号 这四位数字是学号。
我应该读取文件中的所有数据然后格式化并输出它以便于阅读:
Grade Data For Class 161
ID Programs Midterm Final Weighted Average Programs grade
-- -------- ------- ----- ---------------- --------------
3333 70 60 50 59.00 Pass
4444 50 50 50 50.00 Fail
5555 80 90 80 83.00 Pass
Class Average: 64.00
我有扫描仪设置,并且能够解析文本中的数字,我的问题是它们都被保存为字符串,因此我无法对它们进行任何数学检查。我开始怀疑这是否是最好的解决方法。
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class classAverage
{
public static void main(String[] args) throws IOException
{
//in variable equals entire file
Scanner in = new Scanner(new File("courseData.txt"));
int classID;
System.out.println("ID Programs Midterm Final Weighted Average Programs Grade");
System.out.println("-- -------- ------- ----- ---------------- --------------");
while(in.hasNextLine()) //While file has new lines
{
//line equals each line of text
String line = in.nextLine();
Scanner lineParser = new Scanner(line);
System.out.println(line);
for(; lineParser.hasNext();)
{
//number = each number
String number = lineParser.next();
System.out.println(number);
if(number < 1 && number > 0)
{
double programsAverage = number.nextDouble();
double midtermAverage = number.nextDouble();
double finalAverage = number.nextDouble();
System.out.println(programsAverage);
System.out.println(midtermAverage);
System.out.println(finalAverage);
}
}
}
}
}
更新我已经包含了我的更新代码。我现在的问题是我的陈述中的条件。这些应该检查传入的扫描仪数据的值,以查看数据是否:
- 重量计算器(0,1)
- 得分(1,100),
- 一个classNumber(100,400),
- studentNumber(1000,9999),
- 类分隔符(0)。
我想的是:
for(in.next(); in&lt; 100&amp;&amp; in&gt; 1; next());
但这并不是很好。
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
/**
* Write a description of class classAverage here.
*
* @author
* @version
*/
public class classAverage
{
public static void main(String[] args) throws IOException
{
//in variable equals entire file
Scanner in = new Scanner(new File("courseData.txt"));
int classNumber;
int studentNumber;
int programs;
int midterm;
int finals;
double programWeight = in.nextDouble();
double midtermWeight = in.nextDouble();
double finalWeight = in.nextDouble();
//System.out.println(programWeight + " " + midtermWeight + " " + finalWeight);
for(int k = 0; k < 3; k++)
{
for(int i = 0; i <= 0; i++)
{
classNumber = in.nextInt();
System.out.println("Grades for class: " + classNumber);
System.out.println(" ID Programs Midterm Final Weighted Average Programs Grade");
System.out.println(" -- -------- ------- ----- ---------------- --------------");
}
int studentCount = 0;
double sumAverage = 0.0;
for(int j = 0; j <= 2; j++)
{
studentNumber = in.nextInt();
programs = in.nextInt();
midterm = in.nextInt();
finals = in.nextInt();
studentCount++;
double weightedAverage = (programWeight * programs) + (midtermWeight * midterm) + (finalWeight * finals);
sumAverage += weightedAverage;
System.out.printf("%d %d %d %d %.2f ", studentNumber,programs,midterm,finals,weightedAverage);
if(programs >= 70)
{
System.out.print("PASS");
} else {
System.out.print("FAIL");
}
System.out.println();
}
double classAverage = sumAverage / studentCount;
System.out.printf("Class average is: %.2f", classAverage);
System.out.println("\n\n");
}
}
}
答案 0 :(得分:1)
这里有2个选项:
使用
Scanner#hasNextInt()
和nextInt()
代替nextLine()
直接阅读Integers
,而不是 String 格式的数字。- 醇>
保持阅读/扫描代码不变。使用
Integer.parseInt()
将 String转换为整数。 - &GT;首选解决方案因为它更有效率。