Java编程银行账户代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20578267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java programing bank account code
提问by Kristen
I am using eclipse to look at my code and the most common error that comes up is "Syntax error on token(s), misplaced construct(s)" I'm not sure what I am doing wrong, but I am fairly new to Java.
我正在使用 eclipse 查看我的代码,出现的最常见错误是“令牌上的语法错误,错位的构造”我不确定我做错了什么,但我对爪哇。
My code is supposed to withdraw an indicated (user-input) amount from a bank account, I started off with $10,000 and set up the program so that if the withdrawal amount is less than 0 or greater than $10,000 it will trigger an assertion error.
我的代码应该从银行账户中提取指定的(用户输入)金额,我从 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());
}
Thank you for all of your help!
谢谢大家的帮助!
回答by Ashish
rename your class "ThreadsUnitProject2" to BankAccount and remove main() method from that and make BankAccountTester class public and finally remove void from BankAccount constructor
将您的类“ThreadsUnitProject2”重命名为 BankAccount 并从中删除 main() 方法并使 BankAccountTester 类公开,最后从 BankAccount 构造函数中删除 void
回答by Tdorno
After seeing so many issues in your code I decided I should just fix it and let you try to learn from the solution seen below.
在您的代码中看到这么多问题后,我决定我应该修复它,让您尝试从下面看到的解决方案中学习。
This should be the first Class file.
这应该是第一个 Class 文件。
public class BankAccount {
private int balance;
public BankAccount() { //constructor
balance = 10000;
}
public int withdraw(int amount) {
balance -= amount;
return amount;
}
public int getBalance() {
return balance;
}
}
This should be your second Class file. This one will contain your main
method which will test your BankAccount class.
这应该是您的第二个 Class 文件。这将包含main
将测试您的 BankAccount 类的方法。
import java.util.Scanner;
public class BankAccountTester {
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("NewBalance = " + myAccount.getBalance());
}
}
Please read and review this link to proper coding conventions.
请阅读并查看此链接以了解正确的编码约定。