如何用Java编写四则运算程序?

作者&投稿:诗龙 (若有异议请与网页底部的电邮联系)
编写一个实现四则运算的JAVA程序~

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

public class Zhidao {

public static void main(String[] args) {
String condition = "";
Zhidao zhidao = new Zhidao();
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);
zhidao.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.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class Calculator extends JFrame implements ActionListener{ JLabel jl1=new JLabel("第一个数"); JLabel jl2=new JLabel("运算符"); JLabel jl3=new JLabel("第二个数"); JLabel jl4=new JLabel("结果:"); JTextField jtf1=new JTextField(8); JTextField jtf2=new JTextField(8); JTextField jtf3=new JTextField(8); String a[]={"+","-","*","/"}; JComboBox jcb=new JComboBox(a); JButton jb1=new JButton("计算"); JButton jb2=new JButton("退出"); JPanel jp=new JPanel(); Calculator(){ setTitle("计算器"); jb1.addActionListener(this); jb2.addActionListener(this); jp.setLayout(null); jl1.setBounds(30, 30, 80, 20); jl2.setBounds(110, 30, 80, 20); jl3.setBounds(190, 30, 80, 20); jtf1.setBounds(30, 60, 70, 20); jcb.setBounds(110, 60, 70,20); jtf2.setBounds(190, 60, 70, 20); jl4.setBounds(80, 110, 40, 20); jtf3.setBounds(110, 110, 100, 20); jb1.setBounds(60, 160, 60, 25); jb2.setBounds(170, 160, 60, 25); jp.add(jl1);jp.add(jl2);jp.add(jl3); jp.add(jtf1);jp.add(jcb);jp.add(jtf2); jp.add(jl4);jp.add(jtf3); jp.add(jb1);jp.add(jb2); add(jp,BorderLayout.CENTER); setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new Calculator(); } @Override public void actionPerformed(ActionEvent ae) { if(ae.getSource()==jb1){ String c=((String) jcb.getSelectedItem()).trim(); if(jtf1.getText().trim().equals("")){ JOptionPane.showMessageDialog(this, "第一个数不能为空"); jtf1.requestFocus(); return; } if(jtf2.getText().trim().equals("")){ JOptionPane.showMessageDialog(this, "第二个数不能为空"); jtf2.requestFocus(); return; } double num1=Double.parseDouble(jtf1.getText().trim()); double num2=Double.parseDouble(jtf2.getText().trim()); double num3 = 0; switch(c){ case "+":num3=num1+num2;break; case "-":num3=num1-num2;break; case "*":num3=num1*num2;break; case "/": if(num2==0){ JOptionPane.showMessageDialog(this, "除数不能为0"); jtf2.requestFocus(); return; }else{ num3=num1/num2; break; } } jtf3.setText(String.valueOf(num3)); } if(ae.getSource()==jb2){ System.exit(0); } }}运行结果

(首先建个类,把这些复制粘贴进去)
import java.awt.*;

import javax.swing.*;

public class F {
JFrame frame = new JFrame("计算机");

JPanel pl = new JPanel();

JPanel p2 = new JPanel();

static JTextField show = new JTextField();

static JButton b0 = new JButton("0");

static JButton b1 = new JButton("1");

static JButton b2 = new JButton("2");

static JButton b3 = new JButton("3");

static JButton b4 = new JButton("4");

static JButton b5 = new JButton("5");

static JButton b6 = new JButton("6");

static JButton b7 = new JButton("7");

static JButton b8 = new JButton("8");

static JButton b9 = new JButton("9");

JButton bjia = new JButton("+");

JButton bjian = new JButton("-");

JButton bcheng = new JButton("*");

JButton bchu = new JButton("/");

JButton bdian = new JButton(".");

JButton bdeng = new JButton("=");

JButton bqingchu = new JButton("清除");

public void y() {
pl.setLayout(new GridLayout(1, 1));
pl.add(show);
}

public void p() {
b1.addActionListener(new U());
b2.addActionListener(new U());
b3.addActionListener(new U());
b4.addActionListener(new U());
b5.addActionListener(new U());
b6.addActionListener(new U());
b7.addActionListener(new U());
b8.addActionListener(new U());
b9.addActionListener(new U());
b0.addActionListener(new U());

bjia.addActionListener(new Fu());
bjian.addActionListener(new Fu());
bcheng.addActionListener(new Fu());
bchu.addActionListener(new Fu());

bdeng.addActionListener(new Deng());
bqingchu.addActionListener(new Qing());

p2.setLayout(new GridLayout(6, 3));
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b0);
p2.add(bjia);
p2.add(bjian);
p2.add(bcheng);
p2.add(bchu);
p2.add(bdian);
p2.add(bqingchu);
p2.add(bdeng);
}

public void o() {
frame.setLayout(new BorderLayout());
frame.add(pl, BorderLayout.NORTH);
frame.add(p2, BorderLayout.CENTER);
frame.setSize(400, 300);
frame.setVisible(true);

}

public static void main(String[] args) {
F f = new F();
f.y();
f.p();
f.o();

}

}

