【问题标题】:running into problems with java code遇到java代码问题
【发布时间】:2016-06-29 17:39:29
【问题描述】:

在以下代码中,使用 main 方法在每次交易后打印帐号和更新的余额,或者直到输入 Q。

在方法Update balance which中,给定账户余额,交易类型(D =存款,W =提款,Q =退出)和交易金额,计算机并在存款后返回新的账户余额或提取给定金额号码。

但是现在我遇到了问题,不知道如何修复我生成的这段代码

import java.util.Scanner ;
public class CustomerAccount
{
public static void main( String[] args ) 
{
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the account number: ") ;
    String accountNumber = in.nextLine() ; 
    System.out.print("Please enter the initial balance: ") ;
    int startingBal = in.nextInt() ;
    int updatedBal = startingBal ;
    System.out.print("Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: ") ;
    String type = in.nextLine() ;
    while(!"Q".equals(type)) 
    {
        System.out.print("Please enter the amount to be deposited or withdrawn: ") ;
        int adjustment = in.nextInt();
        if(type.equals("D"))
        {
            updatedBal = updatedBalance(depositAmt);
        }
        else 
            updatedBal = updatedBalance(withdrawAmt);
        }
        System.out.println("Account Number: " + accountNumber + "    UpdatedBalance: $" + updatedBal) ;
    }
}
public static int updatedBalance(int amount)
{
    amount = amount + adjustment ;
    return amount;
}
}

给出以下输出:

[File: /CustomerAccount.java Line: 28, Column: 19] class, interface, or enum expected
[File: /CustomerAccount.java Line: 31, Column: 9] class, interface, or enum expected
[File: /CustomerAccount.java Line: 32, Column: 5] class, interface, or enum expected

【问题讨论】:

  • 使用do-while循环,先获取输入工作,然后检查循环条件。
  • 在你修复大括号之后,你会遇到一个问题,可以通过查看这里的解决方案来解决 ==> stackoverflow.com/questions/13102045/…
  • 是的,现在它找不到变量 depositAmt、withdrawAmt 和adjustment
  • @timNorth - 老实说,我认为您应该考虑学习基本教程或其他内容?
  • 是的,我认为这是个好主意

标签: java methods


【解决方案1】:

这里是您的代码的一个“改进”版本,但仍有很多事情要做。看来你是初学者,我做的主要修改是做一个抽象。我添加了一个名为“CustomerAccount”的新类以及哪些商店;

  • 有关帐户的数据
  • 存款和取款等方法

所以,现在帐户数据和功能被封装在 CustomerAccount 类中。

程序的入口点成为 CustomerAccountHandler 类。

这里是代码和输出,希望对你的学习有所帮助;

A - CustomerAccount 类

public class CustomerAccount {

    private final String accountId; // account number is final, it should never be updated
    private int amount;

    // Constructor with two paremeters
    public CustomerAccount(String accountId, int initialAmount) {
        this.accountId = accountId;
        this.amount = initialAmount;
    }

    // Funcionality : deposits to the amount
    // Returns      : latest amount
    public int deposit(int adjustment) {
        this.amount = amount + adjustment;

        return this.amount;
    }

    // Funcionality : witdraws from the amount
    // Returns      : latest amount
    public int withdraw(int adjustment) {
        this.amount = amount - adjustment;

        return this.amount;
    }

    // getters

    public String getAccountId() {
        return this.accountId;
    }

    public int getAmount() {
        return this.amount;
    }

}

B - 演示代码

import java.util.Scanner;

public class CustomerAccountHandler {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.print("Please enter the account number: ");
        String accountId = in.nextLine();
        System.out.print("Please enter the initial balance: ");
        int startingBal = Integer.parseInt(in.nextLine());

        // Create the account here
        CustomerAccount account = new CustomerAccount(accountId, startingBal);

        int updatedBal, adjustment; // create here, we will use it a lot
        String type;

        while (true) {
            System.out.println("Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: ");
            type = in.nextLine();

            if(type.equals("Q")) {
                System.out.println("Quitting transactions...");
                break;
            } else if(type.equals("W") || type.equals("D")) {
                System.out.println("Please enter the amount to be deposited or withdrawn: ");
                adjustment = Integer.parseInt(in.nextLine());

                if (type.equals("D")) {
                    updatedBal = account.deposit(adjustment);
                    System.out.println("Updated Balanced : " + updatedBal);
                } else if(type.equals("W")) {
                    updatedBal = account.withdraw(adjustment);
                    System.out.println("Updated Balanced : " + updatedBal);
                }
            } else {
                System.out.println("Please enter a valid Command: D,W,Q");
            }
        }

        System.out.println("\nAccount Number: " + account.getAccountId() + "    UpdatedBalance: $" + account.getAmount());
    }
}

C - 样本输出

Please enter the account number: Levent1299
Please enter the initial balance: 150
Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: 
W
Please enter the amount to be deposited or withdrawn: 
30
Updated Balanced : 120
Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: 
D
Please enter the amount to be deposited or withdrawn: 
80
Updated Balanced : 200
Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: 
Q
Quitting transactions...

Account Number: Levent1299    UpdatedBalance: $200

始终使用面向对象编程的封装支柱。不同的域应该有一个不同的类,在这种情况下是 CustomerAccount 类,存储客户数据和客户方法并封装它们。

【讨论】:

  • @timNorth 我喜欢在这里练习,如果真的有帮助,我很高兴。
猜你喜欢
  • 2020-02-16
  • 1970-01-01
  • 2012-04-09
  • 1970-01-01
  • 2022-07-28
  • 2019-03-03
  • 1970-01-01
  • 2018-09-23
  • 2021-05-19
相关资源
最近更新 更多