我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java实现四则运算代码 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.*;

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("");

}

}

java实现四则运算

最后一个提示没看懂意思。import java.util.Random;

public class JiS {

public static void main(String[] args)

{

Random r=new Random();

char[]ch=new char[]{'+','-','*','/'};

boolean flag=true;

while(flag){

int a=r.nextInt(10001);

int b=r.nextInt(10001);

char c=ch[r.nextInt(ch.length)];

// System.out.println(a+","+b+","+c);

switch(c)

{

case '+':

if(a+b=10000){System.out.println(a+"+"+b+"="+(a+b));flag=false;}

break;

case '-':

if(a-b=0){System.out.println(a+"-"+b+"="+(a-b));flag=false;}

break;

case '*':

if(a*b=10000){System.out.println(a+"*"+b+"="+a*b);flag=false;}

break;

case '/':

if(b!=0){System.out.println(a+"/"+b+"="+a/b);flag=false;}

break;

}

}

}

}

定义一个方法,实现四则运算的功能。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实现四则运算代码 java简单四则运算代码
URL网址:http://mswzjz.cn/article/dogeddp.html

其他资讯