gpt4 book ai didi

java - 带循环的输入和输出

转载 作者:行者123 更新时间:2023-12-01 13:43:52 25 4
gpt4 key购买 nike

我需要编写一个程序,从输入文件中获取 3 个学生信息。根据扫描的学分确定他们是全日制还是非全日制学生后计算学费,然后将他们的所有信息输出到输出文件。到目前为止,我非常有信心大多数代码都能正常工作,但我遇到了问题。

我们的类(class)刚刚复习了有关循环的 Material ,我需要确保代码使用这样的 Material 。到目前为止,我使用 while 循环来扫描整个文件直到结束。虽然它确实获得了所需的学生信息。它只打印最后一个,所以显然只得到 1 个总数,然后用下一部分覆盖相同的一个。这是代码。

我或多或少只需要输入和可能输出方面的帮助。

至于输入文件,如下

Dom Pilorusso
1037 Waterford Court
Canonsburg PA 15317
C937493021
15

Dan Madeupname
106 Cidar Lane
McMurray PA 15317
C927012312
11

Steve Arnold
281 Maple Drive
Canonsburg PA 15317
C482716209
9

<小时/>
public class Program4 {

// Sets the prices for full time and part time students.

static final double TUITION_PER_CREDIT = 276.00;
static final double FEE_PER_CREDIT = 15.00;
static final double SERVICE_PER_CREDIT = 7.09;
static final double FULL_TIME_TUITION = 3311.00;
static final double FULL_TIME_FEE = 184.00;
static final double FULL_TIME_SERVICE = 85.00;


public static void main(String[] args) throws FileNotFoundException
{
int i ;
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");

//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);

//Output to file all info, needs fixed.
outFile.println("Tuition Billing Report ");
outFile.printf("CWID\t\t"+ "Name\t\t"+ "Credits\t\t"+ "Tuition\t\t"+ "Fees\t\t"+ "Total%n");
outFile.printf(accountNumber + "\t"+ firstName + " "+ lastName + "\t"+ creditsTaken + "\t\t" + formatTuition + "\t\t" + formatFees + "\t\t" +formatTotal);

inFile.close();
outFile.close();

JOptionPane.showMessageDialog(null, "The program was saved in tuitionAndFees.dat");
}
}

最佳答案

您只创建了每个变量之一,并且在正确浏览文件的同时,您不断地将值分配给变量并覆盖以前的值。

您有 3 个选择:

1 - 为每个变量创建一系列数组并在期间分配值(这是有点糟糕的结构化编程)

2 - 您创建一个类,用所有变量表示您的学生实体,并在创建实例并将每个实例分配给数组位置(学生数组)的同时进行

3 - 你读取每一行并进行你想要的处理(有时这会让事情变得有点困难,因为你可能必须有很多操作系统累加器和辅助变量)

我会选择选项 2。

关于java - 带循环的输入和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20484899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com