我怎么称呼这个方法?

问题描述 投票:-3回答:2

对于项目,我需要在主方法中调用方法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;
        }


    }
java methods bufferedreader
2个回答
0
投票

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

您的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)

…将允许您按预期的方式调用该方法。


0
投票

您的getBill方法有5个参数,并且您要传入一个参数。

© www.soinside.com 2019 - 2024. All rights reserved.