【问题标题】:Input/Output Java Method输入/输出 Java 方法
【发布时间】:2013-06-21 09:15:37
【问题描述】:

说明如下:

要处理交易,您需要从 transactions.txt 文件中一次读取一行并解析您检索到的字符串。您可以为此使用 Scanner 类。分隔符将是一个冒号。然后处理交易;您无需检查交易类型。只需将交易金额添加到支票簿余额中即可。添加负交易金额将按预期减少余额。请务必在适当的情况下使用 try/catch 块。

处理完每个事务后,调用 animate 方法。此方法属于 Accounting 类,因此您将在不使用对象引用的情况下调用 animate。动画方法如下的API

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

如您所见,animate 方法接受三个参数:currentTransaction 是交易名称(例如“Deposit”),currentAmount 是交易金额(例如-45.00),currentBalance 是当前余额的支票簿。假设您有一个名为 transactionName 的 String 变量、一个名为 amount 的双精度变量和另一个名为 balance 的双精度变量,对 animate 的调用将如下所示:

animate( transactionName, amount, balance);

当您调用 animate 时,窗口将按地理位置显示当前事务。它还将显示交易金额(红色为负数,蓝色为正数)和当前支票簿余额(黑色)。通过将先前的支票簿余额添加到当前金额,您将能够在脑海中计算出当前支票簿的余额并确定您的程序是否正常运行。

当您到达文件末尾时,打印最终余额并将其写入名为 balance.txt 的文件中

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

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( );
 }
}

【问题讨论】:

  • public void animate { String currentTransaction, double currentAmount, double currentBalance, }:这是不可能的
  • 您的问题是什么?好像你在寻求解决方案
  • @Stephan 我有点想寻求解决方案。这不是为了上课,也不是为了作业。这是教科书上的纯练习。我想不通,所以我想也许请别人教我怎么做会帮助我理解它。

标签: java graphics exception-handling io output


【解决方案1】:

使用扫描仪是在 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);

}    

【讨论】:

  • 谢谢!在我发布问题后,我做到了这一点。但是我该如何调用 animate 函数呢?
  • line 变量解析为字符串,并进行2个双打并将它们传递给animate()
  • @StackP 你可以使用另一个扫描仪并调用 lineScanner.next() 或 lineScanner.nextDouble()
  • 我知道这可能问得太多了,但是由于我正在尝试自己练习,请您帮我做一下吗?阅读代码让我明白发生了什么,但此时我不知道我需要做什么。
  • @StackP 伙计,我在工作......但是我添加了一些东西,我不知道你的文本文件是什么样的,所以我编了一些东西,每当你调用 next()扫描仪上的方法,它获取下一个标记,并返回一个字符串,如果下一个标记是一个数字,则调用 nextDouble() 它将返回一个双精度
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 2016-06-07
  • 2012-07-19
相关资源
最近更新 更多