gpt4 book ai didi

java - 从另一个类调用方法并检索其结果 (Java)

转载 作者:行者123 更新时间:2023-11-30 07:45:37 26 4
gpt4 key购买 nike

我有 2 个类(IterativeVsRecursive 和 Sequence),Sequence 类执行数学操作并将值返回给 IterativeVsRecursive,或者至少这是目的。

我相信我错过了一些简单的事情,但对如何完成这项工作感到困惑。

迭代与递归类:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.sound.midi.Sequence;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class IterativeVsRecursiveGUI extends JFrame implements ActionListener {
private JLabel radioButton1Label = new JLabel();
private JLabel radioButton2Label = new JLabel();
private JLabel inputLabel = new JLabel();
private JLabel resultLabel = new JLabel();
private JLabel efficiencyLabel = new JLabel();
private JLabel computeButtonLabel = new JLabel();
private ButtonGroup radioButtonGroup = new ButtonGroup();
private JRadioButton radioButtonIterative = new JRadioButton("Iterative");
private JRadioButton radioButtonRecursive = new JRadioButton("Recursive");
private JTextField inputField = new JTextField();
private JTextField resultField = new JTextField();
private JTextField efficiencyField = new JTextField();
private JButton computeButton = new JButton();
private int efficiencyCounter;

public IterativeVsRecursiveGUI()

{
super("Project 3");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(6, 2));
radioButtonGroup.add(radioButtonIterative);
radioButtonIterative.setSelected(true);
// radioButtonIterative.setText("Iterative");
getContentPane().add(radioButtonIterative);
radioButtonGroup.add(radioButtonRecursive);
// radioButtonRecursive.setText("Recursive");
getContentPane().add(radioButtonRecursive);
inputLabel.setText("Enter number: ");
getContentPane().add(inputLabel);
getContentPane().add(inputField);
computeButton.setText("Compute");
computeButton.addActionListener(this);
getContentPane().add(computeButton);
resultLabel.setText("Result: ");
getContentPane().add(resultLabel);
getContentPane().add(resultLabel);
resultField.setEditable(false);
efficiencyLabel.setText("Efficiency: ");
getContentPane().add(efficiencyLabel);
getContentPane().add(efficiencyField);
efficiencyField.setEditable(false);
pack();
}

@Override
public void actionPerformed(ActionEvent button) {
int result;
efficiencyCounter = 0;
if (radioButtonIterative.isSelected()) {
result = Sequence.computeIterative(Integer.parseInt(inputField.getText()));
} else {
result = Sequence.computeRecursive(Integer.parseInt(inputField.getText()));
}
resultField.setText(Integer.toString(result));
efficiencyField.setText(Integer.toString(efficiencyCounter));
}

public static void main(String[] args) {
IterativeVsRecursiveGUI IterativeVsRecursiveGUI = new IterativeVsRecursiveGUI();
IterativeVsRecursiveGUI.setVisible(true);
}
}

序列类:

public class Sequence {
private int efficiencyCounter = 0;

public int computeIterative(int input) {
int answer = 0;
if (input == 0) {
efficiencyCounter++;
answer = 0;
} else if (input == 1) {
efficiencyCounter++;
answer = 1;
} else {
efficiencyCounter++;
int firstTerm = 0;
int secondTerm = 1;
for (int i = 2; i <= input; i++) {
answer = (3 * secondTerm) - (2 * firstTerm);
firstTerm = secondTerm;
secondTerm = answer;
}
}
return answer;
}

public int computeRecursive(int input) {
int answer = 0;
efficiencyCounter++;
if (input == 0) {
answer = 0;
} else if (input == 1) {
answer = 1;
} else {
answer = (3 * computeRecursive(input - 1)) - (2 * computeRecursive(input - 2));
}
return answer;
}
}

目的是拥有一个显示 Sequence 类结果的 GUI。

最佳答案

I should have been clear in that it is not allowing me to call the non-static methods in Sequence, but if I make them static, by adding "void", then the computeRecursive method throws an errror saying that 'void' type not allowed here

或者,由于该方法已经是实例方法,因此创建 Sequence 的实例

@Override
public void actionPerformed(ActionEvent button) {
int result;
efficiencyCounter = 0;

Sequence sequence = new Sequence();
if (radioButtonIterative.isSelected()) {
result = sequence.computeIterative(Integer.parseInt(inputField.getText()));
} else {
result = sequence.computeRecursive(Integer.parseInt(inputField.getText()));
}
resultField.setText(Integer.toString(result));
efficiencyField.setText(Integer.toString(efficiencyCounter));
}

关于java - 从另一个类调用方法并检索其结果 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33990649/

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