(再新建个类 把这些也复制粘贴进去)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class U implements ActionListener {
public static String str = "";

public static String a = "";

public static String b = "";

public static String k = "";

public void actionPerformed(ActionEvent e) {
String w = e.getActionCommand();//字

if (k.equals("")) {
a += w;
F.show.setText(a);

} else {
b += w;
F.show.setText(b);
}

}

}

(再新建一个,把下面的复制粘贴)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Deng implements ActionListener {

public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(U.a);
int b = Integer.parseInt(U.b);
int c = 0;
if (U.k.equals("+")) {
c = a + b;

} else

if (U.k.equals("-")) {
c = a - b;

} else

if (U.k.equals("*")) {
c = a * b;

} else

if (U.k.equals("/")) {
c = a / b;

} else {

}

String d = String.valueOf(c);
F.show.setText(d);
U.a = d;
U.b = "";
U.k = "";
}
}

(在建一个 复制粘贴)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Fu implements ActionListener {

public void actionPerformed(ActionEvent e) {
String a = e.getActionCommand();//字
U.k = a;

}

}

(在建一个)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Qing implements ActionListener {

public void actionPerformed(ActionEvent e) {
U.a = "";
U.b = "";
U.k = "";
F.show.setText("");

}

}

提取运算数字和运算符号和运算结果三个对象,设计原型界面,主要运用SWITCH判断运算符号,分别调用其对象所对应的方法,传入运算数字,把结果返回给运算结果,向界面输出运算结果..搞定


运用JAVA中大数类实现大数的四则运算
import java.math.BigInteger;public class BigIntegerGet { public String getAdd(String Str1,String Str2){ String Str3=new String();BigInteger BigInt1=new BigInteger(Str1);BigInteger BigInt2=new BigInteger(Str2);BigInt1=BigInt1.add(BigInt2);Str3=BigInt1.toString();return Str3;...

用C、C++、C# 、JAVA实现四个数从大到小输出,谢谢你哦
int i,j,tmp;for

JAVA编程题中,四个学生小李,小张,小赵,和小王在打篮球,现在编写一个...
.num + 1;pl.get(nowPeople).num = total;System.out.println(pl.get(nowPeople).getName() + "抢到了,第" + total + "次球");\/\/ 何番目でアウトする if (total == 7) { System.out.println(pl.get(nowPeople).getName() + "不想玩了");pl.remove(nowPeople);} } } ...

