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

时间:2015-11-30 02:35:40

标签: java user-interface recursion iteration

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

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

IterativeVsRecursive类:

:tutq
echo What is the first letter of your name?
echo 1(A
echo 2(B
echo 3(C
echo 4(D
echo 5(E
echo 6(F
echo 7(G
echo 8(H
echo 9(I
echo 10(J
echo 11(K
echo 12(L
echo 13(M
echo 14(N
echo 15(O
echo 16(P
echo 17(Q
echo 18(R
echo 19(S
echo 20(T
echo 21(U
echo 22(V
echo 23(W
echo 24(X
echo 25(Y
echo 26(Z
set /p tutnum=
If not defined !tutnum! (
cls
goto tutq
)
If "!tutnum!" == "1" (
set "tutlet=A"
goto tutp2
)
If "!tutnum!" == "2" (
set "tutlet=B"
goto tutp2
)
If "!tutnum!" == "3" (
set "tutlet=C"
goto tutp2
)
If "!tutnum!" == "4" (
set "tutlet=D"
goto tutp2
)
If "!tutnum!" == "5" (
set "tutnum=E"
goto tutp2
)
If "!tutnum!" == "6" (
set "tutlet=F"
goto tutp2
)
If "!tutnum!" == "7" (
set "tutlet=G"
goto tutp2
)
If "!tutnum!" == "8" (
set "tutlet=H"
goto tutp2
)
If "!tutnum!" == "9" (
set "tutlet=I"
goto tutp2
)
If "!tutnum!" == "10" (
set "tutlet=J"
goto tutp2
)
If "!tutnum!" == "11" (
set "tutlet=K"
goto tutp2
)
If "!tutnum!" == "12" (
set "tutlet=L"
goto tutp2
)
If "!tutnum!" == "13" (
set "tutlet=M"
goto tutp2
)
If "!tutnum!" == "14" (
set "tutlet=N"
goto tutp2
)
If "!tutnum!" == "15" (
set "tutlet=O"
goto tutp2
)
If "!tutnum!" == "16" (
set "tutlet=P"
goto tutp2
)
If "!tutnum!" == "17" (
set "tutlet=Q"
goto tutp2
)
If "!tutnum!" == "18" (
set "tutlet=R"
goto tutp2
)
If "!tutnum!" == "19" (
set "tutlet=S"
goto tutp2
)
If "!tutnum!" == "20" (
set "tutlet=T"
goto tutp2
)
If "!tutnum!" == "21" (
set "tutlet=U"
goto tutp2
)
If "!tutnum!" == "22"(
set "tutlet=V"
goto tutp2
)
If "!tutnum!" == "23" (
set "tutlet=W"
goto tutp2
)
If "!tutnum!" == "24" (
set "tutlet=X"
goto tutp2
)
If "!tutnum!" == "25" (
set "tutlet=Y"
goto tutp2
)
If "!tutnum!" == "26" (
set "tutlet=Z"
goto tutp2
)
:tutp2
echo Congrats!
echo If you did the exersize correct
 echo The first letter of your name should be "!tutlet!"
pause

序列类:

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);
    }
}

目的是让GUI显示Sequence类的结果。

1 个答案:

答案 0 :(得分:1)

  

我应该已经清楚了,它不允许我在Sequence中调用非静态方法,但如果我将它们设为静态,通过添加" void",那么computeRecursive方法会抛出错误的错误说'无效'这里不允许输入

或者,由于该方法已经是实例方法,因此请创建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));
}