我目前有一些可以接受控制台输入的代码,但我需要让它识别名称和测试分数列表。 我目前拥有的代码:
import java.io.*;
import utils.io.*;
class student
{
String name;
double sResult[];
int result;
double sum;
void getdata()
{
System.out.println("Enter name:");
Name = (string) system.in.read();
int index=0;
for (int counter=1; counter<=5; counter++)
{
System.out.println("Enter result for subject"+counter+":");
sResult[count] = (double) system.in.read();
}
}
public static void CalculateAverage()
{
sum = 0;
for (int i=1;i<=5;i++)
{
sum += sResult[i];
}
return (int) Math.round(sum/(values.length-1));
}
Public static char calculateGrade()
{
result=sum;
if (result>=0 && result <=59)
{
return ('F');
}
else
if (result >=60 && result<=69)
{
return ('E');
}
else
if (result>=0 && result<79)
{
return ('D');
}
else
if (result >=70 && result<=79)
{
return ('C');
}
else
if (result>=80 && result<=89)
{
return ('B');
}
else
if (result>=90 && result<=100)
{
return ('A');
}
}
}
和班级考试
public class test
{
public static void main(String[] args)
{
Student std;
do
{
std=new Student();
std.getdata();
System.out.println("Student Name:"+std.Name);
System.out.println("Average for"+std.Name+" "+"is:"+std.average());
System.out.println("Grade for"+std.Name+" "+"is:"+std.gradecal());
System.out.println("Want to continue (1-Yes,2-No");
}
while(System.in.read()==1);
}
文本文档格式为name score1 score2 score3 score4 score5
我只需要帮助搞清楚如何导入值,然后我就可以弄清楚如何使用PrintWriter重写.txt。
提前感谢您的帮助!
答案 0 :(得分:0)
首先,你不应该有以大写字母开头的变量名,即:
string Name;
只有班级名称应以大写字母开头。
另外,我甚至不确定你的代码是否编译。您system.out.println
所在的任何地方都应该是System.out.println
。请注意,System是一个类,因此第一个字母是大写
要阅读数据,您可以使用BufferedReader
进行编写,您可以使用BufferedWriter
。
用法:
BufferedReader in = new BufferedReader(new FileReader("FileName.txt"));
BufferedWriter out = new BufferedWriter(new FileWriter("OutputFileName.txt"));
in.readLine(); //This reads a single line from the text file
out.write("Your Output Text");
in.close(); // Closes the file input stream
out.close(); // Closes the file output stream
因为您正在阅读以下格式:
名称得分1得分2得分3得分4得分5
你可以使用一些String函数来获取每个字段。
String inputLine = in.readLine();
String [] fields = inputLine.split(" "); // Splits at the space
System.out.println(fields[0]); //prints out name
System.out.println(fields[1]); //print out score1
...
答案 1 :(得分:0)
以下是Scanner
的使用方法。
Scanner inputFile = new Scanner(new File("inputfile.txt"));
while(inputFile.hasNext()){ //if there is still something to read
String name = inputFile.next();
int score1 = inputFile.nextInt();
....
int score5 = inputFile.nextInt();
//Do something here with the scores
inputFile.next(); //Read the new line character and prepare for the next iteration
}
要写回文件,您可以查看Davy的答案并使用Buffered Writer。请注意,write()
方法的工作方式与System.out.println()
类似,但您需要自行打印\n
才能转到新行。
答案 2 :(得分:0)
public static void main(String... args) throws IOException {
Scanner s = new Scanner(new BufferedReader(new FileReader("textfile.txt")));
while(s.hasNext()){
Student student = new Student(s.next(), s.nextDouble(), s.nextDouble(),
s.nextDouble(), s.nextDouble(), s.nextDouble() ));
// Do something with student
s.next() // consume endline
}
}