java加减乘除运算的程序怎么编啊

java加减乘除运算的程序怎么编啊,第1张

//JAVA编程:四则运算(接收用户输入的2个 *** 作数,和运算符),计算之后,输出结果~~~~

import javautilScanner;

public class 四则运算 {

public static void main(String[] args) {

Scanner sc = new Scanner(Systemin);

Systemoutprint("请输入第一个数字:");

int a = scnextInt();

Systemoutprint("请输入运算符号:");

String str = scnext();

char ch = strcharAt(0);

Systemoutprint("请输入第二个数字:");

int b = scnextInt();

switch(ch)

{

case '+':

Systemoutprintln(a+"+"+ b + "="+(a+b));

break;

case '-':

Systemoutprintln(a+"-"+ b+ "="+(a-b));

break;

case '':

Systemoutprintln(a+""+ b+ "="+(ab));

break;

case '/':

if(b==0){

Systemoutprintln("被除数为零,运算无意义!");

break;

}

else {

Systemoutprintln(a+"/"+ b+ " = "+(a/b));

break;

}

default:

Systemoutprintln("运算符是无意义字符!");

break;

}

}

}

望采纳~~~~~~~~

public

MyCalculator()

{

f

=

new

JFrame("计算器ByMdou");

Container

contentPane

=

fgetContentPane();

/菜单的创建开始/

JMenuBar

mBar

=

new

JMenuBar();

mBarsetOpaque(true);

mEdit

=

new

JMenu("编辑(E)");

mEditsetMnemonic(KeyEventVK_E);

mCopy

=

new

JMenuItem("复制(C)");

mEditadd(mCopy);

mPaste

=

new

JMenuItem("粘贴(P)");

mEditadd(mPaste);

mView

=

new

JMenu("查看(V)");

mViewsetMnemonic(KeyEventVK_V);

mViewadd(new

JMenuItem("标准型"));

mViewadd(new

JMenuItem("科学型"));

mViewaddSeparator();

mViewadd(new

JMenuItem("查看分组"));

mHelp

=

new

JMenu("帮助(H)");

mHelpsetMnemonic(KeyEventVK_H);

mHelpadd(new

JMenuItem("帮助主题"));

mHelpaddSeparator();

mHelpadd(new

JMenuItem("关于计算器"));

mBaradd(mEdit);

mBaradd(mView);

mBaradd(mHelp);

fsetJMenuBar(mBar);

contentPanesetLayout(new

BorderLayout

());

JPanel

pTop

=

new

JPanel();

tResult

=

new

JTextField("0",26);

tResultsetHorizontalAlignment(JTextFieldRIGHT

);

tResultsetEditable(false);

pTopadd(tResult);

contentPaneadd(pTop,BorderLayoutNORTH);

JPanel

pBottom

=

new

JPanel();

pBottomsetLayout(new

BorderLayout());

JPanel

pLeft

=

new

JPanel();

pLeftsetLayout(new

GridLayout(5,1,3,3));

bM

=

new

JButton("

");

bMsetEnabled(false);

pLeftadd(bM);

bOther

=

new

JButton("MC");

bOtheraddActionListener(this);

bOthersetForeground(ColorRED);

bOthersetMargin(new

Insets(3,2,3,2));

pLeftadd(bOther);

bOther

=

new

JButton("MR");

bOtheraddActionListener(this);

bOthersetForeground(ColorRED);

bOthersetMargin(new

Insets(3,2,3,2));

pLeftadd(bOther);

bOther

=

new

JButton("MS");

bOtheraddActionListener(this);

bOthersetForeground(ColorRED);

bOthersetMargin(new

Insets(3,2,3,2));

pLeftadd(bOther);

bOther

=

new

JButton("M+");

bOtheraddActionListener(this);

bOthersetForeground(ColorRED);

bOthersetMargin(new

Insets(3,2,3,2));

pLeftadd(bOther);

有一个现成的你看看吧

import javaawteventActionEvent;

public class Application extends JFrame {

protected String str = "";

protected boolean isChar = true;

protected boolean isEqual = false;

protected JTextField textField;

public Application() {

Listener listerner = new Listener(this);

getContentPane()setLayout(null);

JButton button = new JButton("7");

buttonaddActionListener(listerner);

buttonsetBounds(12, 69, 43, 27);

getContentPane()add(button);

textField = new JTextField();

textFieldsetText("0");

textFieldsetEditable(false);

textFieldsetHorizontalAlignment(JTextFieldRIGHT);

textFieldsetBounds(12, 22, 377, 27);

getContentPane()add(textField);

textFieldsetColumns(10);

JButton button_1 = new JButton("8");

button_1addActionListener(listerner);

button_1setBounds(103, 69, 43, 27);

getContentPane()add(button_1);

JButton button_2 = new JButton("9");

button_2addActionListener(listerner);

button_2setBounds(182, 69, 43, 27);

getContentPane()add(button_2);

JButton button_3 = new JButton("4");

button_3addActionListener(listerner);

button_3setBounds(12, 106, 43, 27);

getContentPane()add(button_3);

JButton button_4 = new JButton("5");

button_4addActionListener(listerner);

button_4setBounds(103, 106, 43, 27);

getContentPane()add(button_4);

JButton button_5 = new JButton("6");

button_5addActionListener(listerner);

button_5setBounds(182, 106, 43, 27);

getContentPane()add(button_5);

JButton button_6 = new JButton("1");

button_6addActionListener(listerner);

button_6setBounds(12, 143, 43, 27);

getContentPane()add(button_6);

JButton button_7 = new JButton("2");

button_7addActionListener(listerner);

button_7setBounds(103, 143, 43, 27);

getContentPane()add(button_7);

JButton button_8 = new JButton("3");

button_8addActionListener(listerner);

button_8setBounds(182, 143, 43, 27);

getContentPane()add(button_8);

JButton button_9 = new JButton("+");

button_9addActionListener(listerner);

button_9setBounds(269, 72, 43, 27);

getContentPane()add(button_9);

JButton button_10 = new JButton("-");

button_10addActionListener(listerner);

button_10setBounds(346, 72, 43, 27);

getContentPane()add(button_10);

JButton button_11 = new JButton("");

button_11addActionListener(listerner);

button_11setBounds(269, 109, 43, 27);

getContentPane()add(button_11);

JButton button_12 = new JButton("/");

button_12addActionListener(listerner);

button_12setBounds(346, 109, 43, 27);

getContentPane()add(button_12);

JButton button_13 = new JButton("=");

button_13addActionListener(listerner);

button_13setBounds(346, 143, 43, 27);

getContentPane()add(button_13);

JButton button_14 = new JButton("0");

button_14addActionListener(listerner);

button_14setBounds(103, 180, 43, 27);

getContentPane()add(button_14);

JButton btnReset = new JButton("reset");

btnResetaddActionListener(listerner);

btnResetsetBounds(269, 180, 118, 27);

getContentPane()add(btnReset);

JButton button_15 = new JButton("");

button_15addActionListener(listerner);

button_15setBounds(269, 146, 43, 27);

getContentPane()add(button_15);

thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

thissetResizable(false);

thissetSize(442, 260);

thissetLocationRelativeTo(null);

thissetVisible(true);

}

public static void main(String[] args) {

new Application();

}

}

class Listener implements ActionListener {

private Application app = null;

public Listener(Application app) {

thisapp = app;

}

public void actionPerformed(ActionEvent e) {

String value = egetActionCommand();

if (valuematches("[0-9]")) {

if (appisChar) {

apptextFieldsetText("");

appisChar = false;

}

if (appisEqual && appstrmatches("[0-9]")) {

appstr = "";

appisEqual = false;

}

appstr += value;

apptextFieldsetText(apptextFieldgetText() + value);

} else if (valuematches("[\\+\\-\\/]")) {

if (!appstrsubstring(appstrlength() - 1)

matches("[\\+\\-\\/]")) {

appstr += value;

appisChar = true;

}

} else if ("="equals(value)) {

appisEqual = true;

if (appstrsubstring(appstrlength() - 1)matches("[\\+\\-]")) {

appstr += "0";

} else if (appstrsubstring(appstrlength() - 1)

matches("[\\/]")) {

appstr += "1";

}

Interpreter bsh = new Interpreter();

String obj = null;

try {

obj = bsheval(appstr)toString();

} catch (Exception exception) {

Systemoutprintln(exceptiongetMessage());

}

Systemoutprintln(appstr);

apptextFieldsetText(obj);

appstr = obj;

appisChar = true;

} else {

appstr = "";

apptextFieldsetText("0");

}

}

}

给你一个参考,希望不要被百度吞了当晚餐

import javaawtBorderLayout;

import javaawtGridLayout;

import javaawteventMouseEvent;

import javaawteventMouseListener;

import javatextDecimalFormat;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJPanel;

import javaxswingJTextField;

public class Calculator {

//计算器面板

private JFrame f = new JFrame("Calculator");

//输入面扳

private JPanel inputPanel = new JPanel();

//加减乘除面板

private JPanel operationPanel = new JPanel();

//数字面板

private JPanel buttonsPanel = new JPanel();

//输入数据文本框

private JTextField input = new JTextField(20);

//退格键

private JButton backspace = new JButton("BackSpace");

//清空

private JButton CE = new JButton("CE ");

//删除

private JButton C = new JButton("C ");

//四则运算符号键

private JButton add = new JButton("+");

private JButton sub = new JButton("-");

private JButton mul = new JButton("");

private JButton div = new JButton("/");

//小数点

private JButton decimal = new JButton("");

//等号

private JButton equal = new JButton("=");

//数字键

private JButton zero = new JButton("0");

private JButton one = new JButton("1");

private JButton two = new JButton("2");

private JButton three = new JButton("3");

private JButton four = new JButton("4");

private JButton five = new JButton("5");

private JButton six = new JButton("6");

private JButton seven = new JButton("7");

private JButton eight = new JButton("8");

private JButton nine = new JButton("9");

private String num1 = "";//保存第一个运算数字

private String operator = "";//保存运算符号

public static void main(String[] args) {

new Calculator();//new计算器实例

}

public Calculator(){

//添加组件,布局

inputPaneladd(input);

fadd(inputPanel, BorderLayoutNORTH);

operationPaneladd(backspace);

operationPaneladd(CE);

operationPaneladd(C);

fadd(operationPanel, BorderLayoutCENTER);

buttonsPaneladd(add);

buttonsPaneladd(sub);

buttonsPaneladd(mul);

buttonsPaneladd(div);

buttonsPaneladd(one);

buttonsPaneladd(two);

buttonsPaneladd(three);

buttonsPaneladd(zero);

buttonsPaneladd(four);

buttonsPaneladd(five);

buttonsPaneladd(six);

buttonsPaneladd(decimal);

buttonsPaneladd(seven);

buttonsPaneladd(eight);

buttonsPaneladd(nine);

buttonsPaneladd(equal);

buttonsPanelsetLayout(new GridLayout(4, 4));

fadd(buttonsPanel, BorderLayoutSOUTH);

//注册各个组件监听事件

backspaceaddMouseListener(new OperationMouseListener());

CEaddMouseListener(new OperationMouseListener());

CaddMouseListener(new OperationMouseListener());

decimaladdMouseListener(new OperationMouseListener());

equaladdMouseListener(new OperationMouseListener());

//注册四则运算监听

addaddMouseListener(new CalcMouseListener());

subaddMouseListener(new CalcMouseListener());

muladdMouseListener(new CalcMouseListener());

divaddMouseListener(new CalcMouseListener());

//注册数字监听事件

zeroaddMouseListener(new NumberMouseListener());

oneaddMouseListener(new NumberMouseListener());

twoaddMouseListener(new NumberMouseListener());

threeaddMouseListener(new NumberMouseListener());

fouraddMouseListener(new NumberMouseListener());

fiveaddMouseListener(new NumberMouseListener());

sixaddMouseListener(new NumberMouseListener());

sevenaddMouseListener(new NumberMouseListener());

eightaddMouseListener(new NumberMouseListener());

nineaddMouseListener(new NumberMouseListener());

fsetVisible(true);

fpack();

fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

}

private class NumberMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {

if(inputgetText()trim()equals("0")){//如果文本框已经是0,结果还是0

inputsetText(((JButton)egetSource())getText()trim());

}else{//否则的话,把0添加到后面,譬如文本框是1,结果就为10

inputsetText(inputgetText()concat(((JButton)egetSource())getText()trim()));

}

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

private class CalcMouseListener implements MouseListener{

//如果输入的是运算符号,保存第一个结果和运算符号

public void mouseClicked(MouseEvent e) {

num1 = inputgetText()trim();inputsetText("");

operator = ((JButton)egetSource())getText()trim();

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

private class OperationMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {

if(egetSource() == backspace){//退格键,删除一个直到没有字符删除

String inputText = inputgetText();

if(inputTextlength() > 0){

inputsetText(inputTextsubstring(0, inputTextlength() - 1));

}

}else if(egetSource() == C){

inputsetText("0");//C,清空所有运算数字和符号

num1 = "";

}else if(egetSource() == CE){

inputsetText("0");//CE-->将文本框置为0

}else if(egetSource() == decimal){

String text = inputgetText()trim();

//如果按了小数点,如果文本框已经有小数点,不做任何 *** 作,否则在结果后面加上小数点

if(textindexOf("") == -1){

inputsetText(textconcat(""));

}

}else if(egetSource() == equal){

//如果是等号

if(!operatortrim()equals("")){

if(!inputgetText()trim()equals("")){

double result = 0D;

if(operatorequals("+")){//执行加法运算

result = DoubleparseDouble(num1) + DoubleparseDouble(inputgetText()trim());

}else if(operatorequals("-")){//减法运算

result = DoubleparseDouble(num1) - DoubleparseDouble(inputgetText()trim());

}else if(operatorequals("")){//乘法运算

result = DoubleparseDouble(num1) DoubleparseDouble(inputgetText()trim());

}else if(operatorequals("/")){//除法运算

result = DoubleparseDouble(num1) / DoubleparseDouble(inputgetText()trim());

}

//格式化最终结果,保留两位小数点

inputsetText(new DecimalFormat("000")format(result));

}

}

}

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

}

import

javautilScanner;

/

用java编写程序计算x的n次幂

/

public

class

Test40003

{

public

static

void

main(String[]

args)

{

int

repeat;//定义要幂次

//int

i,

n;

double

x,

mypow=1;

Scanner

in=new

Scanner(Systemin);//从控制台输入数字,比如

2

15,

//前面是整数,后面是要做幂次运算的数,中间用空格隔开

repeat=innextInt();//获取输入的幂次

x=innextDouble();//获取要进行幂次运算的数

Systemoutprintln("现在要做"+x+"的"+repeat+"次幂运算!");

for(int

ri=1;

ri<=repeat;

ri++){

mypow

=

mypowx;

}

Systemoutprintln(x+"的"+repeat+"次幂运算的结果是:"+mypow);

}

}

以上就是关于java加减乘除运算的程序怎么编啊全部的内容,包括:java加减乘除运算的程序怎么编啊、求一个用JAVA编写的计算器程序(1)实现简单加、减、乘、除的运算。 (、用java语言,编写一个计算器等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9826198.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存