• java基础 ---- 练习for循环


    -----   使用for循环打印图形

    //打印矩形
    public class Print {
        public static void main(String[] args) {
            for(int i=1;i<=5;i++){
                for(int j=1;j<=5;j++){
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    }

    //打印等腰三角形
    public class Pint {
        public static void main(String[] args) {
            for(int i=1;i<=5;i++){
                for(int j=1;j<=5-i;j++){
                    System.out.print(" ");
                }
                for(int j=1;j<=2*i-1;j++){
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    }

    打印数字三角形

     1 /*
     2  * 打印等腰三角形
     3  * */
     4 public class PrintSJX {
     5     public static void main(String[] args) {
     6         //外层循环,执行五次,每次输出一行*
     7         for (int i = 1; i <= 5; i++) {
     8             for(int j=1;j<=5-i;j++){
     9                 System.out.print(" ");
    10             }
    11             //内层循环,执行五次,每次输出一个*
    12             for (int j = 1;j<=2*i-1;j++){
    13                 System.out.print(i);
    14             }
    15             
    16             System.out.println();
    17         }
    18     }
    19 }

     

    //打印数字菱形
    public class Pint {
        public static void main(String[] args) {
            for(int i=1;i<=5;i++){
                for(int j=1;j<=5-i;j++){
                    System.out.print(" ");
                }
                for(int j=1;j<=2*i-1;j++){
                    System.out.print(5-i);
                }
                System.out.println();
            }
            for(int i=1;i<=4;i++){
                for(int j=1;j<=i;j++){
                    System.out.print(" ");
                }
                for(int j=1;j<=2*(5-i)-1;j++){
                    System.out.print(i);
                }
                System.out.println();
            }
        }
    }

     

    /**
     * 反平行四边形
     * @author bdqn
     *
     */
    public class PrintEx6 {
        public static void main(String[] args) {
            for(int i=1;i<=5;i++){
                for(int j=1;j<=i-1;j++){
                    System.out.print(" ");
                }
                for(int j=1;j<=7;j++){
                    if(j<=4 && j>=1 ||j==7){
                        System.out.print("#");
                    }
                    else{
                        System.out.print(".");
                    }
                }
                System.out.println();
            }
        }
    }

    ----  打印100 以内的素数

     1 import java.util.Scanner;
     2 
     3 
     4 public class Prime {
     5 
     6     public static void main(String[] args) {
     7     
     8         public static void main(String[] args) {
     9         int flag = 0;
    10         for(int i=1;i<=100;i++){
    11             for(int j=2;j<i;j++){
    12                 
    13                 if(i%j==0){
    14                     flag =1;  //标志位,不是质数
    15                     break;
    16                 }
    17             }
    18             if(flag==0){
    19                 System.out.print(i+"	");
    20             }
    21             flag = 0; //标志位清空
    22         }
    23         System.out.println();
    24         
    25         //更简单的方法:
    26         for(int i=1;i<=100;i++){
    27             for(int j=2;j<=Math.sqrt(i);j++){
    28                 
    29                 if(i%j==0){
    30                     flag =1;  //标志位,不是质数
    31                     break;
    32                 }
    33             }
    34             if(flag==0){
    35                 System.out.print(i+"	");
    36             }
    37             flag = 0;
    38         }
    39     }
    40 
    41 }

    ---恢复内容结束---

  • 相关阅读:
    《JAVA设计模式》之责任链模式(Chain of Responsibility)
    《JAVA设计模式》之迭代器模式(Iterator)
    《JAVA设计模式》之观察者模式(Observer)
    【python】effective python读书笔记
    【python】contextlib的使用
    【linux】乱七八糟命令
    【深度学习】使用Ray
    【深度学习】超参调节方式:Bayesian Opt
    【linux】scp不需要输入密码的方法
    【深度学习】论文TCN
  • 原文地址:https://www.cnblogs.com/obge/p/10707104.html
Copyright © 2020-2023  润新知