【问题标题】:How do I call this method?我如何调用这个方法?
【发布时间】:2019-12-03 12:22:24
【问题描述】:

对于一个项目,我需要在我的 main 方法中调用方法 getBill。 getBill 返回一个 int 值(bill),但是当我声明它时:

getBill(qty);

我遇到了一个错误。我对使用方法比较陌生,我是否正确声明了方法本身?有没有什么方法可以在不使用方法的情况下完成 getBill 的工作?

这是我正在处理的代码的一部分,我需要调用该方法:

        while (true) {
            line = bc.readLine();
            if (line == null)
                break;
            billdata = line.split(",");
            accs = Integer.parseInt(billdata[0]);
            type = billdata[1];
            qty = Integer.parseInt(billdata[2]);
            company = billdata[3];


            if (accNo == accs) {
                System.out.println("Your RHY Telecom account has been found.");
                System.out.printf("%5s", accs);
                System.out.printf("%24s", qty);
                System.out.printf("%10s", type);
                System.out.printf("%20s", company);

                System.out.println("Your current charges are: ");

            }

        }
    }

这是方法本身:

    public static int getBill(int plan, int bill, int qty, String[] accountdata, String line) throws Exception {//getting details from text file number 2, then computing the bill
        BufferedReader bf = new BufferedReader(new FileReader("res/Account-Info-Final1.txt"));
        accountdata = line.split(",");
        plan = Integer.parseInt(accountdata[4]);
        bill = 0;
        int smslimit = Integer.parseInt(accountdata[5]);
        int calllimit = Integer.parseInt(accountdata[6]);
        double datalimit = Double.parseDouble(accountdata[7]);

        if (qty > smslimit) {
            bill = (qty * 2) + plan;
        } else if (qty > calllimit) {
            bill = (qty * 5) + plan;
        } else if (qty > datalimit) bill = (qty * 3) + plan;
        else if (qty <= smslimit || qty <= datalimit || qty <= calllimit){
                bill += plan;
            }
        return bill;
        }


    }

【问题讨论】:

  • “我遇到了一个错误”是什么错误?
  • 错误是什么?错误消息包含有关问题的有用线索。所以不要只说“有一个错误”——试着理解错误信息告诉你什么。并将其添加到您上面的问题中。
  • 欢迎来到stackoverflow。请提供您的问题的详细描述以及重现所述问题的最小示例。
  • 你的方法需要得到 5 个参数。你只用 1 个参数调用它。你为什么希望它起作用?
  • @TheLittleGreenFreshie 那么您没有将所有需要的数据传递给该方法。如果方法有 5 个参数,则需要将 5 条数据全部传递给它。

标签: java methods bufferedreader


【解决方案1】:

将方法视为具有输入和输出的数学函数。

您的 getBill 方法声明应该只为方法需要作为输入的内容声明方法参数(即括号中的逗号分隔变量)。

调用 getBill 方法的代码不需要提供 plan 值作为输入。方法本身会立即覆盖该值并忽略调用代码提供的任何值。

您应该从方法参数中删除plan,而是在方法体中声明它:

int plan = Integer.parseInt(accountdata[4]);

billaccountdataline 也是如此:

String line = bf.readLine();
String[] accountdata = line.split(",");
int plan = Integer.parseInt(accountdata[4]);
int bill = 0;

从方法签名中删除所有这些后,它将如下所示:

public static int getBill(int qty)

...这将允许您按预期调用方法。

【讨论】:

    【解决方案2】:

    您的 getBill 方法有 5 个参数,而您传入​​一个参数。

    getBill(1, 2, 3, new String[] { "Hello", "World" }, "Line"); 之类的东西会调用该方法,将 plan 设置为 1,bill 设置为 2,等等。您将 plan 设置为 1qtyis, and the other 4 arguments are still missing; you need to explicitly add them to yourgetBill()` 调用的任何值。

    注意:旁注,你没有关闭你的文件资源,所以这个代码很快就会因为资源耗尽而崩溃。查找“尝试使用资源”,以便您可以安全地打开该文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 2011-08-06
      • 2021-02-16
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      • 2011-04-12
      相关资源
      最近更新 更多