空心NxN方形使用循环

时间:2014-10-29 21:12:53

标签: java loops

所以我在Java中练习,我需要让用户输入1到9之间的数字,并根据输入的数字输出一个空心的NxN平方。我需要使用循环和条件。想法是这样的:

输入数字:5

输出:(点将被空格替换)

55555
5   5
5   5
5   5
55555

我似乎无法正确输出。我在第一行的开头标记了一条额外的“中间”行。这就是我所拥有的:

import java.util.Scanner ;

public class Assignment1Q4 {
  public static void main (String [] args) {
    int n, counter1=1, counter2=1 ;
    Scanner kbd = new Scanner(System.in) ;
    System.out.print("Please enter a number between 1 and 9: ") ;
    n = kbd.nextInt() ;

    if(n<=9) {
        while(counter1<=n) { //counter1 keeps track of what line it's on
            if(counter1==1) { //determines if first line
                while(counter2<=n) { //counter2 keeps track of how many characters printed
                    System.out.print(n);
                    counter2=counter2+1 ;
                }
            }
            if(counter1==n) { //determines if last line
                counter2=1 ;
                while(counter2<=n) { //does the same as the while for the first line
                    System.out.print(n);
                    counter2=counter2+1 ;
                }
            }
            else { //is it the lines in between
                counter2=1 ;
                while(counter2<=n) {
                    if(counter2==1 || counter2==n) { //is it first or last char
                        System.out.print(n) ;
                    }
                    else {
                        System.out.print(" ") ;
                    }
                    counter2=counter2+1 ;
                }
            }

            System.out.println() ; //makes sure each new line does print on a new line
            counter1=counter1+1 ;
        }
    }
    else {
        System.out.print("Error: The number you have entered is not between 1 and 9") ;
    }
}

}

这是输出:

Please enter a number between 1 and 9: 4
44444  4
4  4
4  4
4444

3 个答案:

答案 0 :(得分:0)

在你的主while循环中你有一个

if (counter1 == 1) //determines if first line 
   ...
if (counter1 == n) //determines if last line
   ...
else //is it the lines in between
   ...

所以会发生在你的while循环的第一次迭代中,第一个循环的绘图和中间行的逻辑正在完成

解决此变化

if (counter1 == n) //determines if last line

else if (counter1 == n) //determines if last line

答案 1 :(得分:0)

问题是else与if的关联。用||组合两个if块程序运行正常

package ocpjp.hessam;

import java.util.Scanner ;

public class Assignment1Q4 {
  public static void main (String [] args) {
    int n, counter1=1, counter2=1 ;
    Scanner kbd = new Scanner(System.in) ;
    System.out.print("Please enter a number between 1 and 9: ") ;
    n = kbd.nextInt() ;

    if(n<=9) {
        while(counter1<=n) { //counter1 keeps track of what line it's on

            if(counter1==1 || counter1==n) { //determines if last line
                counter2=1 ;
                while(counter2<=n) { //does the same as the while for the first line
                    System.out.print(n);
                    counter2=counter2+1 ;
                }
            }
            else { //is it the lines in between
                counter2=1 ;
                while(counter2<=n) {
                    if(counter2==1 || counter2==n) { //is it first or last char
                        System.out.print(n) ;
                    }
                    else {
                        System.out.print(" ") ;
                    }
                    counter2=counter2+1 ;
                }
            }

            System.out.println() ; //makes sure each new line does print on a new line
            counter1=counter1+1 ;
        }
    }
    else {
        System.out.print("Error: The number you have entered is not between 1 and 9") ;
    }
}
}

答案 2 :(得分:0)

illeyezur的回答是正确的。我想介绍一种不同的方法。

我认为您没有找到问题,因为代码有点复杂。尽量简化。

练习说:如果你在广场的边缘打印数字,否则打印空格。

这样的事情:

import java.util.Scanner ;

public class Assignment1Q4 {
    public static void main (String [] args) {
        int n;
        Scanner kbd = new Scanner(System.in) ;
        System.out.print("Please enter a number between 1 and 9: ") ;
        n = kbd.nextInt() ;

        if (n > 9) {
            System.out.println("Error: The number you have entered is not between 1 and 9");
            return;
        }

        String nS = String.valueOf(n);
        String eS = " ";

        for (int x = 1; x <= n; x++) {
            for (int y = 1; y <= n; y++ ) {
                String toPrint = (x == 1 || x == n || y == 1 || y == n ) ? nS : eS;
                System.out.print(toPrint);
            }
            System.out.println();
        }
    }
}