2016/1/6 输出菱形 while语句计算阶乘分数之和

本文提供了一段Java代码示例,演示如何使用循环结构打印出菱形图案,并通过while循环计算从1到20的阶乘倒数之和。此程序展示了基本的Java语法和流程控制结构。
 1 public class LingXing {
 2 
 3 
 4     public static void main(String[] args) {
 5         //打印菱形
 6         for (int x=1;x<6;x++){
 7             for(int p=x;p<5;p++){
 8                 System.out.print(" ");
 9             }
10             for(int y=0;y<x;y++){
11                 System.out.print("* ");
12             }
13             
14             System.out.println();
15         }
16         for (int m=5;m>0;m--){
17             for(int p=m;p<=5;p++){
18                 System.out.print(" ");
19             }
20             for (int n=1;n<m;n++){
21                 System.out.print("* ");    
22             }
23             System.out.println();
24             }

 1 double t=1;
 2         double sum=0;
 3         double d=1;
 4         
 5         while (t<=20){
 6             
 7         d*=t;
 8         sum+=1/d;
 9         t++;
10             System.out.println(1/d);
11         }
12             System.out.println("1+1/2!+...+1/20!="+sum);
13         }
14         
15     }

 

转载于:https://www.cnblogs.com/haodayikeshu/p/5105125.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值