This question already has answers here:
Syntax error on token(s), misplaced construct(s) Code Help Needed
                                
                                    (2个答案)
                                
                        
                                6年前关闭。
            
                    
我正在使用eclipse来查看我的代码,最常见的错误是“令牌语法错误,构造位置错误”我不确定自己做错了什么,但是我很新Java。

我的代码应该从银行帐户中提取一个指示的(用户输入)金额,我以10,000美元开始并设置了程序,因此,如果提款金额小于0或大于10,000美元,则会触发断言错误。

  class ThreadsUnitProject2 {
public static void main(Sting args [])
// Field member
private int balance;

public void BankAccount()
{
balance = 10000;
}

public int withdraw(int amount)
{
// Subtract requested amount from balance
balance-=amount;

// Return requested amount
return amount;
}


public int getBalance()
{
return balance;
}


import java.util.Scanner;

class BankAccountTester extends BankAccount
{
public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);

    BankAccount myAccount = new BankAccount();

    System.out.println("Balance = " + myAccount.getBalance());

    System.out.print("Enter amount to be withdrawn: ");
int amount = scan.nextInt();

    assert (amount >= 0 && amount <= myAccount.getBalance()):"You can't withdraw that amount!";

    myAccount.withdraw(amount);

    System.out.println("Balance = " + myAccount.getBalance());
}


多谢您的协助!

最佳答案

将您的类“ ThreadsUnitProject2”重命名为BankAccount,然后从其中删除main()方法,并使BankAccountTester类成为公共类,最后从BankAccount构造函数中删除空白

10-08 03:05