C编写一个应用程序,完成从1开始加到n的求和计算。

黄生权 2019-12-21 20:44:00

推荐回答

packagecom.demo;//计算从1加到50求和,输出结果publicclassSum{publicstaticvoidmainStringargs{intsum=0;forinti=1;i<=50;i++{sum+=i;//这句话的意思是sum=sum+i;}System.out.println"1加到50的和是:"+sum;}。
黄盈碧2019-12-21 20:58:04

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

其他回答

  • C语言是普适性最强的一种计算机程序编辑语言,它不仅可以发挥出高级编程语言的功用,还具有汇编语言的优点,计算和的方法为:1、新建一个求1到100自然数之和项目。2、添加一个cpp文件。3、包含需要用到的头文件。4、输入main函数。5、定义两个变量,分别是:a和sum。6、使用while语句计算1到100之间所有自然数之和。7、运行程序,打印出结果。注意事项:C语言的语法限制不太严格,对变量的类型约束不严格,影响程序的安全性,对数组下标越界不作检查等。从应用的角度,C语言比其他高级语言较难掌握。也就是说,对用C语言的人,要求对程序设计更熟练一些。
    齐文胜2019-12-21 21:02:41

相关问答

给你一个参考,希望不要被吞了当晚餐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{}}。