導航:首頁 > 編程語言 > java工廠模式設計四則運算

java工廠模式設計四則運算

發布時間:2024-01-17 11:51:45

java製作一個簡單的計算器。能實現兩個數的加、減、乘、除四則運算

importjava.util.*;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.applet.*;

{
booleani=true;
privateJButtonnum0=newJButton("0");
privateJButtonnum1=newJButton("1");
privateJButtonnum2=newJButton("2");
privateJButtonnum3=newJButton("3");
privateJButtonnum4=newJButton("4");
privateJButtonnum5=newJButton("5");
privateJButtonnum6=newJButton("6");
privateJButtonnum7=newJButton("7");
privateJButtonnum8=newJButton("8");
privateJButtonnum9=newJButton("9");
privateJButtonzuok=newJButton("(");
privateJButtonyouk=newJButton(")");
privateJButtondian=newJButton(".");
privateJButtonNULL=newJButton("N");
privateJButtonplu=newJButton("+");
privateJButtonmin=newJButton("-");
privateJButtonmul=newJButton("x");
privateJButtondiv=newJButton("/");
privateJButtonequ=newJButton("=");
privateJButtoncle=newJButton("C");//清除
privateJTextFieldspace=newJTextField(15);

publicvoidinit(){
JPaneltext=newJPanel();
text.setLayout(newFlowLayout());
text.add(space);
JPanelbuttons=newJPanel();
buttons.setLayout(newGridLayout(5,4));
buttons.add(num9);
buttons.add(num8);
buttons.add(num7);
buttons.add(plu);
buttons.add(num6);
buttons.add(num5);
buttons.add(num4);
buttons.add(min);
buttons.add(num3);
buttons.add(num2);
buttons.add(num1);
buttons.add(mul);
buttons.add(num0);
buttons.add(cle);
buttons.add(equ);
buttons.add(div);

(num9).addActionListener(this);
(num8).addActionListener(this);
(num7).addActionListener(this);
(num6).addActionListener(this);
(num5).addActionListener(this);
(num4).addActionListener(this);
(num3).addActionListener(this);
(num2).addActionListener(this);
(num1).addActionListener(this);
(num0).addActionListener(this);
(plu).addActionListener(this);
(min).addActionListener(this);
(mul).addActionListener(this);
(div).addActionListener(this);
(equ).addActionListener(this);
(cle).addActionListener(this);

setLayout(newBorderLayout());
add("North",text);
add("South",buttons);
space.setText("0");
}

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==num9){
if(i==true){
space.setText("9");
i=false;
}else
space.setText(space.getText()+'9');
}
if(e.getSource()==num8){
if(i==true){
space.setText("8");
i=false;
}else
space.setText(space.getText()+'8');
}
if(e.getSource()==num7){
if(i==true){
space.setText("7");
i=false;
}else
space.setText(space.getText()+'7');
}
if(e.getSource()==num6){
if(i==true){
space.setText("6");
i=false;
}else
space.setText(space.getText()+'6');
}
if(e.getSource()==num5){
if(i==true){
space.setText("5");
i=false;
}else
space.setText(space.getText()+'5');
}
if(e.getSource()==num4){
if(i==true){
space.setText("4");
i=false;
}else
space.setText(space.getText()+'4');
}
if(e.getSource()==num3){
if(i==true){
space.setText("3");
i=false;
}else
space.setText(space.getText()+'3');
}
if(e.getSource()==num2){
if(i==true){
space.setText("2");
i=false;
}else
space.setText(space.getText()+'2');
}
if(e.getSource()==num1){
if(i==true){
space.setText("1");
i=false;
}else
space.setText(space.getText()+'1');
}
if(e.getSource()==num0){
if(i==true){
space.setText("0");
i=false;
}else
space.setText(space.getText()+'0');
}

if(e.getSource()==plu){
space.setText(space.getText()+'+');
i=false;
}
if(e.getSource()==min){
space.setText(space.getText()+'-');
i=false;
}
if(e.getSource()==mul){
space.setText(space.getText()+'*');
i=false;
}
if(e.getSource()==div){
space.setText(space.getText()+'/');
i=false;
}
if(e.getSource()==equ){
space.setText(String.valueOf(Calculator(space.getText())));
i=true;
}
if(e.getSource()==cle){
space.setText("0");
i=true;
}

}

