gpt4 book ai didi

java - 输入/输出 Java 方法

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

说明是:

To process the transactions, you will need to read one line at a time from the transactions.txt file and parse the String that you retrieve. You can use the Scanner class for this. The delimiter will be a colon. Then process the transaction; you do not need to check the type of transaction. Just add the amount of the transaction to the checkbook balance. Adding a negative transaction amount will decrease the balance as expected. Be sure to use try/catch blocks where appropriate.

After you have processed each transaction, call the animate method. This method belongs to the Accounting class, so you will call animate without using an object reference. The API if the animate method is the following

public void animate { String currentTransaction, double currentAmount, double currentBalance, }

As you can see, the animate method takes three arguments: currentTransaction is the transaction name ("Deposit," for example), currentAmount is the amount of the transaction (-45.00, for example), and currentBalance is the current balance of the checkbook. Assuming that you have a String variable called transactionName, a double variable called amount, and another double called balance, a call to animate will look like the following:

animate( transactionName, amount, balance);

When you call the animate, the window will display the current transaction geographically. It will also display the transaction amount (red, if negative, blue if positive), and the current checkbook balance (in black). By adding the previous checkbook balance to the current amount, you will be able to compute in your head what the current checkbook valance should be and determine if your program is working correctly.

When you reach the end of the file, print the final balance and write it to a file named balance.txt

到目前为止,我已经在 Accounting 类中编写了这么多代码:

import javax.swing.*;
import java.text.DecimalFormat;
import java.awt.Graphics;
import java.util.*;
import java.io.*;

public class Accounting extends JFrame
{
private BankAccount bankAccount;

public Accounting( )
{
bankAccount = new BankAccount( getBackground( ) );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 300, 300 );
setVisible( true );
}

public void balanceCheckBook( )
{
// ***** Write the body of this method *****
//
// Using a while loop, read the file transactions.txt
// The file transactions.txt contains
// transactions between you and your bank
//
// You will need to call the animate method inside
// the body of the loop that reads the file contents
//
// The animate method takes three arguments:
// a String, representing the type of transaction
// a double, representing the transaction money amount
// a double, representing the new checkbook balance
// So if these three variables are:
// transactionName, currentAmount, and balance,
// then the call to animate will be:
//
// animate( transactionName, currentAmount, balance );
//
// You should make that call in the body of your while
// loop, after you have updated the checkbook balance
//


double balance = 0.00;
double currentAmount;
String nextLine;
StringTokenizer st;
String transactionName;
}

public void animate( String currentTransaction, double currentAmount, double currentBalance )
{
if ( currentTransaction.startsWith( "Ch" ) )
bankAccount.setCurrentTransaction( new Check(currentAmount ) );
else if ( currentTransaction.startsWith( "With" ) )
bankAccount.setCurrentTransaction( new Withdrawal(currentAmount ) );
else if ( currentTransaction.startsWith( "Dep" ) )
bankAccount.setCurrentTransaction( new Deposit(currentAmount ) );
else
bankAccount.setCurrentTransaction( new UnknownTransaction(currentAmount ) );


bankAccount.updateBalance( currentBalance );

repaint( );
try
{
Thread.sleep( 3000 );
}
catch ( Exception e )
{
}
}

public void paint( Graphics g )
{
super.paint( g );
bankAccount.draw( g );
}

public static void main( String [] args )
{
Accounting app = new Accounting( );
app.balanceCheckBook( );
}
}

最佳答案

使用扫描仪是在java中读取文件的最简单方法

import java.util.Scanner

然后

Scanner myScanner = new Scanner(new File("/Your/File/Path/Here/transactions.txt");

然后

String line;
while (myScanner.hasNextLine()) {
line = myScanner.nextLine();
//i think you'll call your animate function in here

}

根据您的文件结构,您可以使用 myScanner.next() 和 myScanner.nextInt() 分别获取标记和整数,标记通常是由空格分隔的单词

[编辑]

op 想要了解如何扫描每一行,这是一个演示

while (myScanner.hasNextLine()) {
line = myScanner.nextLine();
Scanner lineReader = new Scanner(line);
String firstWord = lineReader.next();
String secondWord = lineReader.next();
double thirdWordValue = lineReader.nextDouble();
double fourthWordValue = lineReader.nextDouble();

animate(firstWord, thirdWordValue, fourthWordValue);

}

关于java - 输入/输出 Java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17282692/

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