Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java代码遇到问题_Java_Methods - Fatal编程技术网

java代码遇到问题

java代码遇到问题,java,methods,Java,Methods,在下面的代码中,我们使用main方法在每次交易后或在输入Q之前打印帐号和更新的余额 在methodUpdate balance中,给定账户余额、交易类型(D=存款,W=取款,Q=退出)和交易金额,计算并返回存款或取款给定金额编号后的新账户余额 但是现在我遇到了问题,不知道如何修复我生成的代码 import java.util.Scanner ; public class CustomerAccount { public static void main( String[] args ) {

在下面的代码中,我们使用main方法在每次交易后或在输入Q之前打印帐号和更新的余额

在methodUpdate balance中,给定账户余额、交易类型(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

下面是代码的一个“改进”版本,但仍然有很多事情要做。看来你是个初学者,我做的主要修改是做一个抽象。我添加了一个名为“CustomerAccount”的新类,该类用于存储

  • 有关帐户的数据
  • 存款和取款功能等方法
因此,现在帐户数据和功能用CustomerAccount类封装

程序的入口点成为CustomerAccountHandler类

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

A-CustomerAccount类 B-演示代码 C-样本输出
始终使用面向对象编程的封装支柱。对于不同的域,应该有一个不同的类,CustomerAccount类在这种情况下,存储客户数据和客户方法并封装它们。

使用
do while
loop,首先获取输入工作,然后检查循环条件。在修复支架后,您将遇到一个问题,可以通过查看此处的解决方案来解决==>是,现在它找不到变量depositAmt、DrawitAmt和adjustment@timNorth-老实说,我想你应该考虑参加一个基础教程什么的?是的,我认为这是一个很好的选择idea@timNorth我喜欢在这里练习via,如果真的有帮助,我会很高兴。
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;
    }

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