JAVA 编写计算器要代码最简单的学java的时候自己编的java算数运算程序代码,很简单java算数运算程序代码 , 能够连续输入计算式后进行计算
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**简易计算器java算数运算程序代码,能够进行简单的计算
*
* @see 2008.12.9
*/
public class CalculatorA
implements ActionListener{
private JFrame frame;
private JTextField field;
private JButton[] allButtons;
private JButton clearButton;
//private JButton backButton;
String result="";//保存结果
StringBuilder sb = new StringBuilder();//保存要进行的计算式
int x = 0;//用来判断上一次的事件类型
String str = "123 456-789*0.=/";
ArrayListString arrayList = new ArrayListString();//保存计算式,通过方法进行运算
public CalculatorA(){
frame = new JFrame("java算数运算程序代码我的计算器v1.1");
frame.setLocation(300,300);
field = new JTextField(25);
allButtons = new JButton[16];
for(int i=0;iallButtons.length;i){
allButtons[i]= new JButton(str.substring(i,i 1));
}
clearButton = new JButton("CLEAR");
//backButton = new JButton("——");
init();
setFondAndColor();
addEventHander();
}
public void init(){
frame.setLayout(new BorderLayout());
JPanel northPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel southPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
【java算数运算程序代码 java 算术运算符】northPanel.add(field);
for(int i=0;iallButtons.length;i){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
//southPanel.add(backButton);
frame.add(northPanel,BorderLayout.NORTH);
frame.add(centerPanel,BorderLayout.CENTER);
frame.add(southPanel,BorderLayout.SOUTH);
}
//设置输入字体
public void setFondAndColor(){
field.setFont(new Font("宋体",Font.BOLD,24));
field.setBackground(new Color(100,200,200));
field.setForeground(Color.RED);
//设置字体从右起始
field.setHorizontalAlignment(JTextField.RIGHT);
}
public void showMi(){
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addEventHander(){
for(int i=0;iallButtons.length;i){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(this);
//backButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();//取得当前事件返回的值
if("0123456789.".indexOf(str)!=-1){
if(x == 0){ //当x为0时表示还没有进行输入
result=str;
sb.append(str);
field.setText(str);
x = 1;
}
else if(x == 1){
result = resultstr;
sb.append(str);
field.setText(result);
x = 1;
}
else if(x == 2){
sb.delete(0,sb.length());
result = result str;
sb.append(str);
field.setText(result);
x = 1;
}
else if(x == 3){
result = str;
sb.delete(0,sb.length());
arrayList.clear();
field.setText(str);
sb.append(str);
field.setText(str);
x = 1;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
result = str;
sb.append(str);
field.setText(str);
x = 1;
}
else{
result = resultstr;
sb.append(str);
field.setText(result);
x = 1;
}
}
else if(" *-/".indexOf(str)!=-1){
if(x == 0){
field.setText("");
x = 2;
}
else if(x == 1){
result = resultstr;
arrayList.add(sb.toString());
arrayList.add(str);
sb.append(str);
field.setText(result);
x = 2;
}
else if(x == 2){
x = 2;
}
else if(x == 3){
field.setText(result str);
arrayList.add(result);
arrayList.add(str);
result = result str;
x = 2;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
x = 2;
}
else{
field.setText(result str);
arrayList.add(result);
arrayList.add(str);
result = result str;
x = 2;
}
}
else if("=".equals(str)){
if(x == 0){
field.setText("0");
arrayList.clear();
result = "0";
x = 3;
}
else if(x == 1){
try{
arrayList.add(sb.toString());
arrayList = getResult(arrayList);
result = arrayList.get(0);
field.setText(result);
arrayList.clear();
x = 3;
}catch(Exception e1){
field.setText("数据格式异常");
x = 0;
}
}
else if(x == 2){
field.setText("数据格式错误.....");
arrayList.clear();
x = 0;
}
else if(x == 3){
field.setText(result);
x = 3;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
x = 3;
}
else {
try{
arrayList.add(sb.toString());
arrayList = getResult(arrayList);
result = arrayList.get(0);
field.setText(result);
arrayList.clear();
x = 3;
}catch(Exception e1){
field.setText("数据格式异常");
x = 0;
}
}
}
else if("CLEAR".equals(str)){
arrayList.clear();
field.setText("0");
arrayList.add("0");
x = 4;
}
else{
if(result.length()1){
result = result.substring(0,result.length()-1);
if(sb.length()0){
sb.delete(sb.length()-1,sb.length());
}
else {
sb.delete(0,1);
}
field.setText(result);
x = 5;
}
else{
result = "";
sb.delete(0,sb.length());
arrayList.clear();
field.setText("0");
x = 0;
}
}
}
public static ArrayListString getResult(ArrayListString list){
String res = null;
String[] s = {"/","*","-"," "};
int i=0;
if(list.size()1){
for(;is.length;){
if(s[i].equals("/")){
for(int j=0;jlist.size();j){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))/Double.parseDouble(list.get(j 1)));
//本地的数据格式
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i;
}
else if(s[i].equals("*")){
for(int j=0;jlist.size();j){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))*Double.parseDouble(list.get(j 1)));
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i;
}
else if(s[i].equals("-")){
for(int j=0;jlist.size();j){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))-Double.parseDouble(list.get(j 1)));
NumberFormat nf = NumberFormat.getNumberInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i;
}
else {
for(int j=0;jlist.size();j){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1)) Double.parseDouble(list.get(j 1)));
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i;
}
}
}
return list;
}
//对数字字符串进行排除不必要符号
public static String getChange(String res){
String s_temp = "";
char[] c = new char[res.length()];
for(int k=0;kc.length;k){
c[k] = res.charAt(k);
}
for(int k=0;kc.length;k){
if((c[k]= '0'c[k]= '9')|| c[k] == '.'){
s_temp= c[k];
}
}
return s_temp;
}
public static void main(String[] args){
new CalculatorA().showMi();
}
}
如何用JAVA语言编写计算器小程序?具体代码如下:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculatorextends JFrame implements ActionListener{
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtf.setEditable(false);
String str="123 456-789*0.=/";
for(int i=0;iallButtons.length;i){
allButtons[i]=new JButton(str.substring(i,i 1));
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(jtf);
for(int i=0;i16;i){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("");
}
});
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理使计算器可以运行
String action=e.getActionCommand();
if(action==" "||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}
编写一个实现四则运算的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 写一个两个整数相加的程序代码如下:
public class Test {
public static int add(int a,int b){
return a b;
}
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数");
int a = scanner.nextInt();
System.out.println("请输入第二个数");
int b = scanner.nextInt();
System.out.println("和为:" add(a,b));
}
}
扩展资料
运算符
运算符是一些特殊的符号,主要用于数学函数、一些类型的赋值语句和逻辑比较方面 。
1、赋值运算符
赋值运算符以符号“=”表示,它是一个二元运算符(对两个操作数作处理),其功能是将右方操作数所含的值赋给左方的操作数 。
例如:
1int a = 100;
2、算术运算符
运算符说明:
“ ” 加;“-” 减;“*” 乘 ;“/”除 ; “%” 取余数
3、自增和自减运算符
自增和自减是单目运算符,可以放在操作元之前,也可以放在操作元之后 。操作元必须是一个整型或浮点型变量 。自增、自减运算符的作用是使变量的值增1或减1 。放在操作元前面的自增、自减运算符,会先将变量的值加1或减1 , 然后再使该变量参与表达式的运算 。放在操作元后面的自增、自减运算符,会先使变量参与表达式的运算 , 然后再将该变量的值加1或减1 。
例如:
假设a=5
1b=a;//先将a的值加1,然后赋值给b,此时a的值为6 , b的值为6
2b=a;//先将a的值赋值给b,再将a的值变为6,此时a的值为6,b的值为5
4、比较运算符
比较运算符属于二元运算符 , 用于程序中的变量之间,变量和自变量之间以及其他类型的信息之间的比较 。比较运算符的运算结果是boolean型 。当运算符对应的关系成立时 , 运算的结果为true,否则为false 。比较运算符共有6个,通常作为判断的依据用于条件语句中 。
运算符说明:
""比较左方是否大于右方
"" 比较左方是否小于右方
"=="比较左方是否等于右方
" = "比较左方是否大于等于右方
"= "比较左方是否小于等于右方
"!= "比较左方是否不等于右方
参考链接:Java(计算机编程语言)_百度百科
java算数运算程序代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 算术运算符、java算数运算程序代码的信息别忘了在本站进行查找喔 。
推荐阅读
- 高清画质赛车类游戏,高画质赛车端游
- u盘选择什么品牌,u盘选择什么品牌比较好
- python函数计算器 python计算器总结
- 关于模仿支付账单的小程序的信息
- 动物类角色扮演游戏投放,steam角色扮演动物
- 虎牙直播晶石是什么意思,虎牙主播晶哥怎么了
- kotlingo语言 kotlin语言能做什么
- 一万左右的电脑什么配置好,一万左右的电脑配置清单
- flutter下拉筛选,flutter 下拉列表