如果可用余额小于最小余额,我打算在输出中打印“FINE IS IMPOSED”。 但是我得到的错误如下所述。 PL。帮助我
//Savings Account of a customer by setting and getting MinimumBalance
public class SavingsAccount {
private int AccountNumber;
private String Customer;
private double Balance;
private double MinimumBalance;
/**
* constructor
*
* @param Number
* the account's number.
* @param Name
* the account holders name.
* @param bal
* the balance available.
*/
public void open(int Number, String Name, double bal) {
AccountNumber = Number;
Customer = Name;
Balance = bal;
}
/**
* sets Minimum Balance
*
* @param minbal
* the Minimum Balance
*/
public void setMinimumBalance(int minbal) {
MinimumBalance = minbal;
}
/**
* returning Minimum Balance
*
* @return it is returning Minimum Balance
*/
public double getMinimumBalance() {
return MinimumBalance;
}
/**
* display the details of savings account
*/
public void print() {
System.out.println("The Account Number is:" + (AccountNumber));
System.out.println("The name of the Customer is :" + (Customer));
System.out.println("Available balance is :" + (Balance));
System.out.println("Minimum Balance to be maintained is :"
(MinimumBalance));
}
}
// testing of savings account
public class Savings1 {
public static void main(String[] args) {
SavingsAccount S1; //Object Reference
S1 = new SavingsAccount(); //Creating Object
S1.open(256101, "Mr.Gates ", 5000);
S1.setMinimumBalance(6000);
S1.print();
if (S1.Balance < S1.getMinimumBalance()) {
System.out.println("FINE IS IMPOSED");
}
}
}
错误:线程“main”中的异常java.lang.Error:未解析的编译 问题: SavingsAccount.Balance字段不可见 在Savings1.main(Savings1.java:9)
答案 0 :(得分:0)
S1.Balance是私有的,公开或公开吸气。
答案 1 :(得分:0)
类SavingsAccount有一个属性Balance声明为private,这意味着它只在该类中可见。
访问Balance attibute的一个好方法是使用getBalance并从其他类
调用它public double getBalance(){
return this.Balance;
}
然后调用getBalance
if ( S1.getBalance() < S1.getMinimumBalance())
System.out.println("FINE IS IMPOSED");
完整版:
public class SavingsAccount {
private int AccountNumber;
private String Customer;
private double Balance;
private double MinimumBalance;
/**
* constructor
* @param Number the account's number.
* @param Name the account holders name.
* @param bal the balance available.
*/
public void open (int Number, String Name,double bal) {
AccountNumber=Number;
Customer=Name;
Balance=bal;
}
/**
* sets Minimum Balance
* @param minbal the Minimum Balance
*/
public void setMinimumBalance (int minbal) {
MinimumBalance=minbal;
}
/**
* returning Minimum Balance
* @return it is returning Minimum Balance
*/
public double getMinimumBalance() {
return MinimumBalance;
}
/**
* display the details of savings account
*/
public void print()
{
System.out.println("The Account Number is:" +(AccountNumber));
System.out.println("The name of the Customer is :" +(Customer));
System.out.println("Available balance is :" +(Balance));
System.out.println("Minimum Balance to be maintained is :" +
(MinimumBalance));
}
public double getBalance(){
return this.Balance;
}
}
// testing of savings account
class Savings1 {
public static void main(String[]args) {
SavingsAccount S1; //Object Reference
S1= new SavingsAccount(); //Creating Object
S1.open (256101,"Mr.Gates ", 5000);
S1.setMinimumBalance (6000);
S1.print();
if ( S1.getBalance() < S1.getMinimumBalance())
System.out.println("FINE IS IMPOSED");
}
}
答案 2 :(得分:0)
为Balance添加getter方法并改为使用
public double getBalance() {
return Balance;
}
if (S1.getBalance()< S1.getMinimumBalance()) {