㈠ 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>