我怎么称呼这个方法?

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


    }
tian150288 回答:我怎么称呼这个方法?

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

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

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

,

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

类似getBill(1,2,3,new String[] { "Hello","World" },"Line");的方法会调用该方法,将plan设置为1,将bill设置为2,以此类推。您要将plan设置为1qty is,and the other 4 arguments are still missing; you need to explicitly add them to your getBill()`调用的任何值。

NB:旁注,您没有关闭文件资源,因此此代码将由于资源不足而很快崩溃。查找“尝试使用资源”,以便您可以安全地打开该文件。

本文链接:https://www.f2er.com/2985913.html

大家都在问