欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java計(jì)算器簡(jiǎn)易代碼,java做計(jì)算器代碼

編寫(xiě)java程序簡(jiǎn)單計(jì)算器

主要涉及的知識(shí)點(diǎn): 類(lèi)的寫(xiě)法, 以及方法的調(diào)用 .建議多做練習(xí). 如果有看不懂的地方. 可以繼續(xù)追問(wèn),一起討論.

創(chuàng)新互聯(lián)建站2013年開(kāi)創(chuàng)至今,先為興寧等服務(wù)建站,興寧等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為興寧企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

參考代碼如下

//Number類(lèi)

class?Number?{

private?int?n1;//私有的整型數(shù)據(jù)成員n1

private?int?n2;//私有的整型數(shù)據(jù)成員n2

//?通過(guò)構(gòu)造函數(shù)給n1和n2賦值

public?Number(int?n1,?int?n2)?{

this.n1?=?n1;

this.n2?=?n2;

}

//?加法

public?int?addition()?{

return?n1?+?n2;

}

//?減法

public?int?subtration()?{

return?n1?-?n2;

}

//?乘法

public?int?multiplication()?{

return?n1?*?n2;

}

//?除法?(可能除不盡,所以使用double作為返回類(lèi)型)

public?double?division()?{

return?n1?*?1.0?/?n2;?//?通過(guò)n1*1.0?把計(jì)算結(jié)果轉(zhuǎn)換成double類(lèi)型.

}

}

//Exam4?類(lèi)

public?class?Exam4{

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

Number?number=new?Number(15,?6);//創(chuàng)建Number類(lèi)的對(duì)象

//下面的是調(diào)用方法得到返回值進(jìn)行輸出顯示

System.out.println("加法"+number.addition());

System.out.println("減法"+number.subtration());

System.out.println("乘法"+number.multiplication());

System.out.println("除法"+number.division());

}

}

求大神發(fā)個(gè)完整簡(jiǎn)單的java計(jì)算器代碼,

public static void main(String[] args) {

System.out.println("簡(jiǎn)單計(jì)算器");

boolean flag=true;//while循環(huán)是否繼續(xù),true繼續(xù)循環(huán),false停止循環(huán)

System.out.println("請(qǐng)輸入第一個(gè)數(shù)字");

while(flag){

Scanner scan = new Scanner(System.in);

String bh=scan.next();

try {

double num = Double.parseDouble(bh);

System.out.println("請(qǐng)輸入符號(hào)(+、-、*、/)");

while(1==1){

String fh=scan.next();

if("+".equals(fh) || "-".equals(fh) || "*".equals(fh) || "/".equals(fh)){

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

while(1==1){

String bh2=scan.next();

try {

double num2 = Double.parseDouble(bh2);

double num3=0;

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

num3=num+num2;

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

num3=num-num2;

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

num3=num*num2;

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

num3=num/num2;

}

System.out.println(num3);

break;

} catch (Exception e) {

System.out.println("請(qǐng)輸入第二個(gè)正確的數(shù)字");

continue;

}

}

break;

}else{

System.out.println("請(qǐng)輸入正確的符號(hào)(+、-、*、/)");

continue;

}

}

System.out.println("是否繼續(xù)運(yùn)算:輸入Y或者y繼續(xù),輸入其它任意字符退出");

String yn=scan.next();

if("Y".equals(yn) || "y".equals(yn)){

continue;

}else{

System.out.println("運(yùn)算結(jié)束");

break;

}

}catch (NumberFormatException e) {//輸入非數(shù)字類(lèi)型時(shí)

System.out.println("請(qǐng)輸入第一個(gè)正確的數(shù)字");

continue;

}

}

}

急求java簡(jiǎn)易計(jì)算器代碼

試試下面的代碼 絕對(duì)沒(méi)有錯(cuò)誤。

package main;

import java.awt.Button;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.Panel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.math.BigDecimal;

import javax.swing.JFrame;

import javax.swing.JTextField;