请高手用java写一个Person类(姓名等四个属性),写一个车类\/\/打印**开*...
测试类这么写:public class PersonCarTester { public static void main(String[] args) { Person p = new Person(); p.setId(1001); p.setName("张三"); p.setSex("男"); p.setAge(29); Car car = new Car(); car.drive(p); car.driveCar("别克")...

随机输入四个数字,用加减乘除得到期望值,用java语言
package test;import java.util.Arrays;import java.util.Scanner;public class Compare { Scanner in = new Scanner(System.in);public static int z = 0;public static int expectedValue;public static int a;public static int b;public static int c;public static int d;public static void ...

java编程从键盘输入四个值用if语句求最大值 哪位大虾能帮帮忙 。急求...
不知道你为什么要用if来求,比较繁琐,math类中有max函数可以直接使用的,很方便,不过还是满足你的if吧。import java.util.Scanner;public class Method01{ public static void main(String args[]){ Scanner s = new Scanner(System.in);System.out.println("请输入四个数:");int a = s.next...

给出一个四位整数,用java编程输出各个位上的数字
public class TestBaiduKnow {public static void main(String args[]){int n = 1234;for(int i =1;i<=4;i++){int t = n%10;n = n\/10;if(i==1)System.out.println("个位:"+t);else if(i==2)System.out.println("十位:"+t);else if(i==3)System.out.println("百位:"...

用java随机生成四位验证码,只求编程代码
我自己做的系统里面用作验证码的JSP的 <%@page contentType="image\/jpeg;charset=utf-8"%> <%@page import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" %> <%@ page import="java.io.OutputStream" %> <%!Color getRandColor(int fc,int bc){ Random rd=new ...

定义一个方法,实现四则运算的功能。java
import java.text.DecimalFormat;import java.util.Scanner;public class Zhidao { public static void main(String[] args) { String condition = "";Zhidao zhidao = new Zhidao();do{ Scanner scanner = new Scanner(System.in);try{ System.out.print("请输入第一个数:");double x = ...

用java编写:1,2,3,4四个数字可以组成多少不重复的三位数(每个三位数中...
\/\/ 1,2,3,4四个数可以组成多少个无重复数字的三位数 \/\/考察多重循环 int count = 0;\/\/计数器 for (int i = 1; i <= 4; i++) {\/\/遍历个位的所有数 for (int j = 1; j <= 4; j++) {\/\/遍历十位的所有数 for (int k = 1; k <= 4; k++) {\/\/遍历百位的所有数 i...

柞水县19448403255: 求用JAVA编写数的四则运算程序 -
拓喻多潘: (首先建个类,把这些复制粘贴进去) import java.awt.*; import javax.swing.*; public class F { JFrame frame = new JFrame("计算机"); JPanel pl = new JPanel(); JPanel p2 = new JPanel(); static JTextField show = new JTextField(); static ...

柞水县19448403255: JAVA 实现四则运算
拓喻多潘: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random;public class ssss {/*** @param args* @throws IOException* @throws IOException*/ public static void main(String[] args) ...

柞水县19448403255: java实现四则运算 -
拓喻多潘: class Class1 {public static void main(String[] args){System.out.println("Hello World!");//中缀 => 后缀表达式String s = "( 1.9 + (20 + 41) / (25 * 11) - 3 ) * 2"; //中缀String S = ""; //后缀char[] Operators = new char[s.length()];int ...

柞水县19448403255: java怎么实现数字四则混合运算 -
拓喻多潘: 首先要知道四则混合运算是什么.其实就是加减乘除运算.java实现这个很简单.首先定义变量a,b用来运算.double a=1; b=2;//定义变量 system.out.println(a+b); //加分运算 system.out.println(a-b);//减法运算 system.out.println(a/b);//除法运算 system.out.println(a*b);//乘法运算

柞水县19448403255: 用JAVA语言编写一个图形界面的四则运算,简单点,谢谢哈,有急用 -
拓喻多潘: #include using namespace std; void compute() { for(int i = 0; ifor(int j = 0; jfor(int k = 0; k{ if((i+j+k == 100) && 5*i+3*j+k/3 == 100) cout } } int main() { compute(); system("pause"); return 0; } gongji=0 muji=25 xiaoji=75 gongji=4 muji=18 xiaoji=78 ...

柞水县19448403255: 求助写一个四则运算的JAVA程序,要求:有括号,私有,有计算过程(比如:1+2+3,输出结果为 1+2+3=3+3=6)新手学JAVA,培训老师给出的题,学不... -
拓喻多潘:[答案] package test; import java.util.*; public class demo2 { private static int intercePosition = 0; // 记录单个运算数据的长度 private static int[] intercePositionIndex = null; // 记录“(”的下标 private static int[] intercePositionEnd = null; // 记录“)”的下标 ...

柞水县19448403255: Java中怎么实现从键盘上输入几个数做四则运算 -
拓喻多潘: package test;import java.util.Scanner;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import javax.script.ScriptException;public class A1{ public static void main ( String[] args ) { ScriptEngineManager sem = new ...

柞水县19448403255: java加减乘除运算的程序怎么编啊
拓喻多潘: //JAVA编程:四则运算(接收用户输入的2个操作数,和运算符),计算之后,输出结果~~~~ import java.util.Scanner; public class 四则运算 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输...

柞水县19448403255: 定义一个方法,实现四则运算的功能.java -
拓喻多潘: import java.text.DecimalFormat; import java.util.Scanner;public class Zhidao { public static void main(String[] args) {String condition = "";Zhidao zhidao = new Zhidao();do{Scanner scanner = new Scanner(System.in);try{System.out.print...

柞水县19448403255: 运用java的基本库类,实现一个计算四则运算的程序 -
拓喻多潘: package Ex1; //Ex1为包名 import java.util.*; public class SzYs { //SzYs为类名 public static void main(String[] args){ double num1; double num2; String fuHao; Scanner input = new Scanner(System.in); System.out.print("输入第一个数的值:");...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网