简体   繁体   English

从方法返回数组

[英]Returning an array from a method

THis is in the main class 这是在主要班级

actionsClass actionObject = new actionsClass(tipArray, hourArray, 
     hourlyWageInput, gasArray, wageArray, incomeArray, totalHourlyEarnings, 
     totalGas, totalHours, avgGasLabel);

actionObject.calculateTable();

This is my class where I am trying to implement the method (there are currently excessive declared variables): 这是我的类,我试图实现该方法(目前有过多的声明变量):

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class actionsClass {

private JLabel hourlyWage, blank, row2, totalTips, totalHours, totalHourlyEarnings, 
totalPay, weekPay, day, totalGas, totalHoursLabel, totalTipsLabel, totalGasLabel, 
totalWageLabel, avgGas, avgGasLabel;
private JTextField hourlyWageInput;

private double incomeArray[] = new double[7];
private JTextField tipArray[] = new JTextField[7];
private JTextField hourArray[] = new JTextField[7];
private JTextField gasArray[]= new JTextField[7];
private JLabel wageArray[] =new JLabel[7];


public actionsClass() {
}

public actionsClass(JTextField[] tipArray, JTextField[] hourArray,
        JTextField hourlyWageInput, JTextField[] gasArray,
        JLabel[] wageArray, double[] incomeArray,
        JLabel totalHourlyEarnings, JLabel totalGas, JLabel totalHours,
        JLabel avgGasLabel) {
    this.tipArray = tipArray;
    this.hourArray = hourArray;
    this.hourlyWageInput = hourlyWageInput;
    this.gasArray = gasArray;
    this.wageArray =  wageArray;
    this.incomeArray =  incomeArray;
    this.totalHourlyEarnings =  totalHourlyEarnings;
    this.totalGas = totalGas;
    this.totalHours = totalHours;
    this.avgGasLabel = avgGasLabel;
}

public String calculateTable (){
    for (int i = 0; i < 7; i++) {
        double tipx = Double.parseDouble(tipArray[i].getText());
        double houry = Double.parseDouble(hourArray[i].getText());
        double hourlyz = Double.parseDouble(hourlyWageInput.getText());

        String[] wageArrayStrings = null;

        if (houry != 0 ){
            wageArrayStrings[i] = String.format("%.2f", (hourlyz*houry+tipx)/houry);

        }
        else {
            wageArrayStrings[i] = ("$ 0.00");
        }

    }
    return wageArrayStrings[];
}

} }

There is a syntax error on return wageArrayStrings[]; return wageArrayStrings []存在语法错误; with or without brackets. 带或不带括号。 What am I doing wrong? 我究竟做错了什么?

First, the return type should be String[] . 首先,返回类型应为String[]

Then you need to initialize the array (as @MattBall points out, before the loop): 然后你需要初始化数组(如@MattBall指出,在循环之前):

String[] wageArrayStrings = new String[7];
for (int i = 0; i < 7; i++) {

Then you can do 那你可以做

return wageArrayStrings;

It should be just return wageArrayStrings ; 它应该只是返回wageArrayStrings ; no need of square brackets and return type should be String[] instead String (assuming your intention is returning String array). 不需要方括号和返回类型应该是String[]而不是String (假设你的意图是返回String数组)。

It should simply be (changed function returned type too) - 它应该只是(改变函数返回类型) -

public String[] calculateTable () {

    //// your code.
    return wageArrayStrings;
}

Also, you have not initialized your array. 此外,您还没有初始化您的阵列。 You should do that before the for loop. 你应该在for循环之前做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM