在main方法中缺少return语句

时间:2013-06-24 03:21:32

标签: java return

我正在尝试编译程序并收到错误消息:“acc_man.java:46:缺少return语句”。我在类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

请提出更正建议。将非常感谢帮助。

3 个答案:

答案 0 :(得分:0)

主要方法应该是公共类

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

    }
}

答案 1 :(得分:0)

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

替换:     public int Bank_Account(int initial_balance){

balance = initial_balance;
}
// end constructor

使用:

public Bank_Account (int initial_balance) {

balance = initial_balance;
}
// end constructor

答案 2 :(得分:0)

删除构造函数中的int语句

public  Bank_Account (int initial_balance) {
     balance = initial_balance;
}
抛出它是因为它被解析为普通方法