publicdoubleCalculator(Stringf)//科學計算
{
inti=0,j=0,k;
charc;
StringBuffers=newStringBuffer();
s.append(f);
s.append('=');
Stringformula=s.toString();
char[]anArray;
anArray=newchar[50];
Stack<Character>mystack=newStack<Character>();
while(formula.charAt(i)!='='){
c=formula.charAt(i);
switch(c){

case'+':
case'-':
while(!mystack.empty()&&mystack.peek().charValue()!='('){
anArray[j++]=mystack.pop().charValue();

}
mystack.push(newCharacter(c));
i++;
break;
case'*':
case'/':
while(!mystack.empty()
&&(mystack.peek().charValue()=='*'||mystack.peek()
.charValue()=='/')){
anArray[j++]=mystack.pop().charValue();
}
mystack.push(newCharacter(c));
i++;
break;
case'':
i++;
break;
default:
while((c>='0'&&c<='9')||c=='.'){
anArray[j++]=c;
i++;
c=formula.charAt(i);
}
anArray[j++]='#';
break;
}
}
while(!(mystack.empty()))
anArray[j++]=mystack.pop().charValue();

i=0;
intcount;
doublea,b,d;
Stack<Double>mystack1=newStack<Double>();
while(i<j){
c=anArray[i];
switch(c){
case'+':
a=mystack1.pop().doubleValue();
b=mystack1.pop().doubleValue();
d=b+a;
mystack1.push(newDouble(d));
i++;
break;
case'-':
a=mystack1.pop().doubleValue();
b=mystack1.pop().doubleValue();
d=b-a;
mystack1.push(newDouble(d));
i++;
break;
case'*':
a=mystack1.pop().doubleValue();
b=mystack1.pop().doubleValue();
d=b*a;
mystack1.push(newDouble(d));
i++;
break;
case'/':
a=mystack1.pop().doubleValue();
b=mystack1.pop().doubleValue();
if(a!=0){
d=b/a;
mystack1.push(newDouble(d));
i++;
}else{
System.out.println("Error!");
}
break;
default:
d=0;
count=0;
while((c>='0'&&c<='9')){
d=10*d+c-'0';
i++;
c=anArray[i];
}
if(c=='.'){
i++;
c=anArray[i];
while((c>='0'&&c<='9')){
count++;
d=d+(c-'0')/Math.pow(10,count);
i++;
c=anArray[i];
}
}
if(c=='#')
mystack1.push(newDouble(d));
i++;
break;
}
}
return(mystack1.peek().doubleValue());
}
}

你看看這個合適不?

㈡ 用java設計,鍵盤輸入一個四則運算等式,判斷運算是否正確

		Scannersc=newScanner(System.in);
Stringstr=sc.nextLine();
String[]arr=str.split("=");//根據等號分隔等式兩邊的字元串
ScriptEngineManagermanager=newScriptEngineManager();
ScriptEngineengine=manager.getEngineByName("js");

