gpt4 book ai didi

java - 从方法返回数组

转载 作者:行者123 更新时间:2023-12-02 07:37:57 25 4
gpt4 key购买 nike

这是在主类中

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

actionObject.calculateTable();

这是我尝试实现该方法的类(当前声明的变量过多):

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[];
}

}

返回工资ArrayStrings[]时有语法错误;带或不带括号。我做错了什么?

最佳答案

首先,返回类型应该是String[]

然后你需要初始化数组(正如 @MattBall 指出的,在循环之前):

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

然后你就可以了

return wageArrayStrings;

关于java - 从方法返回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11944082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com