for循环程序练习

来源:互联网 发布:数据分析职业 编辑:程序博客网 时间:2024/04/30 08:10

For循环

 

 

public class Xh                       //九九乘法表

{

      //程序主入口

      public static void main(String[] agrs)

      {

           //fou循环,这里的变量i代表着行数

           for(int i=1;i<10;i++)

           {

                 //fou循环,这里的变量i代表着列数

                 for(int j=1;j<i;j++)

                 {

                      //打印输出不换行

                      System.out.print(i+"*"+j+"="+i*j+"\t");     

                 }

                 //打印换行

                 System.out.println();

           }

      }

}

打印结果如下:

2*1=2

3*1=3   3*2=6

4*1=4   4*2=8  4*3=12

5*1=5   5*2=10 5*3=15  5*4=20

6*1=6   6*2=12 6*3=18  6*4=24  6*5=30

7*1=7   7*2=14 7*3=21  7*4=28  7*5=35 7*6=42

8*1=8   8*2=16 8*3=24  8*4=32  8*5=40 8*6=48  8*7=56

9*1=9   9*2=18 9*3=27  9*4=36  9*5=45 9*6=54  9*7=63  9*8=72

 

 

 

 

 

 

For循环

 

 

public class Sxh         //水仙花

{

       //程序主入口

       public static void main(String[] args)

       {

              //for循环,变量i定义水仙花数的范围

              for(int i=100;i<1000;i++)

              {

                     int ge=i%10;

                     int shi=i/10%10;

                     int bai=i/100;

              //判断三位数的三个数的三次方是否等于它本身,等于就打印输出

                     if(i==Math.pow(ge,3)+Math.pow(shi,3)+Math.pow(bai,3))

                     {

                            System.out.println("水仙花为"+i);

                     }

              }

       }

}

打印结果如下:

水仙花为153

水仙花为370

水仙花为371

水仙花为407

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

For循环

publicclass Xh                    //100以内的质数

{

       public static void main(String[] args)

       {

              //记录一行多少个质数换行

              int line=0;     

              //记录    质数相加

              int sum=0;

              //for循环变量i确定质数范围

              for(int i=2;i<100;i++)  

              {

                     //记录每一个数有多少个因数

                     int count=0;

                     for(int j=i-1;j>0;j--)

                     {

                           

                            if(i%j==0)

                            {

                            count++;

                            }

                           

                     }

                     //因数只有一个,那么这个数为质数

                     if(count==1)

                     {

                            System.out.print(i+"\t");

                            line++;

                     }

                     //判断一行五个质数换行

                     if(line%5==0)

                     {

                            System.out.println();

                     }

                     //质数相加sum=sum+i;

                     sum+=i;

                    

              }

              System.out.println("100以内的质数和为:"+sum);

             

             

       }

}

打印结果如下:

2       3      5       7       11

13      17     19      23      29

31      37     41      43      47

53      59     61      67      71

73      79     83      89     97

100以内的质数和为:4949

 

For循环

importjava.util.Scanner;//导入包              最大公约数,最小公倍数

public class Xh

{

       public static void main(String[] args)

       {

              //声明对象           创建对象

              Scanner scanner=newScanner(System.in);

              System.out.println("put anumber");

              //接收第一个数字

              int number1=scanner.nextInt();

              System.out.println("put othernumber");

              //接收第二个数字

              int number2=scanner.nextInt();

              int min=0;

              if(number1>number2)

              {

                     min=number2;

              }

              else

              {

                     min=number1;

              }

              for(int i=min;i>0;i--)

              {

                     if(number1%min==0&&number2%min==0)

                     {

                            System.out.println("最小公约数为"+i);

                            System.out.println("最大公倍数为"+number1*number2/i);

                            //跳出循环

                            break;   

                     }

                    

      

              }

             

       }

}

0 0
原创粉丝点击