真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java共詞計(jì)算代碼 java計(jì)算公式字符串

JAVA 編寫計(jì)算器 要代碼最簡(jiǎn)單的

學(xué)java的時(shí)候自己編的,很簡(jiǎn)單,能夠連續(xù)輸入計(jì)算式后進(jìn)行計(jì)算

創(chuàng)新互聯(lián)建站是專業(yè)的金華網(wǎng)站建設(shè)公司,金華接單;提供成都做網(wǎng)站、網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行金華網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!

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;

/**簡(jiǎn)易計(jì)算器,能夠進(jìn)行簡(jiǎn)單的計(jì)算

*

* @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="";//保存結(jié)果

StringBuilder sb = new StringBuilder();//保存要進(jìn)行的計(jì)算式

int x = 0; //用來(lái)判斷上一次的事件類型

String str = "123+456-789*0.=/";

ArrayListString arrayList = new ArrayListString();//保存計(jì)算式,通過(guò)方法進(jìn)行運(yùn)算

public CalculatorA(){

frame = new JFrame("我的計(jì)算器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());

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

}

//設(shè)置輸入字體

public void setFondAndColor(){

field.setFont(new Font("宋體",Font.BOLD,24));

field.setBackground(new Color(100,200,200));

field.setForeground(Color.RED);

//設(shè)置字體從右起始

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();//取得當(dāng)前事件返回的值

if("0123456789.".indexOf(str)!=-1){

if(x == 0){ //當(dāng)x為0時(shí)表示還沒(méi)有進(jìn)行輸入

result=str;

sb.append(str);

field.setText(str);

x = 1;

}

else if(x == 1){

result = result +str;

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 = result +str;

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 = result + str;

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("數(shù)據(jù)格式異常");

x = 0;

}

}

else if(x == 2){

field.setText("數(shù)據(jù)格式錯(cuò)誤.....");

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("數(shù)據(jù)格式異常");

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

//本地的數(shù)據(jù)格式

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;

}

//對(duì)數(shù)字字符串進(jìn)行排除不必要符號(hào)

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 計(jì)算器代碼

import javax.swing.*;

import javax.swing.border.Border;

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.math.BigDecimal;

import java.math.RoundingMode;

import java.util.HashMap;

/**

* 我的計(jì)算器。Cheshi 繼承于 JFrame,是計(jì)算器的界面

c*/

public class Cheshi extends JFrame {

private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);

private JTextField textbox = new JTextField("0");

private CalculatorCore core = new CalculatorCore();

private ActionListener listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

JButton b = (JButton) e.getSource();

String label = b.getText();

String result = core.process(label);

textbox.setText(result);

}

};

public Cheshi(String title) throws HeadlessException {

super(title); // 調(diào)用父類構(gòu)造方法

setupFrame(); // 調(diào)整窗體屬性

setupControls(); // 創(chuàng)建控件

}

private void setupControls() {

setupDisplayPanel(); // 創(chuàng)建文本面板

setupButtonsPanel(); // 創(chuàng)建按鈕面板

}

// 創(chuàng)建按鈕面板并添加按鈕

private void setupButtonsPanel() {

JPanel panel = new JPanel();

panel.setBorder(border);

panel.setLayout(new GridLayout(4, 5, 3, 3));

createButtons(panel, new String[]{

"7", "8", "9", "+", "C",

"4", "5", "6", "-", "CE",

"1", "2", "3", "*", "", // 空字符串表示這個(gè)位置沒(méi)有按鈕

"0", ".", "=", "/", ""

});

this.add(panel, BorderLayout.CENTER);

}

/**

* 在指定的面板上創(chuàng)建按鈕

*

* @param panel 要?jiǎng)?chuàng)建按鈕的面板

* @param labels 按鈕文字

*/

private void createButtons(JPanel panel, String[] labels) {

for (String label : labels) {

// 如果 label 為空,則表示創(chuàng)建一個(gè)空面板。否則創(chuàng)建一個(gè)按鈕。

if (label.equals("")) {

panel.add(new JPanel());

} else {

JButton b = new JButton(label);

b.addActionListener(listener); // 為按鈕添加偵聽(tīng)器

panel.add(b);

}

}

}

// 設(shè)置顯示面板,用一個(gè)文本框來(lái)作為計(jì)算器的顯示部分。

