本文目录一览:
java计算简单的数学公式.
public class Demo2 {
public static void main(String[] args) {
double price = 100.0 ;//单价
int nums = 200;//数量
double total;//总价
total = price*nums;// 计算总价
double profit ;//利润
double cost=12000;//成本
double tax=0.17;//税率
profit = (total-cost)*(1-tax); //计算利润
System.out.println("利润:"+profit+"元");//输出利润
}
}
运行测试
利润:6640.0元
Java怎样实现数学问题的计算?
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.SystemColor;
public class Test extends JFrame {
private JPanel contentPane;
private JTextField headField;
private JTextField footField;
private JLabel result;
public static void main(String[] args) {
Test frame = new Test();
frame.setVisible(true);
}
public Test() {
setTitle("鸡兔同笼问题");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 200);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("请输入头的数量:");
label.setBounds(10, 32, 112, 15);
contentPane.add(label);
headField = new JTextField();
headField.setBounds(132, 29, 112, 21);
contentPane.add(headField);
headField.setColumns(10);
JLabel label_1 = new JLabel("请输入脚的数量:");
label_1.setBounds(10, 74, 112, 15);
contentPane.add(label_1);
footField = new JTextField();
footField.setBounds(132, 71, 112, 21);
contentPane.add(footField);
footField.setColumns(10);
JButton cal = new JButton("计算");
cal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int head = Integer.parseInt(headField.getText());
int foot = Integer.parseInt(footField.getText());
int rabitNum = (foot - head * 2) / 2;
int roosterNum = head - rabitNum;
result.setText("兔子有" + rabitNum + "只,鸡有" + roosterNum + "只");
}
});
cal.setBounds(281, 28, 93, 61);
contentPane.add(cal);
result = new JLabel("兔子有0只,鸡有0只");
result.setForeground(SystemColor.textHighlight);
result.setHorizontalAlignment(SwingConstants.RIGHT);
result.setBounds(10, 117, 364, 15);
contentPane.add(result);
JLabel lblNewLabel = new JLabel("公式:(总脚数-总头数*鸡的脚数)÷(兔的脚数-鸡的脚数)=兔的只数");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 12));
lblNewLabel.setBounds(10, 137, 364, 15);
contentPane.add(lblNewLabel);
this.setLocationRelativeTo(null);
}
}
java 数学公式
这个公式不复杂啊,统共就会用到Math里面的求次方的pow函数了。
因为这个属于java.lang包下的,调用方法就是Math.pow(a,b),相当于a^b了。
当然是一步一步算了,Java不是matlab,还不支持符号运算。double的精度应该够你用了,
如果要用任意精度的运算,才考虑用java.math包下的BigDecimal, BigInteger那些类,一般不需要。
你的公式用java写,如下:
double loanamount, monthlyInterestRate;
int numOfYears;
//上述变量的赋值 (略)
double result = (loanamount*monthlyInterestRate)/(1-1/Math.pow(1+monthlyInterestRate, numOfYears*12));