- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于我的 Java 类,我们需要创建一个银行帐户,该帐户具有取款、存款和显示当前余额的方法。在 Tester 类中,我想让它询问姓名、余额,然后让你选择 1、2 或 3。然后它重复你选择的选项,直到你说键入“n”。问题是运行这段代码会导致它在你存钱后说“你存入了(存入的金额)账户(账户名称)。你的新余额是(这个)。”它说“this”的部分与存入的金额完全相同。换句话说,它不添加它,它只是使新余额与存款相同,而不管之前有多少。有什么帮助吗?谢谢。
import java.io.*;
import java.util.*;
public class BankAccount
{
public BankAccount(double b, String n)
{
double balance = b;
String name = n;
}
public void deposit(double d)
{
balance += d;
}
public void withdraw(double w)
{
balance -= w;
}
public String nickname()
{
System.out.print("Enter a new name: ");
Scanner kbIn = new Scanner(System.in);
String n = kbIn.nextLine();
return n;
}
double balance;
String name;
}
测试类:
import java.io.*;
import java.util.*;
public class Tester
{
public static void main(String args[])
{
Scanner kbInLine = new Scanner(System.in);
Scanner kbIn = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = kbInLine.nextLine();
System.out.print("Please enter balance: $");
double balance = kbIn.nextDouble();
BankAccount myAccount = new BankAccount(balance, name);
String proceed = "y";
while(proceed.equalsIgnoreCase("y"))
{
System.out.println("\nPlease pick a number. Would you like to...\n\t 1. Deposit\n\t 2. Withdraw\n\t 3. Print Balance\n");
int choice = kbIn.nextInt();
switch(choice)
{
case 1:
System.out.print("How much would you like to deposit?\n\t$");
double deposit = kbIn.nextDouble();
myAccount.deposit(deposit);
System.out.println("You have deposited $" + deposit + " into the account of " + name + ". The new balance is: " + myAccount.balance);
break;
case 2:
System.out.print("How much would you like to withdraw?\n\t$");
double withdraw = kbIn.nextDouble();
if(myAccount.balance - withdraw > 0)
{
myAccount.withdraw(withdraw);
System.out.println("You have withdrawn $" + withdraw + " from the account of " + name + ". The new balance is: " + myAccount.balance);
}
else
{
System.out.println("Sorry, you have insufficient funds for this operation. Your existing balance is $" + myAccount.balance);
}
break;
case 3:
System.out.print("The balance in the account of " + name + " is $" + myAccount.balance);
break;
}
System.out.print("\nWould you like to do another transaction? (Y/N)");
proceed = kbIn.next();
}
System.out.println("\nThank you for banking with us. Have a good day!");
}
}
真正奇怪的是,我之前做过一个项目(实际上是一个简化版本),它存入并提取预定的编码金额,然后输出新的银行余额,它做得很好.但是 BankBalance 的代码是相同的。这是这些的代码。
BankAccount 类是:
public class BankAccount
{
public BankAccount(String nm, double amt) // Constructor
{
name = nm;
balance = amt;
}
public void deposit(double d) // Sets up deposit object as balance += d
{
balance += d;
}
public void withdraw(double w) // Sets up withdraw object as balance -= w
{
balance -= w;
}
public double balance;
public String name;
}
Tester 类是:
import java.io.*;
import java.util.*;
public class Tester
{
public static void main(String args[])
{
Scanner kbIn = new Scanner(System.in);
System.out.print("Enter your name:");
String name = kbIn.nextLine();
System.out.print("Enter the balance:");
double balance = kbIn.nextDouble();
BankAccount myAccount = new BankAccount(name, balance);
myAccount.deposit(505.22);
System.out.println(myAccount.balance);
myAccount.withdraw(100.00);
System.out.println("The " + myAccount.name + " account balance is, $" + myAccount.balance);
}
}
最佳答案
你实际上并没有在这里初始化你的 balance
成员变量:
public BankAccount(double b, String n)
{
double balance = b;
这将创建一个名为 balance
的新本地 变量,您可以将 b
的值分配给该变量。在此构造函数运行后,成员 变量 balance 将保持为 0(默认值)。
关于java - 银行账户程序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7762444/
我有一个英国 PayPal 企业帐户。我目前正在开发一个网站支付系统,使用 Express Checkout 向这个账户付款。 到目前为止,我的 PHP 代码正在运行,我能够连接到沙箱并设置和快速结帐
如何使用自定义消息和自定义 from 参数而不是默认参数向用户发送电子邮件验证。 Meteor.methods({ sendveryficationmail: function (theuse
考虑一个场景,其中他们是 PartyA,并且 account1 是在 PartyA 中创建的。现在我们要在account1和PartyA之间发起一笔交易。是否可以有一个由账户发起、接收方为主机节点的交
我有多个 AWS 账户,根据我所在的项目目录,我想在 AWS CLI 中键入命令时使用不同的账户。 我知道 AWS 凭据可以通过环境变量传递,所以我认为一种解决方案是根据它所在的目录设置 AWS_CO
我希望能够使用 PayPal Mass Payment 向我网站的用户付款。如果他们有 PayPal 帐户,我认为这非常简单。 但是,如果他们没有 PayPal 帐户,有没有办法让他们通过我的网站注册
是否可以使用 paypal API 向任何 paypal 帐户(不仅仅是 API 凭据所有者)汇款。我知道使用 IPN 可以做到这一点,但我需要使用 SOAP。 最佳答案 您有两个主要选择:使用 Ma
这个问题在这里已经有了答案: Confirm PayPal sandbox account email (3 个答案) 关闭 2 年前。
是否可以使用 paypal API 向任何 paypal 帐户(不仅仅是 API 凭据所有者)汇款。我知道使用 IPN 可以做到这一点,但我需要使用 SOAP。 最佳答案 您有两个主要选择:使用 Ma
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8 年前关闭。 Improve this
我一直在尝试 Meteor。我想使用 OAuth 对我网站上的用户进行身份验证,因为我不想自己实现登录功能。 目前我的网站非常简单。计数器,单击按钮计数器就会加一。当用户转到另一台机器并登录其计数时,
在我看来,您可以输入任何随机的用户名/密码组合,并且嵌入式 UI 小部件和后端都会接受它作为有效的 PayPal 帐户。付款和一切都会完成。 有没有办法可以将 Braintree 沙箱设置为仅接受真实
我已经设置并一直在使用 PayPal 使用 IPN/Express Checkout 在我的主网站上接受订单。但是我还有另外 4 个完全独立的网站,我也需要在它们上使用 PayPal 处理订单。购买我
我来这里是为了尝试解决 paypal 中没有人可以帮助我们解决的看似黑洞的问题。 我们有一个企业帐户。 Paypal Express(数码商品)。已验证,解除限制等...我们有 paypal expr
我正在编写一个 Web 应用程序,过去他们使用自己的 PayPal 帐户为外部自雇人员开设的类(class)付款。然而,税收制度已经改变,慈善机构希望他们的客户通过他们的网站预订(就像以前一样),但直
让我解释一下我的网站(ruby on Rails)的当前场景: 1) 我们可以从我的网站创建多个管理员帐户。 2) 每个管理员都有自己的客户,这些客户也有他们的网站访问部分。 3) 每个客户都可以向他
我正在两个 AWS 账户之间迁移。我想从我的 Mac 临时访问两个帐户上的代码提交存储库。 我已经为两个帐户的用户生成了 HTTPS Git 凭据。当我最初访问旧帐户时,它要求我提供存储在 OSX 钥
是否可以将一笔金额从一个关联账户转移到另一个关联账户?两者都在一个 Stripe 帐户下连接。 我知道我可以在两个帐户之间拆分转账,例如 $transfer = \Stripe\Transfer::c
如果一个人有两个 AWS 账户,一个用于开发,一个用于实时(例如)我知道可以使用 terraform 工作区来管理每个环境的状态。 但是,如果我将工作区从“开发”切换到“实时”,有没有办法告诉 ter
在 Meteor 中,我想将数据(和其他 CRUD 函数)插入到 users 表中,该表是在 Meteor 中下载 account-ui 和 account-password 包时生成的。我尝试在 c
有没有办法将 ETH 添加到 Ganache 账户?我知道我通常可以通过重新启动 ganache cli 来刷新帐户,但我使用的是 --db 选项,这意味着帐户是持久的。因此,它们很快就会枯竭。 最佳
我是一名优秀的程序员,十分优秀!