У меня проблема со школьным проектом, где мне нужна помощь ... Проблема в том, что Я не могу понять, как правильно использовать Math.pow
, поэтому я могу сказать, что (1/(1 + x^5)), и он вычислит x^5 как math.pow(x,5)
.Java math.pow, get '^', чтобы быть как 4^3 <=> math.pow (4,3)
for (int i = 1; i <= (n/2)-1; i++) {
xi = a + i*h;
s1 += f(numF, xi);
}
Здесь numF
есть (1/(1 + х^5)).
Это интегральный калькулятор, поэтому он должен быть как (1/(1 + x^xi)). И numF
это из TextField, так что не всегда то же самое, так что когда ^
используется, то он должен сделать расчет
Вот как это выглядит в полном
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) {
}
}
}
Можете ли вы опубликовать весь свой код? Что такое, numF, h, n и т. Д.? – aeliusd
'Math.pow (a, b)' вычисляет a^b, поэтому, если вы хотите вычислить что-то вроде 1/1 + x^xi, вы должны иметь что-то вроде '1/(1 + Math.pow (x, xi)) '. Также следует понимать, что 'Math.pow' вычисляется на' double 'not' int'egers. Вы также должны преобразовать свой 'numF' (' String') в 'double' с чем-то вроде' Double.parseDouble (numF) '. –
Я вижу, но проблема в том, что она должна быть не только (1/(1 + x^xi)), но также может быть случайным текстом, например (x4) +5 – Schiellerup