Public Function factorial(ByVal num As Integer) ' 计算给定数字的阶乘
If num <= 1 Then
factorial = 1
Else
factorial = factorial(num - 1) num
End If
End Function
Public Function sum(ByVal num As Integer) ' 计算阶乘的总和
Dim i As Integer
If num <= 1 Then
sum = factorial(num)
Else
For i = 1 To num
sum = sum + factorial(i)
Next i
End If
End Function
Private Sub Form_Load()
MsgBox Str(sum(20)) ' 显示结果
End Sub
public class ss
{
public static void main(String [] args)
{
int x=0,y=1;
for(;y<=100;y++)
{
x=x+y;
} Systemoutprintln(x);
}
public class Demo {
public static void main(String[] args) {
int n = 20;
int sum = 0;
for (int i = 1; i <= n; i++) {
Systemoutprint(fibo(i) + "\t");
sum +=fibo(i);
}
Systemoutprintln("\n菲波那契数列的前20项和为:"+sum);
}
private static int fibo(int n) {
if (n == 1)
return 1;
if (n == 2)
return 1;
return fibo(n - 1) + fibo(n - 2);
}
}
不知道你的平均数要不要取整 下面是比较懒得做法:
public static void main(String[] args) throws Exception {int a[] = {1,2,3,4,5,6,7,112};
int tmp = 0;
for(int b : a){
tmp += b;
}
Systemoutprintln("和:"+tmp);
Systemoutprintln("平均:"+tmp/alength);
}
再给你一个看上去好点的:
public class MyMath{int[] array;
int sum;
float average;
public MyMath(){
//懒得写了
}
public MyMath(inta){
array = new int[alength];
int index = 0;
sum = 0;
for(int b:a){
array[index++]=b;
sum += b;
}
average = (float)sum/alength;
}
public int[] getArray() {
return array;
}
public int getSum() {
return sum;
}
public float getAverage() {
return average;
}
public static void main(String[] args){
MyMath m =new MyMath(1,2,3,4,5,6,7,8);
int[] result = mgetArray();
Systemoutprintln(mgetSum());
Systemoutprintln(mgetAverage());
}
}
Public Sum, Num, Avg As Single
Private Sub cmdOK_Click()
Sum = Val(Text1) + Sum
Num = Num + 1
Text1 = ""
MeCls
Print Sum
End Sub
Private Sub Form_Click()
MeCls
Avg = Sum / Num
Print Avg
Num = 0
Sum = 0
End Sub
Private Sub Form_Load()
Text1 = ""
End Sub
#include<stdioh>void main(){ int i,n; int sum=0; scanf("%d",&n); for(i=1;i<=n;i++) sum+=i; printf("%d",sum);} void main(){ int i=0,n; int sum=0; while(i<=n) { i++;sum+=n; }printf("%d",sum);}------------------ int i=0,sum=0; int n;scanf("%d",&n);do{ i++; sum+=i;}while(i<=n);printf("%d",sum);}
效果如图
参考代码
import javaawt;import javaawtevent;
import javaxswing;
public class AddFrame extends JFrame implements ActionListener {
JTextField jtf1, jtf2, jtf3;
public AddFrame() {
//控件的创建
jtf1 = new JTextField(10);
JLabel jl = new JLabel("+");
jtf2 = new JTextField(10);
JButton jbAdd = new JButton("求和");
jbAddaddActionListener(this);//给按钮添加事件响应
jtf3 = new JTextField(10);
jtf3setEditable(false);//结果文本框不能被编辑
add(jtf1);
add(jl);
add(jtf2);
add(jbAdd);
add(jtf3);
setLayout(new FlowLayout());//窗口流式布局
setSize(458, 95);
setTitle("求和");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//当按钮被点击时执行actionPerformed方法
public void actionPerformed(ActionEvent e) {
try{
double d1 = DoubleparseDouble(jtf1getText());
double d2 = DoubleparseDouble(jtf2getText());
jtf3setText(""+(d1+d2));
}catch(Exception ex){
JOptionPaneshowMessageDialog(this, "提示:只能输入两个数字,不能为空或者其他字符");
}
}
public static void main(String[] args) {
new AddFrame();//启动窗口
}
}
public class demo {
public static long getFactorial(long num)
{
long sum = 1;
for(int i=1;i<=num;i++)
{
sum = i;
}
return sum;
}
public static void main(String[] args) {
long sum = 0;
for(long i=1;i<24;i+=2)
{
sum += getFactorial(i);
}
Systemoutprintln(sum);
}
}
以上使用Java语言写的。仅供参考
以上就是关于1!+2!+3!+....20!求和。 的VB程序编写全部的内容,包括:1!+2!+3!+....20!求和。 的VB程序编写、java的1+100求和的程序写法、用java编写一个输出菲波那契数列的前20项,并求和的程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)