JAVA-for循环案例(冒泡、九九乘法表、素数、菱…

2018-06-27 09:44:59来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

  1 冒泡排序
  2 public static void main(String[]args){
  3         int[] x={1,24,45,56,6,98,90,3,2,65,3};        
  4         for (int i = 0; i < x.length-1; i++) {
  5             //外循环是轮数
  6             for (int j = 0; j < x.length-i-1; j++) {
  7                 //内循环是次数
  8                 if(x[j+1]<x[j]){
  9                     int tem=x[j+1];
 10                     x[j+1]=x[j];
 11                     x[j]=tem;    
 12                 }
 13             }
 14         }
 15         for (int i = 0; i < x.length; i++) {
 16             System.out.print(x[i]+"\t");
 17         }
 18     }
 19 九九乘法表
 20 public static void main(String[]args){
 21         for (int i = 1; i < 10; i++) {
 22             for (int j = 1; j <=i; j++) {    
 23                 System.out.print(j+"*"+i+"="+j*i+"\t");
 24             }System.out.println();
 25         }
 26     }
 27 素 数
 28 public static void sum3() {
 29         int sum = 0;
 30         for (int i = 2; i <= 100; i++) {
 31             boolean k = true;
 32             for (int j = 2; j < i; j++) {
 33                 if (i % j == 0) {
 34                     k = false;
 35                     break;
 36                 }
 37             }
 38             if (k) {
 39                 System.out.print(" " + i);
 40                 sum += i;
 41             }
 42         }
 43         System.out.println(" \n=" + sum);
 44     }
 45 菱 形
 46 public static void pattern1() {
 47         int ling=50;
 48         for (int i = 1; i <= ling; i +=2) {
 49             for (int j = 0; j < i; j++) {
 50                 if(j==0){
 51                     for (int j2 =ling-1; j2>i; j2-=2) {
 52                         System.out.print(" ");
 53                     }    
 54                 }
 55                 System.out.print("*");                
 56             }
 57             System.out.println();
 58         }        
 59         for (int i = 1; i <= ling; i +=2) {
 60             for (int j = i; j < ling-2; j++) {
 61                 if(j==i){
 62                     for (int j2 =0; j2<i; j2+=2) {
 63                         System.out.print(" ");
 64                     }    
 65                 }
 66                 System.out.print("*");                
 67             }
 68             System.out.println();
 69         }
 70     }
 71 
 72 空 心 菱 形
 73 public static void pattern1() {
 74         int ling=18;
 75         for (int i = 1; i <= ling; i +=2) {
 76             for (int j = 0; j < i; j++) {
 77                 if(j==0){
 78                     for (int j2 =ling-1; j2>i; j2-=2) {
 79                         System.out.print(" ");                        
 80                     }                                    
 81                 }    
 82                 if(j==i-1||j==0){
 83                     System.out.print("*");
 84                 }else{
 85                     System.out.print(" ");        
 86                 }                    
 87             }
 88             System.out.println();
 89         }        
 90         for (int i = 1; i <= ling; i +=2) {
 91             for (int j = i; j < ling-2; j++) {
 92                 if(j==i){
 93                     for (int j2 =0; j2<i; j2+=2) {
 94                         System.out.print(" ");
 95                     }    
 96                 }
 97                 if(j==i||j==ling-3){
 98                     System.out.print("*");
 99                 }else{
100                     System.out.print(" ");        
101                 }                    
102             }
103             System.out.println();
104         }
105     }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:使用spring-boot-maven-plugin插件打包spring boot项目

下一篇:Spring 入门