Java math.pow,让'^'像 4^3 <=> math.pow(4,3)

  • 本文关键字:math pow Java java int double pow
  • 更新时间 :
  • 英文 :


我对学校项目有问题,我需要一些帮助...问题是,我无法弄清楚如何正确使用Math.pow,因此我可以说(1/(1 x^5)),它将计算为x^5为 math.pow(x,5)

for (int i = 1; i <= (n/2)-1; i++) {
    xi = a + i*h;
    s1 += f(numF, xi);
}

这里 numF是(1/(1 x^5))。

这是一个积分计算器,因此应该像(1/(1 x^Xi))。numF来自文本场,因此并非总是相同的,因此当使用^时,它应进行计算

这就是完整的外观

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
public class Calculator extends JFrame {
    public JButton CalcButton;
    public JTextField InputA;
    public JTextField InputB;
    public JTextField InputN;
    public JTextField InputF;
    public JTextField OutputAnswer;
    public JLabel OutputAnswerLabel;
    public JLabel InputALabel;
    public JLabel InputBLabel;
    public JLabel InputNLabel;
    public JLabel InputFLabel;
    public Calculator() {
        this.setTitle("Integral Lommeregner");
        this.setSize(280, 95);
        this.setLocation(700, 300);
        this.setLayout(new FlowLayout());
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.addComponentListener(new ComponentListener() {
            public void componentHidden(ComponentEvent arg0) { }
            public void componentMoved(ComponentEvent arg0) { }
            public void componentResized(ComponentEvent arg0) { }
            public void componentShown(ComponentEvent arg0) {
                Calculator_Load();
            }
        });
        this.CalcButton = new JButton();
        this.CalcButton.setText("     Udregn     ");
        this.CalcButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CalcButton_ActionPerformed();
            }
        });
        this.InputA = new JTextField();
        this.InputA.setText("     ");
        this.InputALabel = new JLabel();
        this.InputALabel.setText("a:");
        this.InputB = new JTextField();
        this.InputB.setText("     ");
        this.InputBLabel = new JLabel();
        this.InputBLabel.setText("b:");
        this.InputN = new JTextField();
        this.InputN.setText("     ");
        this.InputNLabel = new JLabel();
        this.InputNLabel.setText("n:");
        this.InputF = new JTextField();
        this.InputF.setText("                                  ");
        this.InputFLabel = new JLabel();
        this.InputFLabel.setText("F(x)=");
        this.OutputAnswer = new JTextField();
        this.OutputAnswer.setText("                                 ");
        this.OutputAnswer.setEditable(false);
        this.OutputAnswerLabel = new JLabel();
        this.OutputAnswerLabel.setText("Areal:");
        add(InputALabel);
        add(InputA);
        add(InputBLabel);
        add(InputB);
        add(InputNLabel);
        add(InputN);
        add(InputFLabel);
        add(InputF);
        add(CalcButton);
        add(OutputAnswerLabel);
        add(OutputAnswer);
    }
    private void Calculator_Load() {
        InputA.setText("");
        InputB.setText("");
        InputN.setText("");
        InputF.setText("");
        OutputAnswer.setText("");
    }
    private void CalcButton_ActionPerformed() {
        try {
            int a, b, numF, n;
            double h, xi, xj, s1, s2, integral;
            a = Integer.parseInt(InputA.getText());
            do {
                b = Integer.parseInt(InputB.getText());
            }
            while (b <= a);
            n = Integer.parseInt(InputN.getText());
            numF = Integer.parseInt(InputF.getText());
            h = (double) (b - a) / n;
            for (int i = 1; i <= (n / 2) - 1; i++) {
                xi = a + i * h;
                s1 += f(numF, xi);
            }
            for (int j = 1; j <= (n / 2); j++) {
                xj = a + j * h;
                s2 += 2;
            }
            integral = (h / 3) * 2;
        } catch (Exception ex) {
        }
    }
}

您似乎真的在问如何解析和评估以字符串形式提供的算术表达式。

对于初学者来说,这是一个棘手的问题,但是有两种"相对简单"的方法可以解决此问题:

  1. 在分流码算法上阅读并将其应用于您的表达语言。

  2. 为您的表达语言开发A 语法,并使用解析器生成器(ANTLR,JAVACC等)生成解析器和Lexer,并将表达式评估逻辑嵌入解析器动作中。(表达解析是PGS文档集中的一个常见教程主题。)

最新更新