c窗体应用程序加法运算?

黄玉瑛 2019-12-21 20:38:00

推荐回答

新鲜出炉的,来吧。classProgram{staticvoidMainstringargs{whiletrue{Calcu;Console.WriteLine"是否继续执行循环?Y/N";strings=Console.ReadLine;ifs.ToUpper=="Y"{Calcu;continue;}break;}}staticvoidCalcu{Console.WriteLine"请输入第一个数字:";floatnum1=0;floatnum2=0;stringfuhao="";whiletrue{try{num1=Convert.ToSingleConsole.ReadLine;break;}catchException{num1=0;Console.WriteLine"输入错误,请重新输入第一个数字:";}}Console.WriteLine"请输入第二个数字:";whiletrue{try{num2=Convert.ToSingleConsole.ReadLine;break;}catchException{num2=0;Console.WriteLine"输入错误,请重新输入第二个数字:";}}Console.WriteLine"请输入运算符+-*/:";whiletrue{try{fuhao=Console.ReadLine;iffuhao=="+"||fuhao=="-"||fuhao=="*"||fuhao=="/"break;fuhao="";Console.WriteLine"运算符输入错误,请重新输入运算符+-*/:";}catchException{fuhao="";Console.WriteLine"运算符输入错误,请重新输入运算符+-*/";}}floatnum=0;switchfuhao{case"+":num=num1+num2;break;case"-":num=num1-num2;break;case"*":num=num1*num2;break;case"/":num=num1/num2;break;}Console.WriteLine"运算结果出炉:"+num1+fuhao+num2+"="+num;}。
连亚臣2019-12-21 21:02:18

提示您:回答为网友贡献,仅供参考。

其他回答

  • 创建一个隐藏的Lable,把计算结果保存在Lable上,然后每次计算跟lable运算就可以了。
    龚小雪2019-12-21 20:57:39

相关问答

给你一个参考,希望不要被吞了当晚餐importjava.awt.BorderLayout;importjava.awt.GridLayout;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.text.DecimalFormat;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JTextField;publicclassCalculator{//计算器面板privateJFramef=newJFrame"Calculator";//输入面扳privateJPanelinputPanel=newJPanel;//加减乘除面板privateJPaneloperationPanel=newJPanel;//数字面板privateJPanelbuttonsPanel=newJPanel;//输入数据文本框privateJTextFieldinput=newJTextField20;//退格键privateJButtonbackspace=newJButton"BackSpace";//清空privateJButtonCE=newJButton"CE";//删除privateJButtonC=newJButton"C";//四则运算符号键privateJButtonadd=newJButton"+";privateJButtonsub=newJButton"-";privateJButtonmul=newJButton"*";privateJButtondiv=newJButton"/";//小数点privateJButtondecimal=newJButton".";//等号privateJButtonequal=newJButton"=";//数字键privateJButtonzero=newJButton"0";privateJButtonone=newJButton"1";privateJButtontwo=newJButton"2";privateJButtonthree=newJButton"3";privateJButtonfour=newJButton"4";privateJButtonfive=newJButton"5";privateJButtonsix=newJButton"6";privateJButtonseven=newJButton"7";privateJButtoneight=newJButton"8";privateJButtonnine=newJButton"9";privateStringnum1="";//保存第一个运算数字privateStringoperator="";//保存运算符号publicstaticvoidmainStringargs{newCalculator;//new计算器实例}publicCalculator{//添加组件,布局inputPanel.addinput;f.addinputPanel,BorderLayout.NORTH;operationPanel.addbackspace;operationPanel.addCE;operationPanel.addC;f.addoperationPanel,BorderLayout.CENTER;buttonsPanel.addadd;buttonsPanel.addsub;buttonsPanel.addmul;buttonsPanel.adddiv;buttonsPanel.addone;buttonsPanel.addtwo;buttonsPanel.addthree;buttonsPanel.addzero;buttonsPanel.addfour;buttonsPanel.addfive;buttonsPanel.addsix;buttonsPanel.adddecimal;buttonsPanel.addseven;buttonsPanel.addeight;buttonsPanel.addnine;buttonsPanel.addequal;buttonsPanel.setLayoutnewGridLayout4,4;f.addbuttonsPanel,BorderLayout.SOUTH;//注册各个组件监听事件backspace.addMouseListenernewOperationMouseListener;CE.addMouseListenernewOperationMouseListener;C.addMouseListenernewOperationMouseListener;decimal.addMouseListenernewOperationMouseListener;equal.addMouseListenernewOperationMouseListener;//注册四则运算监听add.addMouseListenernewCalcMouseListener;sub.addMouseListenernewCalcMouseListener;mul.addMouseListenernewCalcMouseListener;div.addMouseListenernewCalcMouseListener;//注册数字监听事件zero.addMouseListenernewNumberMouseListener;one.addMouseListenernewNumberMouseListener;two.addMouseListenernewNumberMouseListener;three.addMouseListenernewNumberMouseListener;four.addMouseListenernewNumberMouseListener;five.addMouseListenernewNumberMouseListener;six.addMouseListenernewNumberMouseListener;seven.addMouseListenernewNumberMouseListener;eight.addMouseListenernewNumberMouseListener;nine.addMouseListenernewNumberMouseListener;f.setVisibletrue;f.pack;f.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE;}privateclassNumberMouseListenerimplementsMouseListener{publicvoidmouseClickedMouseEvente{ifinput.getText.trim.equals"0"{//如果文本框已经是0,结果还是0input.setTextJButtone.getSource.getText.trim;}else{//否则的话,把0添加到后面,譬如文本框是1,结果就为10input.setTextinput.getText.concatJButtone.getSource.getText.trim;}}publicvoidmouseEnteredMouseEvente{}publicvoidmouseExitedMouseEvente{}publicvoidmousePressedMouseEvente{}publicvoidmouseReleasedMouseEvente{}}privateclassCalcMouseListenerimplementsMouseListener{//如果输入的是运算符号,保存第一个结果和运算符号publicvoidmouseClickedMouseEvente{num1=input.getText.trim;input.setText"";operator=JButtone.getSource.getText.trim;}publicvoidmouseEnteredMouseEvente{}publicvoidmouseExitedMouseEvente{}publicvoidmousePressedMouseEvente{}publicvoidmouseReleasedMouseEvente{}}privateclassOperationMouseListenerimplementsMouseListener{publicvoidmouseClickedMouseEvente{ife.getSource==backspace{//退格键,删除一个直到没有字符删除StringinputText=input.getText;ifinputText.length>0{input.setTextinputText.substring0,inputText.length-1;}}elseife.getSource==C{input.setText"0";//C,清空所有运算数字和符号num1="";}elseife.getSource==CE{input.setText"0";//CE-->将文本框置为0}elseife.getSource==decimal{Stringtext=input.getText.trim;//如果按了小数点,如果文本框已经有小数点,不做任何操作,否则在结果后面加上小数点iftext.indexOf"."==-1{input.setTexttext.concat".";}}elseife.getSource==equal{//如果是等号if!operator.trim.equals""{if!input.getText.trim.equals""{doubleresult=0D;ifoperator.equals"+"{//执行加法运算result=Double.parseDoublenum1+Double.parseDoubleinput.getText.trim;}elseifoperator.equals"-"{//减法运算result=Double.parseDoublenum1-Double.parseDoubleinput.getText.trim;}elseifoperator.equals"*"{//乘法运算result=Double.parseDoublenum1*Double.parseDoubleinput.getText.trim;}elseifoperator.equals"/"{//除法运算result=Double.parseDoublenum1/Double.parseDoubleinput.getText.trim;}//格式化最终结果,保留两位小数点input.setTextnewDecimalFormat"0.00".formatresult;}}}}publicvoidmouseEnteredMouseEvente{}publicvoidmouseExitedMouseEvente{}publicvoidmousePressedMouseEvente{}publicvoidmouseReleasedMouseEvente{}}。