我正在尝试创建并执行一种方法,该方法允许用户选择一个帐户,用户可以从中选择一个帐户,也可以将金额转入一个帐户,用户也可以选择该帐户。但是,我不知道如何让用户输入询问他们想要将资金转移到哪个类别。
我试过使用 Scanner 方法,但无法正常工作。
这是代码,它与下面的代码相同,但这更容易查看 imo:https://gist.github.com/KamronKelley/32d9a40c285e501f64cf73fa28bf87b5
package banking;
public class Account {
String name;
double balance;
public Account() {
name = "";
balance = 0.0;
}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public void addFunds(double addedAmount) {
balance = balance + addedAmount;
}
public void withdraw(double withdrawnAmount) {
balance = balance - withdrawnAmount;
}
public void transfer(double amount, Account from, Account to) { //here is the transfer method, if i can improve this or alter it in order to make it easier to run using user input from the main file, let me know
if(from.balance >= amount){
from.balance = from.balance - amount;
to.balance = to.balance + amount;
System.out.println("Funds successfully transfered.");
} else {
System.out.println("Insufficient funds");
}
}
}
package banking;
import java.util.Scanner;
public class BankSimulator {
public static void main(String[] args) {
System.out.println("Hello and welcome to the banking system. Please enter a name to create an account, no spaces: ");
Scanner scan = new Scanner(System.in);
Account a1 = new Account();
a1.setName(scan.next());
System.out.println("Account name: " + a1.getName());
int count = 0;
while(count == 0) {
我无法对用户输入做任何事情,因为我需要所需的帐户才能使用它们运行传输方法。
Cats萌萌
慕盖茨4494581
一只萌萌小番薯
潇潇雨雨
相关分类