private void setupDisplayPanel() {

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.setBorder(border);

setupTextbox();

panel.add(textbox, BorderLayout.CENTER);

this.add(panel, BorderLayout.NORTH);

}

// 調(diào)整文本框

private void setupTextbox() {

textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右對(duì)齊

textbox.setEditable(false); // 文本框只讀

textbox.setBackground(Color.white); // 文本框背景色為白色

}

// 調(diào)整窗體

private void setupFrame() {

this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 當(dāng)窗體關(guān)閉時(shí)程序結(jié)束

this.setLocation(100, 50); // 設(shè)置窗體顯示在桌面上的位置

this.setSize(300, 200); // 設(shè)置窗體大小

this.setResizable(false); // 窗體大小固定

}

// 程序入口

public static void main(String[] args) throws Exception {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Cheshi frame = new Cheshi("我的計(jì)算器");

frame.setVisible(true); // 在桌面上顯示窗體

}

}

/**

* 計(jì)算器核心邏輯。這個(gè)邏輯只能處理 1~2 個(gè)數(shù)的運(yùn)算。

*/

class CalculatorCore {

private String displayText = "0"; // 要顯示的文本

boolean reset = true;

private BigDecimal number1, number2;

private String operator;

private HashMapString, Operator operators = new HashMapString, Operator();

private HashMapString, Processor processors = new HashMapString, Processor();

CalculatorCore() {

setupOperators();

setupProcessors();

}

// 為每種命令添加處理方式

private void setupProcessors() {

processors.put("[0-9]", new Processor() {

public void calculate(String command) {

numberClicked(command);

}

});

processors.put("\\.", new Processor() {

public void calculate(String command) {

dotClicked();

}

});

processors.put("=", new Processor() {

public void calculate(String command) {

equalsClicked();

}

});

processors.put("[+\\-*/]", new Processor() {

public void calculate(String command) {

operatorClicked(command);

}

});

processors.put("C", new Processor() {

public void calculate(String command) {

clearClicked();

}

});

processors.put("CE", new Processor() {

public void calculate(String command) {

clearErrorClicked();

}

});

}

// 為每種 operator 添加處理方式

private void setupOperators() {

operators.put("+", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.add(number2);

}

});

operators.put("-", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.subtract(number2);

}

});

operators.put("*", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.multiply(number2);

}

});

operators.put("/", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.divide(number2, 30, RoundingMode.HALF_UP);

}

});

}

// 根據(jù)命令處理。這里的命令實(shí)際上就是按鈕文本。

public String process(String command) {

for (String pattern : processors.keySet()) {

if (command.matches(pattern)) {

processors.get(pattern).calculate(command);

break;

}

}

return displayText;

}

// 當(dāng)按下 CE 時(shí)

private void clearErrorClicked() {

if (operator == null) {

number1 = null;

} else {

number2 = null;

}

displayText = "0";

reset = true;

}

// 當(dāng)按下 C 時(shí),將計(jì)算器置為初始狀態(tài)。

private void clearClicked() {

number1 = null;

number2 = null;

operator = null;

displayText = "0";

reset = true;

}

// 當(dāng)按下 = 時(shí)

private void equalsClicked() {

calculateResult();

number1 = null;

number2 = null;

operator = null;

reset = true;

}

// 計(jì)算結(jié)果

private void calculateResult() {

number2 = new BigDecimal(displayText);

Operator oper = operators.get(operator);

if (oper != null) {

BigDecimal result = oper.process(number1, number2);

displayText = result.toString();

}

}

// 當(dāng)按下 +-*/ 時(shí)(這里也可以擴(kuò)展成其他中間操作符)

private void operatorClicked(String command) {

if (operator != null) {

calculateResult();

}

number1 = new BigDecimal(displayText);

operator = command;

reset = true;

}

// 當(dāng)按下 . 時(shí)

private void dotClicked() {

if (displayText.indexOf(".") == -1) {

displayText += ".";

} else if (reset) {

displayText = "0.";

}

reset = false;

}

// 當(dāng)按下 0-9 時(shí)

private void numberClicked(String command) {

if (reset) {

displayText = command;

} else {

displayText += command;

}

reset = false;

}

// 運(yùn)算符處理接口

interface Operator {

BigDecimal process(BigDecimal number1, BigDecimal number2);

}

// 按鈕處理接口