try{
Objectresult0=engine.eval(arr[0]);
Objectresult1=engine.eval(arr[1]);
System.out.println("等式左邊計算結果:"+result0);
System.out.println("等式右邊計算結果:"+result1);
if(result0==result1){
System.out.println("運算正確");
}else{
System.out.println("運算錯誤");
}
}catch(ScriptExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

主要使用的時候下面這些庫

㈢ 編寫一個實現四則運算的JAVA程序

import java.text.DecimalFormat;
import java.util.Scanner;

public class Zhi {

public static void main(String[] args) {
condition = "";
Zhi = new Zhi();
do{
Scanner scanner = new Scanner(System.in);
try{
System.out.print("請輸入第一個數:");
double x = scanner.nextDouble();
System.out.print("請輸入第二個數:");
double y = scanner.nextDouble();
System.out.print("請輸入運算符:");
String s = scanner.next();
char z = s.charAt(0);
.yunsuan(x, y, z);
}catch(Exception e){
System.out.println("請輸入正確的數據!");
}
System.out.print("是否繼續?continue:繼續,任意字元:結束");
condition = scanner.next();

}while("continue".equals(condition));
}

public static void yunsuan(double x,double y,Character z){
DecimalFormat r=new DecimalFormat();
r.applyPattern("#0.00");
if(z.equals('+')){
System.out.println(x+"+"+y+"=" + r.format((x+y)));
} else if(z.equals('-')){
System.out.println(x+"-"+y+"=" + r.format((x-y)));
} else if(z.equals('*')){
System.out.println(x+"*"+y+"=" + r.format((x*y)));
} else if(z.equals('/')){
if(y==0){
System.out.println("被除數不能為0");
} else{
System.out.println(x+"/"+y+"=" + r.format((x/y)));
}

}else{
System.out.println("無法識別改運算符");
}
}

}

㈣ java 寫的計算器源代碼只實現加減乘除四則運算即可

import java.awt.*; //引入AWT包
import java.awt.event.*; //引入awt.event包
import javax.swing.*; //引入javax.swing包
public class JSQ extends JFrame implements ActionListener //事件監聽器為介面
{
private String name[] =
{"0","1", "2","+","3","4","5","-","6","7","8","*","9",".","=","/"}; //創建按鈕數組
private String extend[] =
{"x^2","ln","back","sin","cos","tan","sqrt","關閉","清空"}; String s="";
int flag=0;
double x;
final double pi = 3.14159265358979323846;
private JButton button[] = new JButton[name.length]; //創建按鈕數組

private JButton button1[] = new JButton[extend.length]; //創建功能鍵按鈕數組

JLabel dis_show = new JLabel("結果:");
JTextField w = new JTextField(12); //文本長度為12
public JSQ2() //構造函數
{
super("計算機");
setSize(200,290);
Container c = getContentPane();
c.setLayout(new GridLayout(2,1,0,0));
JPanel result = new JPanel();
result.add(dis_show);
result.add(w);
for(int i= 0;i<extend.length;i++) //循環
{
button1[i] = new JButton(extend[i]);
result.add(button1[i]);
}
JPanel border = new JPanel();
for(int i= 0;i<name.length;i++)
{
button[i] = new JButton(name[i]);
border.add(button[i]);
}
c.add(result);
c.add(border);
for(int i= 0;i<name.length;i++)
{
button[i].addActionListener(this);
}
for(int i= 0;i<extend.length;i++)
{
button1[i].addActionListener(this);
}
show();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button[3])
{
x=Double.parseDouble(s);

flag=1;
w.setText("");
s="";
}
else if (e.getSource()==button[7])
{
x=Double.parseDouble(s);
flag=2;
w.setText("");
s="";
}
else if (e.getSource()==button[11])
{
x=Double.parseDouble(s);
flag=3;
w.setText("");
s="";
}
else if (e.getSource()==button[15])
{
x=Double.parseDouble(s);
flag=4;
w.setText("");
s="";
}
else if (e.getSource()==button[14])
{
switch(flag)
{
case 1:
{

x=x+Double.parseDouble(s);
String s=String.valueOf(x);
w.setText(s);
break;
}
case 2:
{
x=x-Double.parseDouble(s);
String s=String.valueOf(x);
w.setText(s);
break;
}
case 3:
{
x=x*Double.parseDouble(s);
String s=Double.toString(x);
w.setText(s);
break;
}
case 4:
{
if(Double.parseDouble(s)==0)
{
w.setText("除數不能為0");
break;
}
x=x/Double.parseDouble(s);
String s=String.valueOf(x);
w.setText(s);
break;
}
}
}
else if(e.getSource()==button1[2])
{
StringBuffer str = new StringBuffer(w.getText());
int n=str.length();
int m=n-1;
s = String.valueOf(str.delete(m,n));
w.setText(s);
}
else if(e.getSource()==button1[8])
{
w.setText("");
s="";
flag=0;
}
else if(e.getSource()==button1[0])
{
x=Double.parseDouble(s);
x=x*x;
String s=String.valueOf(x);
w.setText(s);
}
else if(e.getSource()==button1[1])
{
x=Math.log(Double.parseDouble(s));
String s=String.valueOf(x);
w.setText(s);
}
else if(e.getSource()==button1[3])
{
x=Math.sin(Double.parseDouble(s)*pi/180);
String s=String.valueOf(x);
w.setText(s);
}
else if(e.getSource()==button1[6])
{
x=Math.sqrt(Double.parseDouble(s));
String s=String.valueOf(x);
w.setText(s);
}
else if(e.getSource()==button1[4])
{
x=Math.cos(Double.parseDouble(s)*pi/180);
String s=String.valueOf(x);
w.setText(s);
}
else if(e.getSource()==button1[5])
{
x=Math.tan(Double.parseDouble(s)*pi/180);
String s=String.valueOf(x);
w.setText(s);
}
else if(e.getSource()==button1[7])
{
System.exit(0);
}
else
{
s=s+e.getActionCommand();
w.setText(s);
}
}
public static void main(String[] args)
{
JSQ2 bun = new JSQ();
bun.addWindowListener(new WindowAdapter()
{
public void windowListener(WindowEvent e)
{
System.exit(0);
}
});
}
}
太多啦,懶得解釋啦!總的來說就是先創建數組按鈕,然後放入面板,面板再加入FRAME,然後實現監聽器的方法(按鈕功能)!其中要用到布局管理器,具體查看API!

