不僅可以加減還可以乘除,代碼如下:

專注于為中小企業(yè)提供成都網(wǎng)站制作、成都做網(wǎng)站服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)馬鞍山免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
package 計算器;
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("計算器");
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, "請輸入正確表達式!");
}
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());
}
}
}
import?java.util.Scanner;
public?class?JianJian?{
public?static?void?main(String[]?args)?{
System.out.println("一百以內(nèi)的加減法");
System.out.println("輸入-1,退出系統(tǒng)");
Scanner?sc?=?new?Scanner(System.in);
int?score?=?0;
int?nums?=?0;
while?(true)?{
int?x?=?(int)?(Math.random()?*?2);//隨機一個0,1的數(shù)字,當數(shù)字是1的時候,輸出加法,當數(shù)字是0的時候,輸出減法
int?a?=?(int)?(Math.random()?*?100);
int?b?=?(int)?(Math.random()?*?100);
if?(x?==?1)?{
System.out.print(a?+?"+"?+?b?+?"=");
int?c?=?sc.nextInt();
if?(c?==?-1)?{
break;
}
nums++;
if?(c?==?(a?+?b))?{
score?=?score?+?10;
}
}?else?{
if?(a??b)?{//?排除a-b0的情況.保證結(jié)果都是大于0的
b?=?a?+?b;
a?=?b?-?a;
b?=?b?-?a;
}
System.out.print(a?+?"-"?+?b?+?"=");
int?c?=?sc.nextInt();
if?(c?==?-1)?{
break;
}
nums++;
if?(c?==?(a?-?b))?{
score?=?score?+?10;
}
}
}
System.out.println("做了"?+?nums?+?"道題目,得分:?"?+?score);
}
}
輸出
一百以內(nèi)的加減法
輸入-1,退出系統(tǒng)
87-18=59
23-6=17
72-30=42
97+14=-1//-1表示退出系統(tǒng),所以不算做一道題
做了3道題目,得分:?20
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("請輸入兩個整數(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("錯誤");
if(0==a)
System.out.println("除數(shù)不能為0");
return b/a;
}
}
當前題目:java做加減代碼 java如何實現(xiàn)加減乘除
瀏覽地址:http://chinadenli.net/article12/ddodcdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、外貿(mào)建站、做網(wǎng)站、網(wǎng)站維護、響應(yīng)式網(wǎng)站、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)