【问题标题】:I'm trying to print this without just doing println for each line我试图打印这个而不只是为每一行做 println
【发布时间】:2016-01-08 22:20:09
【问题描述】:

我想通过对 lops 和/或 if 语句使用嵌套来打印它

ABABABAB
BABABABA
ABABABAB
BABABABA
ABABABAB
BABABABA
ABABABAB
BABABABA
public class chess {

    public static void main(String[] args) {
            // TODO Auto-generated method stub


            for (int i=0; i<4; i++)
            {
                for (int r = 0; r < 1; r++)
                {
                    if (r%2== 0)
                    {
                            System.out.print("A");

                    }

                    if (r%2==1)
                    {
                            System.out.print("B");

                    }
                }

                for (int x = 0; x < 1; x++)
                {
                    if (x%2== 0)
                    {
                            System.out.print("B");

                    }

                    if (x%2 == 1)
                    {
                            System.out.print("A");

                    }
                }

            }

    }

}

输出只是:ABABABAB

我不想只是来回做System.out.println("BABABABA")。

谢谢!我是编码新手,不胜感激。

【问题讨论】:

  • I don't want to just do System.out.println("BABABABA").. 你想用什么?
  • 嵌套 for 循环和/或 if 语句
  • 将x &lt; 1 和r &lt; 1 更改为x &lt; 8 和r &lt; 8

标签: java if-statement for-loop printing


【解决方案1】:

使用此代码,您应该无需使用System.out.println() 一次即可获得所需的内容。

您只需检查i 和j 的总和是否是一对,如果是则打印A,如果不是则打印B。

for (int i = 0 ; i < 8 ; i++){
    for (int j = 0 ; j < 8 ; j++){
        if ((i+j)%2 == 0) System.out.print("A");
        else System.out.print("B");
    }
    System.out.print("\n");
}

【讨论】:

    【解决方案2】:

    尽可能避免 if's:

    for (int i = 0 ; i < 8 ; i++){
        for (int j = 0 ; j < 8 ; j++){
            System.out.print( (char)('A' + (i+j)%2) );
        }
        System.out.println();
    }
    

    (对于其他有趣的模式,您可以将 %2 替换为 %3、%4、...。)

    【讨论】:

      【解决方案3】:
      for (int i = 0; i < 8; i++)
      {
          if(i % 2 == 0)
          {
              for (int r = 0; r < 8; r++)
              {
                  if (r % 2 == 0)
                  {
                       System.out.print("A");
                  }
                  if (r % 2 == 1)
                  {
                       System.out.print("B");
                  }
              }
          }
          else
          {
              for (int r = 0; r < 8; r++)
              {
                  if (r % 2 == 0)
                  {
                       System.out.print("B");
                  }
                  if (r % 2 == 1)
                  {
                       System.out.print("A");
                  }
              }
          }
          System.out.print("\n");
      }
      

      更改i 迭代的值将更改行数,更改r 迭代的值将更改您每行打印的A 和B 的数量.

      【讨论】:

      • 哈哈,天哪,哎呀。应该修好了!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多