本文目錄一覽:
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));
原創文章,作者:EXWL,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/140319.html