0

我正在尝试编译一个程序并收到一条错误消息:“acc_man.java:46: missing return statement”。我在 acc_man 类下创建了两个银行帐户,另一个类具有 main 方法要使用的所有方法。Bank_Account 类下的私有方法用于将参数传递给构造函数。

代码如下:

import java.io.*;
import java.util.*;

class acc_man {
public static void main (String[] args) throws IOException {

Bank_Account sav_account = new Bank_Account ();
Bank_Account cr_account = new Bank_Account ();
BufferedReader stdin = new BufferedReader
(new InputStreamReader (System.in));

System.out.println ("Please choose Account: 1.Savings 2.Credit");
int acc_type = Integer.parseInt (stdin.readLine());

System.out.println ("What do you want to do: 1. Deposit 2. Withdraw ");
int tr_type = Integer.parseInt (stdin.readLine());

System.out.println ("Please enter amount");
int amount = Integer.parseInt (stdin.readLine());

if (acc_type < 2) 

  if (tr_type < 2)
  sav_account.deposit (amount);
  sav_account.print();

  if (tr_type > 1)
  sav_account.withdraw (amount);
  sav_account.print();

if (acc_type > 1)

  if (tr_type < 2)
  sav_account.deposit (amount);
  sav_account.print();

   if (tr_type > 1)
   sav_account.withdraw (amount);
   sav_account.print();

}// method main
}// class acc_man

class Bank_Account {

private int amount;
private int balance;

public int Bank_Account (int initial_balance) {

balance = initial_balance;
}
// end constructor

public int deposit (int amount) {

balance = balance + amount;

return balance;
}
//end method deposit

public  int withdraw (int amount) {

balance = balance - amount;
return balance;
}

//end method withdraw

private int set_balance (int initial_balance) {

initial_balance = balance;
return initial_balance;
 }

// end method set_balance

public void print() {

System.out.println ("Current balance of the account is:" + balance);
}


// end method print
}
//end class Bank_Account

请指正并指正。帮助将不胜感激。

4

3 回答 3

0

主要方法应该在公共类中

public class acc_man {
    public static void main (String[] args) throws IOException {
          // ........ your codes

    }
}
于 2013-06-24T03:57:31.180 回答
0

构造函数使用类的名称并且没有返回类型。

替换: public int Bank_Account (int initial_balance) {

balance = initial_balance;
}
// end constructor

和:

public Bank_Account (int initial_balance) {

balance = initial_balance;
}
// end constructor
于 2013-06-24T04:00:37.343 回答
0

删除构造函数中的 int 语句

public  Bank_Account (int initial_balance) {
     balance = initial_balance;
}

它抛出它是因为像普通方法一样被解析

于 2014-11-21T00:11:08.767 回答