Java 带循环的输入和输出
我需要写一个程序,从输入文件中获取3个学生信息。根据扫描的学分数确定他们是全日制学生还是兼职学生后,计算学费,然后将他们的所有信息输出到输出文件。到目前为止,我很有信心大部分代码都能正常工作,但我遇到了一个问题 我们班刚刚复习了有关循环的材料,我需要确保代码使用的就是这样的循环。到目前为止,我使用while循环扫描整个文件,直到结束。虽然它确实获得了所需的学生信息。它只打印最后一个,所以它显然只得到1个总数,然后用下一个部分覆盖同一个。这是代码 我或多或少只需要输入和输出方面的帮助 至于输入文件,如下所示 Dom PilorussoJava 带循环的输入和输出,java,loops,input,Java,Loops,Input,我需要写一个程序,从输入文件中获取3个学生信息。根据扫描的学分数确定他们是全日制学生还是兼职学生后,计算学费,然后将他们的所有信息输出到输出文件。到目前为止,我很有信心大部分代码都能正常工作,但我遇到了一个问题 我们班刚刚复习了有关循环的材料,我需要确保代码使用的就是这样的循环。到目前为止,我使用while循环扫描整个文件,直到结束。虽然它确实获得了所需的学生信息。它只打印最后一个,所以它显然只得到1个总数,然后用下一个部分覆盖同一个。这是代码 我或多或少只需要输入和输出方面的帮助 至于输入文件
1037沃特福德法院
宾夕法尼亚州佳能斯堡15317
C937493021
15
Dan Madeupname
西达尔巷106号
宾夕法尼亚州麦克默里15317
C927012312
11
史蒂夫·阿诺德
枫树路281号
宾夕法尼亚州佳能斯堡15317
C482716209
9
公共课程4{
//为全日制和非全日制学生设定价格。
静态最终双学费/学分=276.00;
静态最终双倍费用/学分=15.00;
静态最终双重服务每项信用=7.09;
静态最终双全日制学费=3311.00;
静态最终双倍全职费用=184.00;
静态最终双全职服务=85.00;
公共静态void main(字符串[]args)引发FileNotFoundException
{
int i;
字符串firstName=null;
字符串lastName=null;
字符串accountNumber=null;
双积分=0;
字符串地址1;
字符串地址,地址2,地址3,地址4,地址5,地址6;
字符串文件名;
双倍学费=0;
双重费用=0;
双倍合计=0;
字符串格式费用;
字符串格式总计;
字符串格式;
//创建名为infle的扫描仪对象,并将其指定为文件input.dat
fileName=JOptionPane.showInputDialog(“请输入输入文件名”);
Scanner infle=新扫描仪(新文件读取器(文件名));
//创建与文件output.dat关联的名为outFile的PrintWriter对象
PrintWriter outFile=新的PrintWriter(“tutionandfees.dat”);
//用于循环输入直到文件结束。
while(infle.hasNext())
{firstName=infle.next();
lastName=infle.next();
地址=infle.next();
address2=infle.next();
address3=infle.next();
address4=infle.next();
address5=infle.next();
address6=infle.next();
accountNumber=infle.next();
creditsTaken=infle.nextDouble();
}
//If Else语句确定该学生是兼职还是全日制学生,然后计算他们的账单。
如果(信用等级<12)
{学费=学费/学分*学分;
费用=(每笔信贷的费用+每笔信贷的服务)*creditsTaken;
总额=学费+手续费;
}
其他的
{
学费=全日制学费;
费用=全职费用+全职服务;
总额=学费+手续费;
}
formatTotal=String.format(“%.2f”,总计);
formatFees=String.format(“%.2f”,费用);
FormatTursey=String.format(“%.2f”,学费);
//输出到文件的所有信息,需要修复。
outFile.println(“学费账单报告”);
outFile.printf(“CWID\t\t”+“Name\t\t”+“Credits\t\t”+“tutory\t\t”+“Fees\t\t”+“Total%n”);
outFile.printf(accountNumber+“\t”+firstName+“+lastName+”\t”+creditsTaken+“\t\t”+FormatTursey+“\t\t”+formatFees+“\t\t”+formatTotal);
infle.close();
outFile.close();
showMessageDialog(null,“程序保存在tutionandfees.dat中”);
}
}
当您正确浏览文件时,您只创建了每个变量中的一个,并且在文件内部创建了一个变量,但是您一直在为变量赋值并覆盖以前的值
您有3种选择:
1-为每个变量创建一系列数组,并在while中赋值(这有点糟糕的结构化编程)
2-在创建实例并将每个实例分配给数组位置(学生的数组)时,创建一个类,该类用所有变量和内部表示学生实体
3-你阅读每一行并进行你想要的处理(有时这会使它变得有点困难,因为你可能需要很多os累加器和辅助变量)
我将使用选项2。在现有代码中,只有在完全读取输入文件后才执行写入。这就造成了问题 解决方案:
读取一个学生的数据,计算该学生的数据并将其写入输出文件。
对所有学生重复相同的步骤。
使用逐行方法处理数据 不要将所有学生的数据存储在数组中,因为这样会增加内存使用量,并且在将数据写入输出文件后,不会在程序中进一步使用这些数据 修改的主要方法如下:
public static void main(String[] args) throws FileNotFoundException {
String firstName = null;
String lastName = null;
String accountNumber = null;
double creditsTaken = 0;
String address1;
String address, address2, address3, address4, address5, address6;
String fileName;
double tuition = 0;
double fees = 0;
double total = 0;
String formatFees;
String formatTotal;
String formatTuition;
// create a scanner object named inFile and assign it the file input.dat
fileName = JOptionPane
.showInputDialog("Please enter the input file name. ");
Scanner inFile = new Scanner(new FileReader(fileName));
// create a PrintWriter object named outFile associated with the file
// output.dat
PrintWriter outFile = new PrintWriter("tuitionAndFees.dat");
// Print headers in output file
outFile.println("Tuition Billing Report ");
outFile.printf("CWID\t\t" + "Name\t\t" + "Credits\t\t" + "Tuition\t\t"
+ "Fees\t\t" + "Total%n");
// Intended to loop the input until the end of the file.
while (inFile.hasNext()) {
firstName = inFile.next();
lastName = inFile.next();
address = inFile.next();
address2 = inFile.next();
address3 = inFile.next();
address4 = inFile.next();
address5 = inFile.next();
address6 = inFile.next();
accountNumber = inFile.next();
creditsTaken = inFile.nextDouble();
// If Else statement to determine if the student is a part time or
// full time student, and then calculates their bill.
if (creditsTaken < 12) {
tuition = TUITION_PER_CREDIT * creditsTaken;
fees = (FEE_PER_CREDIT + SERVICE_PER_CREDIT) * creditsTaken;
total = tuition + fees;
}
else {
tuition = FULL_TIME_TUITION;
fees = FULL_TIME_FEE + FULL_TIME_SERVICE;
total = tuition + fees;
}
formatTotal = String.format("%.2f", total);
formatFees = String.format("%.2f", fees);
formatTuition = String.format("%.2f", tuition);
outFile.printf(accountNumber + "\t" + firstName + " " + lastName
+ "\t" + creditsTaken + "\t\t" + formatTuition + "\t\t"
+ formatFees + "\t\t" + formatTotal+"\n");
}
inFile.close();
outFile.close();
JOptionPane.showMessageDialog(null,
"The program was saved in tuitionAndFees.dat");
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
字符串firstName=null;
字符串lastName=null;
字符串accountNumber=null;
双积分=0;
字符串地址1;
字符串地址,地址2,地址3,地址4,地址5,地址6;
字符串文件名;
双倍学费=0;
双重费用=0;
双倍合计=0;
字符串格式费用;
字符串格式总计;
字符串格式;
//创建名为infle和assi的扫描仪对象
public static void main(String[] args) throws FileNotFoundException {
String firstName = null;
String lastName = null;
String accountNumber = null;
double creditsTaken = 0;
String address1;
String address, address2, address3, address4, address5, address6;
String fileName;
double tuition = 0;
double fees = 0;
double total = 0;
String formatFees;
String formatTotal;
String formatTuition;
// create a scanner object named inFile and assign it the file input.dat
fileName = JOptionPane
.showInputDialog("Please enter the input file name. ");
Scanner inFile = new Scanner(new FileReader(fileName));
// create a PrintWriter object named outFile associated with the file
// output.dat
PrintWriter outFile = new PrintWriter("tuitionAndFees.dat");
// Print headers in output file
outFile.println("Tuition Billing Report ");
outFile.printf("CWID\t\t" + "Name\t\t" + "Credits\t\t" + "Tuition\t\t"
+ "Fees\t\t" + "Total%n");
// Intended to loop the input until the end of the file.
while (inFile.hasNext()) {
firstName = inFile.next();
lastName = inFile.next();
address = inFile.next();
address2 = inFile.next();
address3 = inFile.next();
address4 = inFile.next();
address5 = inFile.next();
address6 = inFile.next();
accountNumber = inFile.next();
creditsTaken = inFile.nextDouble();
// If Else statement to determine if the student is a part time or
// full time student, and then calculates their bill.
if (creditsTaken < 12) {
tuition = TUITION_PER_CREDIT * creditsTaken;
fees = (FEE_PER_CREDIT + SERVICE_PER_CREDIT) * creditsTaken;
total = tuition + fees;
}
else {
tuition = FULL_TIME_TUITION;
fees = FULL_TIME_FEE + FULL_TIME_SERVICE;
total = tuition + fees;
}
formatTotal = String.format("%.2f", total);
formatFees = String.format("%.2f", fees);
formatTuition = String.format("%.2f", tuition);
outFile.printf(accountNumber + "\t" + firstName + " " + lastName
+ "\t" + creditsTaken + "\t\t" + formatTuition + "\t\t"
+ formatFees + "\t\t" + formatTotal+"\n");
}
inFile.close();
outFile.close();
JOptionPane.showMessageDialog(null,
"The program was saved in tuitionAndFees.dat");
}