码迷,mamicode.com
首页 > 其他好文 > 详细

for循环的使用(附代码)

时间:2018-05-15 22:48:37      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:等于   exti   nbsp   用例   代码   用户输入   测试   ++   微软雅黑   

1、输出1-100之间的奇数和 

 

 1 public class One {
 2     public static void main(String[] args) {
 3         int sum=0;
 4         for (int i=1;i<=100;i++){
 5             if ( i%2 !=0){
 6                 sum+=i;
 7             }
 8 
 9         }
10         System.out.println(sum);
11 
12     }
13 }

 

 

2、1+1/2!+1/3!+ …… 1/20!

 1 /**
 2  * 1+1/2!+1/3!+ …… 1/20!
 3  *
 4  * 
 5  * 处理阶乘的时候,使用两个for循环 ,
 6  * 最关键的是在最外面引入一个double局部变量设置值为1,进入第二个for循环进行运算,第二个for循环进行完后跳到第一个for循环,设置
 7  * m的值仍为1;在第一个for循环中,计算出来一次m的值进行一次累加,for循环都结束后输出全局变量sum的值
 8  * 运行出来为1或者1.0   解决办法是将sum 和m的数据类型都设置为double
 9  *
10  * */
11 public class Two {
12     public static void main(String[] args) {
13          
14          double sum=0;
15          for (int i=1;i<=20;i++) {
16              double m=1;
17              for (int j=i;j>0;j--){
18                 m*=j;
19              }
20              sum+=(1/m);
21 
22          }
23         System.out.println(sum);
24     }
25 }

 

3、写一个程序,由用户输入一个整数,判断这个数是否是素数(素数:只能被1和本身整除的数)

 

 1 public class Three {
 2 
 3     public static void main(String[] args) {
 4         int number;
 5         int count=0;
 6         System.out.println("请输入一个大于0的正整数:");
 7         Scanner scanner=new Scanner(System.in);
 8         number=scanner.nextInt();
 9 
10         /**
11          * 当输入测试用例9的时候,输出结果为 9是素数   9不是素数
12          * 再运算过程中 i 起始值为2 ,判断输出,再次循环判断输出
13          * 下面引入一个count计数器来解决这个问题,
14          * 在for循环中,如果能被i整数count自增1,如果最后count值大于等于1的话,就表示number会被2~sqrt(number)中的某个或者某些数字整除
15          * 就表示输入的数字不是素数
16          * */
17 
18         if (number<=3){
19             System.out.println(number+"是素数");
20         }else{
21             for (int i=2;i<Math.sqrt(number)+1;i++){
22                 if (number%i==0)
23                     ++count;
24             }
25             if (count>0){
26                 System.out.println(number+"不是素数");
27             }else {
28                 System.out.println(number+"是素数");
29             }
30 
31         }

 

 

4、求和。S = n+nn+nnn+nnnn+……的值。

 

 1 public class Four {
 2     public static void main(String[] args) {
 3         int n;
 4         int num;
 5         int sum=0;
 6         int a[];
 7         a=new int[20];
 8         Scanner scanner=new Scanner(System.in);
 9         System.out.println("请输入数字n");
10         n=scanner.nextInt();
11         System.out.println("输入个数:");
12         num=scanner.nextInt();
13         a[0]=n;
14         System.out.println(a[0]);
15         for (int i=1;i<num;i++){
16             a[i]= (int) (a[i-1]+n*(Math.pow(10,i)));
17             System.out.println(a[i]);
18             sum+=a[i];
19         }
20         sum+=a[0];
21         System.out.println(sum);
22     }
23 }

 

for循环的使用(附代码)

标签:等于   exti   nbsp   用例   代码   用户输入   测试   ++   微软雅黑   

原文地址:https://www.cnblogs.com/zhaozishuang/p/9042943.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!