• Java如何使用while和for嵌套循环控制输出数据,使数据奇偶行不同


    /*
        题目1 使用Eclipse编写控制台应用程, 使用while循环在控制台打印10行10列的如下图形
        □ □ □ □ □ □ □ □ □ □
        ■ ■ ■ ■ ■ ■ ■ ■ ■ ■
        □ □ □ □ □ □ □ □ □ □
        ■ ■ ■ ■ ■ ■ ■ ■ ■ ■
        □ □ □ □ □ □ □ □ □ □
        ■ ■ ■ ■ ■ ■ ■ ■ ■ ■
        □ □ □ □ □ □ □ □ □ □
        ■ ■ ■ ■ ■ ■ ■ ■ ■ ■
        □ □ □ □ □ □ □ □ □ □
        ■ ■ ■ ■ ■ ■ ■ ■ ■ ■
    
        题目2 打印如下图形
        □ ■ □ ■ □ ■ □ ■ □ ■
        ■ □ ■ □ ■ □ ■ □ ■ □
        □ ■ □ ■ □ ■ □ ■ □ ■
        ■ □ ■ □ ■ □ ■ □ ■ □
        □ ■ □ ■ □ ■ □ ■ □ ■
        ■ □ ■ □ ■ □ ■ □ ■ □
        □ ■ □ ■ □ ■ □ ■ □ ■
        ■ □ ■ □ ■ □ ■ □ ■ □
        □ ■ □ ■ □ ■ □ ■ □ ■
        ■ □ ■ □ ■ □ ■ □ ■ □
    */
    
    public class Mission1 {
    /*    //题目1使用while循环实现
        public static void main(String[] args) {
            //定义i变量用于控制while循环换行
            int i = 1;
            while (i <= 10) {
                //定义一个j变量用于控制while循环在一行中输出几列数据
                int j = 1;
                while (j <= 10) {
                    if (i % 2 == 1) {
                        System.out.print("□ ");
                    } else {
                        System.out.print("■ ");
                    }
                    j++;
                }
                System.out.println();
                i++;
            }
        }*/
    
        //题目2使用while循环实现
    /*    public static void main(String[] args) {
            //定义i变量用于控制while循环换行
            int i = 1;
            while (i <= 10) {
                //定义一个j变量用于控制while循环在一行中输出几列数据
                int j = 1;
                while (j <= 10) {
                    //定义一个if用于判断是奇数行还是偶数行,执行不同的输出
                    if (i % 2 == 1) {
                        System.out.print((j % 2 == 1) ? "□ " : "■ ");   //用代表列数的j除以2的余数来控制奇偶位的输出
                    } else {
                        System.out.print((j % 2 == 1) ? "■ " : "□ ");
                    }
                    j++;//j如果小于等于10,则持续执行以上操作,然后j++,直到输出j列为止
                }
                System.out.println();
                i++;    //i如果小于等于10,输出一个换行符,然后i++,直到输出10行为止
            }
        }*/
    
        //题目2使用for循环实现
        public static void main(String[] args) {
            //定义i用来控制输出行数,j用来控制输出列数
            for (int i=1;i<=10;i++){            //i如果小于等于10,只输出一个换行符,然后i++,直到输出10行为止
                for (int j=1;j<=10;j++){        //j如果小于等于10,则持续执行以上操作,然后j++,直到输出j列为止
                    if (i % 2 == 1) {
                        System.out.print((j % 2 == 1) ? "□ " : "■ ");   //用代表列数的j除以2的余数来控制奇偶位的输出
                    } else {
                        System.out.print((j % 2 == 1) ? "■ " : "□ ");
                    }
                }
                System.out.println();
            }
        }
    
    }
  • 相关阅读:
    IXmlSerializable With WCFData Transfer in Service Contracts
    Difference Between XmlSerialization and BinarySerialization
    Using XmlSerializer (using Attributes like XmlElement , XmlAttribute etc ) Data Transfer in Service Contracts
    Introducing XML Serialization
    Version Tolerant Serialization
    Which binding is bestWCF Bindings
    Data Transfer in Service Contracts
    DataContract KnownTypeData Transfer in Service Contracts
    Using the Message ClassData Transfer in Service Contracts
    DataContract POCO SupportData Transfer in Service Contracts
  • 原文地址:https://www.cnblogs.com/zengyu1234/p/14826611.html
Copyright © 2020-2023  润新知