新聞中心
用java寫(xiě)個(gè)簡(jiǎn)單的加減計(jì)算器
不僅可以加減還可以乘除,代碼如下:
衡東ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書(shū)合作)期待與您的合作!
package 計(jì)算器;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
boolean flag = false;
JTextField jtf = new JTextField();
public static void main(String[] args) {
new Calculator();
}
public Calculator() {
setSize(300, 300);
setTitle("計(jì)算器");
Container c = getContentPane();
jtf.setHorizontalAlignment(JTextField.RIGHT);
JButton jb = new JButton("=");
jb.addActionListener(this);
JPanel jp = new JPanel();
c.add(jtf, BorderLayout.NORTH);
c.add(jp, BorderLayout.CENTER);
c.add(jb, BorderLayout.SOUTH);
jp.setLayout(new GridLayout(4, 4));
addButton(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void addButton(JPanel jp) {
JButton b = null;
b = new JButton("1");
b.addActionListener(this);
jp.add(b);
b = new JButton("2");
b.addActionListener(this);
jp.add(b);
b = new JButton("3");
b.addActionListener(this);
jp.add(b);
b = new JButton("+");
b.addActionListener(this);
jp.add(b);
b = new JButton("4");
b.addActionListener(this);
jp.add(b);
b = new JButton("5");
b.addActionListener(this);
jp.add(b);
b = new JButton("6");
b.addActionListener(this);
jp.add(b);
b = new JButton("-");
b.addActionListener(this);
jp.add(b);
b = new JButton("7");
b.addActionListener(this);
jp.add(b);
b = new JButton("8");
b.addActionListener(this);
jp.add(b);
b = new JButton("9");
b.addActionListener(this);
jp.add(b);
b = new JButton("*");
b.addActionListener(this);
jp.add(b);
b = new JButton("0");
b.addActionListener(this);
jp.add(b);
b = new JButton(".");
b.addActionListener(this);
jp.add(b);
b = new JButton("C");
b.addActionListener(this);
jp.add(b);
b = new JButton("/");
b.addActionListener(this);
jp.add(b);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "=") {
if (jtf.getText().matches("\\d+\\.?\\d*[\\+\\-\\*\\/]\\d+\\.?\\d*")) {
String s = jtf.getText();
Pattern p = Pattern.compile("\\d+\\.?\\d*");
Matcher m = p.matcher(s);
m.find();
double firstNum = Double.parseDouble(m.group());
m.find();
double secondNum = Double.parseDouble(m.group());
if (jtf.getText().matches("\\d+\\.?\\d*\\+\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum + secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\-\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum - secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\*\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum * secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\/\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum / secondNum));
}
} else {
JOptionPane.showMessageDialog(null, "請(qǐng)輸入正確表達(dá)式!");
}
flag = true;
} else if (e.getActionCommand() == "C") {
jtf.setText("");
} else {
if (flag) {
if (e.getActionCommand() != "+" e.getActionCommand() != "-"
e.getActionCommand() != "*"
e.getActionCommand() != "/") {
//System.out.println(e.getActionCommand() != "+");
jtf.setText("");
}
flag = false;
}
jtf.setText(jtf.getText() + e.getActionCommand());
}
}
}
關(guān)于java加減除的代碼問(wèn)題
import java.util.Scanner;
public class P {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
P prm=new P();
int a,b;
System.out.println("請(qǐng)輸入兩個(gè)整數(shù)(以空格分隔):");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("兩數(shù)相加后等于:"+prm.add(a,b));
System.out.println("兩數(shù)相減后等于:"+prm.sub(a,b));
System.out.println("兩數(shù)相除后等于:"+prm.divide(b,a));
sc.close();
}
int add(int a,int b) {
return a+b;
}
int sub(int a,int b) {
if(ab)
return a-b;
else
return b-a;
}
int divide(int a,int b) {
if(0==b)
System.out.println("錯(cuò)誤");
if(0==a)
System.out.println("除數(shù)不能為0");
return b/a;
}
}
java時(shí)間加減
附上代碼:
Date?date?=?new?Date("2014/1/10?18:20");
Date?date2?=?new?Date("2014/1/11?3:5");
long?temp?=?date2.getTime()?-?date.getTime();????//相差毫秒數(shù)
long?hours?=?temp?/?1000?/?3600;????????????????//相差小時(shí)數(shù)
long?temp2?=?temp?%?(1000?*?3600);
long?mins?=?temp2?/?1000?/?60;????????????????????//相差分鐘數(shù)
System.out.println("date2?與?date?相差"?+?hours?+?"小時(shí)"+?mins?+?"分鐘");
****************************************希望能夠幫助到你!************************************************
如果我的回答對(duì)你有幫助,
別忘了點(diǎn)擊我的回答下方【選為滿意答案】按鈕。
謝謝!
java中簡(jiǎn)單的加減乘除運(yùn)算
在a.jsp中寫(xiě)如下代碼:
請(qǐng)輸入
form action="b.jsp" method="post"
上課作業(yè)比率:input type="text" name="zy" / br /
上課實(shí)訓(xùn):input type="text" name="sksx" / br /
平時(shí)考勤: input type="text" name="kq" / br /
平時(shí)實(shí)訓(xùn):input type="text" name="pssx" / br /
/form
b.jsp中..
%String s = request.getParameter("zy")
//.......
%
懶得寫(xiě)了太麻煩@_@
怎么用java編程,實(shí)現(xiàn)分?jǐn)?shù)的加減乘除運(yùn)算?
java編程實(shí)現(xiàn)分?jǐn)?shù)的加減乘除運(yùn)算的步驟如下:
1、打開(kāi)eclipse,創(chuàng)建一個(gè)Java工程,在此工程里新建一個(gè)類(lèi);
2、在新建的類(lèi)中,添加4個(gè)運(yùn)算類(lèi);
3、在主方法中調(diào)用對(duì)應(yīng)的方法即可完成分?jǐn)?shù)的加減乘除運(yùn)算了。
具體實(shí)現(xiàn)代碼如下:
public?class?Demo?{
public?static?void?main(String[]?args)?{
System.out.println(jia(1,?2));
System.out.println(jian(1,?2));
System.out.println(cheng(1,?2));
System.out.println(chu(1,?2));
}
//加法運(yùn)算
private?static?float?jia(float?x,float?y)?{
return?x?+?y;
}
//減法運(yùn)算
private?static?float?jian(float?x,float?y)?{
return?x?-?y;
}
//乘法運(yùn)算
private?static?float?cheng(float?x,float?y)?{
return?x?*?y;
}
//除法運(yùn)算
private?static?float?chu(float?x,float?y)?{
return?x?/?y;
}
}
本文標(biāo)題:java簡(jiǎn)單加減代碼,java 加減
當(dāng)前URL:http://fisionsoft.com.cn/article/dsipesp.html