老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業提供網站建設、域名注冊、服務器等服務

java四則運算代碼 java四則運算代碼講解

java簡單的四則運算.

public class Arithmetic {

成都創新互聯長期為上1000家客戶提供的網站建設服務,團隊從業經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯網生態環境。為新蔡企業提供專業的做網站、網站設計,新蔡網站改版等技術服務。擁有10余年豐富建站經驗和眾多成功案例,為您定制開發。

public static void Ari(){

Scanner scan = new Scanner(System.in);

StringBuffer buffer = new StringBuffer();

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

String all = "";//所有的計算表達式連在一起

System.out.println("請輸入表達式的個數,只能為正整數");

int n = scan.nextInt();

System.out.println("請依次輸入要計算的表達式");

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

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

? ?}

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

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

? ?String ari = "";//不同的算法表達式

? ?float add;//加法的計算結果

? ?float subtract;//減肥的計算結果

? ?float multi;//乘法的計算結果

? ?float divison;//除法的計算結果

? ?int model;//模運算的計算結果

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

}

}

測試結果截圖如下:

你的測試用例的輸入的表達式的個數是4個,但下面的計算表達式好像少了一個,所以我加了一個除法的計算表達式,若理解有誤,還望說明。

如何用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.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class calculator extends JFrame implements ActionListener {

private boolean i=true,clickable = true ;//這里的clickabel 是用來判斷小數點的 如果有小數點的話 clickable則為false就不對小數點進行添加了 i在下面有解釋

private double value=0; //這個就是顯示出來的值了

String operate = "="; //操作數 默認情況下是=號

JFrame jframe=new JFrame("計算機");

JTextField text1=new JTextField("0");

JButton back = new JButton("退格");

JButton daoshu = new JButton("1/x");

JButton remain = new JButton("%");

JButton PI = new JButton("PI"); //顯示圓周率小數點后7位

JButton square = new JButton("x^2");

JButton cube = new JButton("x^3");

JButton plu=new JButton("+");

JButton min=new JButton("-");

JButton mul=new JButton("*");

JButton equ=new JButton("=");

JButton div=new JButton("/");

JButton code=new JButton("+/-");

JButton point=new JButton(".");

JButton num1=new JButton("1");

JButton num2=new JButton("2");

JButton num3=new JButton("3");

JButton num4=new JButton("4");

JButton num5=new JButton("5");

JButton num6=new JButton("6");

JButton num7=new JButton("7");

JButton num8=new JButton("8");

JButton num9=new JButton("9");

JButton num0=new JButton("0");

JButton num00=new JButton("00");

JButton clear=new JButton("C");

calculator(){

jframe.setSize(400, 400);

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel p=new JPanel();

p.setBackground(Color.gray);

p.setLayout(new GridLayout(5, 5)); //采用的布局方式為網格布局

PI.addActionListener(this);

daoshu.addActionListener(this);

remain.addActionListener(this);

square.addActionListener(this);

cube.addActionListener(this);

plu.addActionListener(this);

equ.addActionListener(this);

num1.addActionListener(this);

num2.addActionListener(this);

clear.addActionListener(this);

num3.addActionListener(this);

num4.addActionListener(this);

num5.addActionListener(this);

num6.addActionListener(this);

num7.addActionListener(this);

num8.addActionListener(this);

num9.addActionListener(this);

num0.addActionListener(this);

num00.addActionListener(this);

min.addActionListener(this);

code.addActionListener(this);

div.addActionListener(this);

point.addActionListener(this);

mul.addActionListener(this);

back.addActionListener(this);

jframe.add(p);

p.add(PI);

p.add(code);

p.add(daoshu);

p.add(back);

p.add(clear);

p.add(num7);

p.add(num8);

p.add(num9);

p.add(plu);

p.add(remain);

p.add(num4);

p.add(num5);

p.add(num6);

p.add(min);

p.add(square);

p.add(num1);

p.add(num2);

p.add(num3);

p.add(mul);

p.add(cube);

p.add(num0);

p.add(num00);

p.add(point);

p.add(div);

p.add(equ);

jframe.add(text1,"North");

jframe.setLocation(300, 300);

jframe.setVisible(true);

}

public void actionPerformed(ActionEvent evt)

{

String str1,str2;

Object soc=evt.getSource();

String s = evt.getActionCommand(); //獲取按鈕的數據

if(soc==back){ //進行退格操作

str1=text1.getText();

int n=str1.length();

text1.setText(str1.substring(0,n-1));

str2 = text1.getText();

}

else if (s.equals("x^2")) {

str1=text1.getText();

double k = Double.parseDouble(str1);

k *=k;

text1.setText(String.valueOf(k));

}

else if (s.equals("x^3")) {

str1=text1.getText();

double k = Double.parseDouble(str1);

k =k*k*k;

text1.setText(String.valueOf(k));

}

else if(s.equals("PI")){ //顯示圓周率

double Pi=3.1415926;

String pi=String.valueOf(Pi);

text1.setText(pi);

}

else if(s.equals("+/-")){ //變換正負號

double a = 0;

str1=text1.getText();

a=Double.parseDouble(str1.trim());

a=-1*a;

str1=String.valueOf(a);

text1.setText(str1);

}

else if(s.equals("1/x")){ //求倒

str1=text1.getText();

double i = 1/Double.parseDouble(str1.trim());

String k = String.valueOf(i);

text1.setText(k);

}

else if(s.equals(".")){ //小數點

clickable=true;

for (int i = 0; i text1.getText().length(); i++)

if ('.' == text1.getText().charAt(i))

{

clickable=false;

break;

} //第一層判斷是否里面含有小數點;

if(clickable==true) //第二層判斷

text1.setText(text1.getText()+".");

i=false;

}

else if(s.equals("C")){ //清零 所有的數據變回初始值

value = 0;

text1.setText("0");

i=true;

operate = "=" ;

}

else if ('0' = s.charAt(0) s.charAt(0) = '9' ) { //若輸入的為數字以及小數點的時候執行下面

if (i) { //判斷獲得的command是否為操作符

text1.setText(s);

}

else {

text1.setText(text1.getText() + s);

}

i = false;

}

else { //若輸入運算操作符的話則執行下面的代碼

count(Double.parseDouble(text1.getText().trim())); //這里一開始調用 count()方法的話首先執行的是operate.equals("=")

//執行上個操作符的運算

operate = s;

i = true;

}

}

private void count(double a) { //計算區域

if (operate.equals("+")) {

value += a;

}

else if (operate.equals("-")) {

value -= a;

}

else if (operate.equals("*")) {

value *= a;

}

else if (operate.equals("/")) {

value /= a;

}

else if (operate.equals("%")) {

value %= a;

}

else if (operate.equals("=")) {

value = a;

}

text1.setText(Double.toString(value));

}

public static void main(String[] arg)

{

calculator cal=new calculator();

}

}

