【问题标题】:Java File Handling DisplayOnConsoleJava 文件处理 DisplayOnConsole
【发布时间】:2017-11-05 06:30:33
【问题描述】:

问题 - 文件处理 [20 分]

鉴于以下第一国民银行客户的一些账户信息数据:

111111 约翰·史密斯 500

222222 马克史密斯 1500

333333 史蒂夫·琼斯 2000

444444 玛丽琼斯 1000

           Write a Java program that:

一个。将上述数据写入文件“payroll.txt”。

b.从“payroll.txt”中读取上述数据。

c。在java控制台上显示每个客户的账户信息如下:

账户 1 id:111111 名称:john smith 余额:500 $

账户 2 id:222222 名称:mark smith 余额:1500 $

 Account 3 id: 333333  name: steve jones  balance: 2000 $

账户 4 id:444444 名称:mary jones 余额:1000 $

d。计算并显示所有客户的总余额: 所有账户的总余额为 5000 美元


我只能做 a 和 b 部分,我需要 c 和 d 部分的帮助

import java.io.*;
import java.util.Scanner;
public class FileHandling {
    public static void main(String[] args) throws Exception {

        PrintWriter out = new PrintWriter("payroll.txt");
        out.println("111111 john smith 500");
        out.println("222222 mark smith 1500");
        out.println("333333 steve jones 2000");
        out.println("444444 mary jones 1000");
        out.close();
        Scanner in=new Scanner("payroll.txt");
        while (in.hasNext()){

【问题讨论】:

    标签: java file-handling


    【解决方案1】:

    c.

    System.out.println("Account 1 id 111111 name: john smith balance: 500 $");
    System.out.println("Account 2 id 222222 name: mark smith balance: 1500 $");
    System.out.println("Account 3 id 333333 name: steve jones balance: 2000 $");
    System.out.println("Account 4 id 444444 name: mary jones balance: 1000 $");
    

    如果您有变量数据,您可以使用字符串连接直接从变量中替换每个 id、name 和 balance。

    d。

    int totalBalance = 500 + 1500 + 2000 + 1000;
    System.out.println("Total balance of all accounts is " + totalBalance + " $");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      • 2015-03-08
      • 1970-01-01
      • 2011-09-10
      • 2012-07-29
      • 1970-01-01
      • 2020-08-25
      相关资源
      最近更新 更多