㈤ 200分 急求java+JavaBean 實現簡單的四則運算(不是混合運算)

package com;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Calculate {

public Calculate() {
}

public String getResult(String op1,String op2,String op){
Double res=0.0;
if(op.equals("+")){
res=Double.valueOf(op1)+Double.valueOf(op2);
}else if(op.equals("-")){
res=Double.valueOf(op1)-Double.valueOf(op2);
}else if(op.equals("*")){
res=Double.valueOf(op1)*Double.valueOf(op2);
}else if(op.equals("/")){
res=Double.valueOf(op1)/Double.valueOf(op2);
}else{
return "操作不當";
}
String res1=String.valueOf(res);
if(res1.indexOf(".")!=-1){
Pattern p=Pattern.compile("0+");
Matcher m=p.matcher(res1.split("\\.")[1]);
if(m.matches())
return res1.split("\\.")[0];
}
return res1;
}
}

main.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<jsp:useBean id="myBean" class="com.Calculate" scope="session"/>
<%
String op1=request.getParameter("op1");
String op2=request.getParameter("op2");
String op=request.getParameter("op");
String res="";
if(op1!=null&&op2!=null&&op!=null)
res=myBean.getResult(op1,op2,op);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>

<form action="main.jsp" method="post" name="myform">
<input type="text" name="op1" />
<select name="op">
<option value="+" selected="selected">+</option>
<option value="-" >-</option>
<option value="*" >*</option>
<option value="/" >/</option>
</select>
<input type="text" name="op2" />
<input type="submit" value="="/>
<span id="result" ></span>
</form>
<script language="javascript">
document.getElementById("result").innerHTML="<%=res%>";
</script>
</body>
</html>

閱讀全文

與java工廠模式設計四則運算相關的資料

熱點內容
mdfldf是什麼文件 瀏覽:569
文件在桌面怎麼刪除干凈 瀏覽:439
馬蘭士67cd機版本 瀏覽:542
javaweb爬蟲程序 瀏覽:537
word中千位分隔符 瀏覽:392
迷你編程七天任務的地圖怎麼過 瀏覽:844
word2003格式不對 瀏覽:86
百度雲怎麼編輯文件在哪裡 瀏覽:304
起名app數據哪裡來的 瀏覽:888
微信怎麼去泡妞 瀏覽:52
百度廣告html代碼 瀏覽:244
qq瀏覽器轉換完成後的文件在哪裡 瀏覽:623
jsp中的session 瀏覽:621
壓縮完了文件去哪裡找 瀏覽:380
武裝突襲3浩方聯機版本 瀏覽:674
網路機頂盒移動網路 瀏覽:391
iphone手機百度雲怎麼保存到qq 瀏覽:148
資料庫設計與實踐讀後感 瀏覽:112
js對象是什麼 瀏覽:744
網頁文件存pdf 瀏覽:567

友情鏈接