【问题标题】:missing return statement in main methodmain 方法中缺少 return 语句
【发布时间】:2013-06-24 03:21:31
【问题描述】:

我正在尝试编译程序并收到错误消息:“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

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

【问题讨论】:

  • 不要只是将您的代码飞溅到文本区域中,而是要确保它按文件分隔并且您指出问题出在哪里
  • 能否请您发布完整的异常详细信息

标签: java return


【解决方案1】:

主方法应该在公共类中

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

    }
}

【讨论】:

    【解决方案2】:

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

    替换: public int Bank_Account (int initial_balance) {

    balance = initial_balance;
    }
    // end constructor
    

    与:

    public Bank_Account (int initial_balance) {
    
    balance = initial_balance;
    }
    // end constructor
    

    【讨论】:

      【解决方案3】:

      删除构造函数中的 int 语句

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

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

      【讨论】:

        猜你喜欢
        • 2013-05-23
        • 1970-01-01
        • 1970-01-01
        • 2019-10-07
        • 2021-03-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多