這是例子 你可以參考下~監聽的 部分

怎么用java編程,實現分數的加減乘除運算?

java編程實現分數的加減乘除運算的步驟如下:

1、打開eclipse,創建一個Java工程,在此工程里新建一個類;

2、在新建的類中,添加4個運算類;

3、在主方法中調用對應的方法即可完成分數的加減乘除運算了。

具體實現代碼如下:

public?class?Demo?{

public?static?void?main(String[]?args)?{

System.out.println(jia(1,?2));

System.out.println(jian(1,?2));

System.out.println(cheng(1,?2));

System.out.println(chu(1,?2));

}

//加法運算

private?static?float?jia(float?x,float?y)?{

return?x?+?y;

}

//減法運算

private?static?float?jian(float?x,float?y)?{

return?x?-?y;

}

//乘法運算

private?static?float?cheng(float?x,float?y)?{

return?x?*?y;

}

//除法運算

private?static?float?chu(float?x,float?y)?{

return?x?/?y;

}

}

編寫一個實現四則運算的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四則運算代碼講解
地址分享:http://www.xueling.net.cn/article/doosgec.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 日韩a在线看免费观看视频 国产做a爱一级毛片久 | 久久人人爽人人爽人人AV东京热 | 护士被两个病人伦奷日出白浆 | 欧美videosfree性派对 | 91porny九色91啦中文 | 欧美精品一区二区三区中文欧美人 | 农村妇女高清毛片一级 | 日本裸体猜人综艺节目在线 | 成人免费av在线播放 | 亚洲AV影院一区二区三区 | 日韩精品一区二区三区老鸭窝 | 国产一级大片 | 6699嫩草久久久精品影院 | 强乱中文字幕 | 美国一级在线 | 精品国产一区二区精华 | 中文av一区二区三区 | 欧美在线一级片 | 亚洲国产精品人人做人人爱 | 全职猎人1999在线动漫免费观看 | 91精品国自产在线观看 | 一区二区三区香蕉视频 | 国产美女在线播放 | 亚洲AV永久无码精品桃花岛知道 | 米奇777第4色狠狠 | 国产精品一二三区 | 视频一区二区免费 | 亚洲人成在久久综合网站 | 成人福利视频网站 | japanese36hdxxxx日韩| 日韩怡红院 | 亚洲hd | 亚洲人成网站在线播放942 | 超碰97国产| 超碰久操| 成人免费av在线播放 | 国产v片成人影院在线观看 免费在线看h | 久久精品国产人伦 | 日日操夜夜添 | 特黄一级免费视频 | 日本免费www|