public class app7 extends JFrame implements ActionListener {

static Panel pan = new Panel();

static JTextField textField = new JTextField("0");

static Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bp, ba, bs, bm, bd,

be, bc, bt, bf, bh;

private StringBuffer temp = new StringBuffer("");

private String optValue = "0";

private String optType="";

private boolean isChoiseOptType=true;

public void init() {

b0 = new Button("0");

b0.addActionListener(this);

b1 = new Button("1");

b1.addActionListener(this);

b2 = new Button("2");

b2.addActionListener(this);

b3 = new Button("3");

b3.addActionListener(this);

b4 = new Button("4");

b4.addActionListener(this);

b5 = new Button("5");

b5.addActionListener(this);

b6 = new Button("6");

b6.addActionListener(this);

b7 = new Button("7");

b7.addActionListener(this);

b8 = new Button("8");

b8.addActionListener(this);

b9 = new Button("9");

b9.addActionListener(this);

bp = new Button(".");

bp.addActionListener(this);

ba = new Button("+");

ba.addActionListener(this);

bs = new Button("-");

bs.addActionListener(this);

bm = new Button("*");

bm.addActionListener(this);

bd = new Button("/");

bd.addActionListener(this);

be = new Button("=");

be.addActionListener(this);

bc = new Button("c");

bc.addActionListener(this);

bt = new Button("退格");

bt.addActionListener(this);

bf = new Button("1/x");

bf.addActionListener(this);

bh = new Button("+/-");

bh.addActionListener(this);

this.setTitle("計(jì)算機(jī)");

this.setLayout(null);

this.setSize(260, 300);

this.setResizable(false);

GridLayout grid = new GridLayout(4, 5);

pan.setLayout(grid);

pan.setBounds(20, 60, 150, 120);

textField.setBounds(20, 35, 150, 20);

textField.setBackground(Color.cyan);

textField.setHorizontalAlignment(textField.RIGHT);

textField.setEditable(false);

pan.add(b1);

pan.add(b2);

pan.add(b3);

pan.add(ba);

pan.add(bc);

pan.add(b4);

pan.add(b5);

pan.add(b6);

pan.add(bs);

pan.add(bt);

pan.add(b7);

pan.add(b8);

pan.add(b9);

pan.add(bm);

pan.add(bf);

pan.add(b0);

pan.add(bh);

pan.add(bp);

pan.add(bd);

pan.add(be);

this.add(textField);

this.add(pan);

}

public static void main(String[] args) {

app7 frm = new app7();

frm.init();

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frm.setVisible(true);

}

@SuppressWarnings("static-access")

@Override

public void actionPerformed(ActionEvent e) {

String value="0";

if (e.getSource().equals(b0)) {

this.temp.append(b0.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b1)) {

this.temp.append(b1.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b2)) {

this.temp.append(b2.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b3)) {

this.temp.append(b3.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b4)) {

this.temp.append(b4.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b5)) {

this.temp.append(b5.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b6)) {

this.temp.append(b6.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b7)) {

this.temp.append(b7.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b8)) {

this.temp.append(b8.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(b9)) {

this.temp.append(b9.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(bp)) {

if(this.temp.length()=0)

this.temp.append("0");

this.temp.append(bp.getLabel());

this.textField.setText(this.temp.toString());

isChoiseOptType=false;

} else if (e.getSource().equals(ba)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

value=value.substring(0,value.length()-1);

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=ba.getLabel();

isChoiseOptType=true;

} else if (e.getSource().equals(bs)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

value=value.substring(0,value.length()-1);

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=bs.getLabel();

isChoiseOptType=true;

} else if (e.getSource().equals(bm)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

value=value.substring(0,value.length()-1);

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=bm.getLabel();

isChoiseOptType=true;

} else if (e.getSource().equals(bd)) {

if(!isChoiseOptType){

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

value=value.substring(0,value.length()-1);

}

this.optValue=value;

this.temp=new StringBuffer("");

}

this.optType=bd.getLabel();

isChoiseOptType=true;

}else if (e.getSource().equals(be)) {

if(!this.optType.equals("")){

BigDecimal opt1=new BigDecimal(this.optValue);

value=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

value=value.substring(0,value.length()-1);

}

BigDecimal opt2=new BigDecimal(value);

BigDecimal result=new BigDecimal(0);

if(this.optType.equals("+")){

result=opt1.add(opt2);

}else if(this.optType.equals("-")){

result=opt1.subtract(opt2);

}else if(this.optType.equals("*")){

result=opt1.multiply(opt2);

}else if(this.optType.equals("/")){

result=opt1.divide(opt2);

}else if(this.optType.equals("%")){

result=opt1.remainder(opt2);

}

this.textField.setText(result.toString());

this.temp=new StringBuffer("");

isChoiseOptType=false;

this.optValue="0";

}

} else if (e.getSource().equals(bc)) {

this.temp=new StringBuffer();

this.textField.setText("0");

} else if (e.getSource().equals(bt)) {

value=this.textField.getText();

value=value.substring(0,value.length()-1);

if(value.indexOf("-")=0 value.length()=1){

value="0";

this.temp=new StringBuffer("");

}else{

this.temp=new StringBuffer(value);

}

this.textField.setText(value);

}else if (e.getSource().equals(bh)) {

value=this.textField.getText();

if(value.indexOf("-")==0){

value=value.substring(1,value.length());

}else{

value="-"+value;

}

this.temp=new StringBuffer(value);

this.textField.setText(value);

} else if (e.getSource().equals(bf)) {

this.optValue=this.textField.getText();

if(value.lastIndexOf(".")==value.length()-1){

this.optValue=this.optValue.substring(0,this.optValue.length()-1);

}

Integer opt1=new Integer(this.optValue);

if(!opt1.toString().equals("0")){

this.textField.setText(1.0/opt1.intValue()+"");

System.out.println(1/opt1.intValue()+"");

}else{

this.textField.setText("0");

}

this.temp=new StringBuffer("");

this.optType="";

this.optValue="0";

}

}

}

記住類(lèi)名是app7.java 包名是main. 如果有不對(duì)的地方 到時(shí)候在找我 我在線(xiàn)。

分享文章:java計(jì)算器簡(jiǎn)易代碼,java做計(jì)算器代碼
當(dāng)前網(wǎng)址:http://www.chinadenli.net/article6/hedsig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司面包屑導(dǎo)航外貿(mào)網(wǎng)站建設(shè)App設(shè)計(jì)電子商務(wù)網(wǎng)站維護(hù)

廣告

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

成都網(wǎng)站建設(shè)