简体   繁体   English

打印带有嵌套循环的棋盘。 【JAVA]

[英]Printing a checker board with nested loops. [Java]

I'm having issues with getting my code to print. 我在打印代码时遇到问题。 I need to print a checker board with a grid the size of a user input. 我需要使用用户输入大小的网格来打印棋盘。

Example of what it should output. 它应输出的示例。

Input a size (must be larger than 1):
5

0 *   *   *
1   *   *  
2 *   *   *
3   *   *  
4 *   *   *

Here's my code: 这是我的代码:

import java.util.Scanner;
public class nestedpractice1 
{
    public static void main(String[] args)
    {
        Scanner kbinput = new Scanner(System.in); 
        //Create Size variable
        System.out.println("Input a size: ");
        int n = 0; n = kbinput.nextInt();

        for(int r = 0; r < n; r++)
        {
            for(int c = 0; c < r; c++)
                    {
                if((r%2) == 0)
                {
                    System.out.print("*");
                }
                else if((r%1) == 0)
                {
                    System.out.print(" *");
                }
            }
            System.out.println("");
            kbinput.close();
        }
    }
}

My code keeps printing 我的代码不断打印

**

****

By looking at your code, I found these potential issues: 通过查看您的代码,我发现了以下潜在问题:

  • kbinput.close(); should not be called inside the loop. 不应在循环内调用。
  • else if((r%1) == 0) should be else if (r % 2 != 0) , although in this case simply else is enough. else if((r%1) == 0)应该是else if (r % 2 != 0) ,尽管在这种情况下, else足够了。
  • in the else , you might want to do System.out.print(" "); else ,您可能想要做System.out.print(" ");
    instead of System.out.print(" *"); 而不是System.out.print(" *");
  • I believe for(int c = 0; c < r; c++) { should loop as long as c < n . 我相信for(int c = 0; c < r; c++) {应该循环只要c < n

This loop yields precisely the output you specified: 该循环精确地产生您指定的输出:

    for (int r = 0; r < n; r++) {
        System.out.print(r);
        for (int c = 0; c < n; c++) {
            System.out.print(r % 2 == 1 ^ c % 2 == 0 ? " *" : "  ");
        }
        System.out.println();
    }

I condensed the body of the inner loop to a single print statement. 我将内部循环的主体压缩为一个print语句。 This statement uses the ^ (xor) operator to test for a condition, and then the ?: (ternary) operator to print an asterisk if the condition is true or spaces if the condition is false . 该语句使用^ (XOR)运算符来测试一个条件,然后?:如果条件是(三元)运算符来打印一个星号true或空格如果条件是false

We could break up that single statement, while retaining its meaning, like so: 我们可以分解单个语句,同时保留其含义,如下所示:

            boolean isOddRow = r % 2 == 1;
            boolean isEvenCol = c % 2 == 0;
            System.out.print(isOddRow ^ isEvenCol ? " *" : "  ");

As explanation, we want to print a * only if the row and column are both even or both odd. 作为解释,我们仅在行和列均为偶数或均为奇数时才打印* So if the row is even but the column is odd, or if the row is odd but the column even, we print only spaces. 因此,如果行是偶数但列为奇数,或者如果行是奇数但列是偶数,则仅打印空格。

We could express the same logic using == instead of ^ by: 我们可以使用==代替^来表达相同的逻辑:

            boolean isEvenRow = r % 2 == 0;
            boolean isEvenCol = c % 2 == 0;
            System.out.print(isEvenRow == isEvenCol ? " *" : "  ");

Or if you prefer the longhand if..else over the shorthand ternary operator: 或者,如果您更喜欢速记if..else而不是速记三元运算符:

            if (isEvenRow == isEvenCol) {
                System.out.print(" *");
            } else {
                System.out.print("  ");
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM