Java數(shù)組四則運(yùn)算代碼 java數(shù)組四要素

如何用Java編寫四則運(yùn)算程序?

(首先建個(gè)類,把這些復(fù)制粘貼進(jìn)去)

超過(guò)10年行業(yè)經(jīng)驗(yàn),技術(shù)領(lǐng)先,服務(wù)至上的經(jīng)營(yíng)模式,全靠網(wǎng)絡(luò)和口碑獲得客戶,為自己降低成本,也就是為客戶降低成本。到目前業(yè)務(wù)范圍包括了:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作,成都網(wǎng)站推廣,成都網(wǎng)站優(yōu)化,整體網(wǎng)絡(luò)托管,微信小程序定制開(kāi)發(fā),微信開(kāi)發(fā),重慶APP軟件開(kāi)發(fā),同時(shí)也可以讓客戶的網(wǎng)站和網(wǎng)絡(luò)營(yíng)銷和我們一樣獲得訂單和生意!

import java.awt.*;

import javax.swing.*;

public class F {

JFrame frame = new JFrame("計(jì)算機(jī)");

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

}

}

(再新建個(gè)類 把這些也復(fù)制粘貼進(jìn)去)

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

}

}

}

(再新建一個(gè),把下面的復(fù)制粘貼)

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 = "";

}

}

(在建一個(gè) 復(fù)制粘貼)

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;

}

}

(在建一個(gè))

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簡(jiǎn)單的四則運(yùn)算.

public class Arithmetic {

public static void Ari(){

Scanner scan = new Scanner(System.in);

StringBuffer buffer = new StringBuffer();

DecimalFormat dec = new DecimalFormat("0.00");//格式化結(jié)果保留兩位小數(shù)

String all = "";//所有的計(jì)算表達(dá)式連在一起

System.out.println("請(qǐng)輸入表達(dá)式的個(gè)數(shù),只能為正整數(shù)");

int n = scan.nextInt();

System.out.println("請(qǐng)依次輸入要計(jì)算的表達(dá)式");

? ?for(int i=0;in+1;i++){

? ? buffer = buffer.append(scan.nextLine()+",");

? ?}

? ?all = buffer.substring(0, buffer.lastIndexOf(","));

? ?String allAri[] = all.split(",");

? ?String ari = "";//不同的算法表達(dá)式

? ?float add;//加法的計(jì)算結(jié)果

? ?float subtract;//減肥的計(jì)算結(jié)果

? ?float multi;//乘法的計(jì)算結(jié)果

? ?float divison;//除法的計(jì)算結(jié)果

? ?int model;//模運(yùn)算的計(jì)算結(jié)果

? ?for(int j=0;jallAri.length;j++){

? ? ari = allAri[j];

? ? if(ari.contains("+")){

? ? String tempAry[] = ari.split("[+]");

? ? add = Float.valueOf(tempAry[0])+Float.valueOf(tempAry[1]);

? ? System.out.println(dec.format(add));

? ? }else if(ari.contains("-")){

? ? String tempAry[] = ari.split("[-]");

? ? subtract = Float.valueOf(tempAry[0])-Float.valueOf(tempAry[1]);

? ? System.out.println(dec.format(subtract));

? ? }else if(ari.contains("*")){

? ? String tempAry[] = ari.split("[*]");

? ? multi = Float.valueOf(tempAry[0])*Float.valueOf(tempAry[1]);

? ? System.out.println(dec.format(multi));

? ? }else if(ari.contains("/")){

? ? String tempAry[] = ari.split("[/]");

? ? divison = Float.valueOf(tempAry[0])/Float.valueOf(tempAry[1]);

? ? System.out.println(dec.format(divison));

? ? }else if(ari.contains("%")){

? ? String tempAry[] = ari.split("[%]");

? ? model = Integer.valueOf(tempAry[0])%Integer.valueOf(tempAry[1]);

? ? System.out.println(model);

? ? }

? ?}

}

public static void main(String[] args) {

Ari();

}

}

測(cè)試結(jié)果截圖如下:

你的測(cè)試用例的輸入的表達(dá)式的個(gè)數(shù)是4個(gè),但下面的計(jì)算表達(dá)式好像少了一個(gè),所以我加了一個(gè)除法的計(jì)算表達(dá)式,若理解有誤,還望說(shuō)明。

編寫一個(gè)實(shí)現(xiàn)四則運(yùn)算的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("請(qǐng)輸入第一個(gè)數(shù):");

double x = scanner.nextDouble();

System.out.print("請(qǐng)輸入第二個(gè)數(shù):");

double y = scanner.nextDouble();

System.out.print("請(qǐng)輸入運(yùn)算符:");

String s = scanner.next();

char z = s.charAt(0);

zhidao.yunsuan(x, y, z);

}catch(Exception e){

System.out.println("請(qǐng)輸入正確的數(shù)據(jù)!");

}

System.out.print("是否繼續(xù)?continue:繼續(xù),任意字符:結(jié)束");

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("被除數(shù)不能為0");

} else{

System.out.println(x+"/"+y+"=" + r.format((x/y)));

}

}else{

System.out.println("無(wú)法識(shí)別改運(yùn)算符");

}

}

}

文章題目:Java數(shù)組四則運(yùn)算代碼 java數(shù)組四要素
轉(zhuǎn)載注明:http://www.muchs.cn/article28/doeegjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃軟件開(kāi)發(fā)、網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站手機(jī)網(wǎng)站建設(shè)網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)