interface Processor {

void calculate(String command);

}

}

java代碼題 給出一段英文句子,讓你把句子里的每個(gè)單詞(無(wú)縮寫形式) 出現(xiàn)次數(shù)計(jì)算出來(lái)!

等我,就寫出來(lái)

import java.io.*;

import java.util.HashMap;

import java.util.Map.Entry;

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

try {

BufferedReader br = new BufferedReader(new FileReader("C:\\text.txt"));//我就不寫有界面的了,這個(gè)參數(shù)就是你英文句子文件所在位置

String sentence = null;

HashMapString,Integer map = new HashMapString, Integer();

while((sentence = br.readLine())!=null){

sentence = sentence.replaceAll("[\\pP'$']", "");

String[] words = sentence.split(" ");

for(String word:words){

if(map.get(word)==null)

map.put(word, 1);

else map.put(word, map.get(word)+1);

}

}

java.util.IteratorEntryString, Integer iter = map.entrySet().iterator();

while(iter.hasNext()){

Entry entry = (Entry) iter.next();

System.out.println(entry.getKey()+" "+entry.getValue());

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

JAVA計(jì)算器相關(guān)代碼求大神{詳解}

我給你找找

package com.bj.calcultor;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Calcultor extends Frame implements ActionListener {

public static void main(String[] args) {//定義主方

new Calcultor();//創(chuàng)建匿名對(duì)象,并調(diào)用test()方法;

}

//定義按鈕名稱

String[] arr={"1","2","3","4","5","6","7","8","9","0","+","-","*","/","=","."};

JButton [] button=new JButton[arr.length];

JButton reset = new JButton("CE");

JTextField display = new JTextField(20);

//創(chuàng)建窗口,定義組件

//執(zhí)行窗口事件:關(guān)閉窗口

private class WindowCloser extends WindowAdapter {

public void windowClosing(WindowEvent we) {

System.exit(0);

}

}

public Calcultor(){

super("計(jì)算器");//定義標(biāo)題

//定義面板容器,并布局

JPanel jpanel=new JPanel(new GridLayout(4,4));

//添加按鈕,并給按鈕添加名稱

for (int i = 0; i arr.length; i++) {

button[i]= new JButton(arr[i]);

jpanel.add(button[i]);

}

JPanel panel2 = new JPanel();

panel2.add("Northr", display);

display.setEnabled(false);

panel2.add("East", reset);

this.add("North", panel2);

this.add("Center", jpanel);

for (int i = 0; i arr.length; i++){

addWindowListener(new WindowCloser());

setVisible(true);

setSize(400,400);

pack();

button[i].addActionListener(this);

reset.addActionListener(this);

display.addActionListener(this);

}

}

@Override

public void actionPerformed(ActionEvent e) {//定義事件

// TODO Auto-generated method stub

Object target=e.getSource();

String lable=e.getActionCommand();

if(target==reset){

handleReset();

}else if("0123456789.".indexOf(lable)0){

handleNumber(lable);

}else{

hadleOperator(lable);

}

}

boolean isFirstDigit=true;

private void hadleOperator(String key) {

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

number += Double.valueOf(display.getText());

}else if (operator.equals("-")){

number -= Double.valueOf(display.getText());

}else if (operator.equals("*")){

number *= Double.valueOf(display.getText());

}else if (operator.equals("/")){

number /= Double.valueOf(display.getText());

}else if(operator.equals("=")){

number =Double.valueOf(display.getText());

}

display.setText(String.valueOf(number));

operator=key;

isFirstDigit=true;

}

private void handleNumber(String key) {

if(true){

display.setText(key);//把鍵值設(shè)置為文本框內(nèi)容

}else if(key.equals(".") display.getText().indexOf(".")0){

display.setText(display.getText()+".");//把文本框內(nèi)容設(shè)置:display.getText()+"."

}else if(!key.equals(".")){

display.setText(display.getText() + key);//把文本框內(nèi)容設(shè)置:display.getText()+key

isFirstDigit=false;

}

}

private void handleReset() {

display.setText("0");

isFirstDigit=true;

operator="=";

}

String operator="=";

Double number=0.0;

}


網(wǎng)站欄目:java共詞計(jì)算代碼 java計(jì)算公式字符串
轉(zhuǎn)載注明:http://weahome.cn/article/doiihoe.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部