简体   繁体   English

中空NxN正方形使用循环

[英]Hollow NxN square using loops

So I have an exercise in Java where I need to have a user input a number between 1 and 9 and have it output a hollow NxN square based off of the number they entered. 因此,我在Java中进行了一个练习,我需要让用户输入一个介于1到9之间的数字,并让其根据输入的数字输出一个空心的NxN平方。 I need to use loops and conditionals. 我需要使用循环和条件。 The idea would be to have something like this: 这个想法将是这样的:

input number: 5 输入数字:5

output: (dots would be replaced by spaces) 输出:(点将被空格替换)

55555
5   5
5   5
5   5
55555

I can't seem to get it to output correctly. 我似乎无法正确输出。 I have an extra "middle" line tagged on to the beginning of my first line. 我在第一行的开头加上了一个额外的“中间”行。 Here's what I have: 这是我所拥有的:

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") ;
    }
}

} }

This is the output: 这是输出:

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

In your main while loop you have an 在主while循环中,您有一个

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

So what happens is in the first iteration of your while loop both the drawing for the first loop and the logic for a middle line is being done 所以发生的是在while循环的第一次迭代中,第一个循环的绘图和中间线的逻辑都已完成

To solve this change 解决这一变化

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

to

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

The problem is the association of else with if. 问题是else与if的关联。 Combine two if blocks with || 将两个if块与||合并 and the program works fine 该程序工作正常

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") ;
    }
}
}

illeyezur's answer was correct. illeyezur的回答是正确的。 I would like to introduce a different approach. 我想介绍一种不同的方法。

I think you didn't find the issue because the code was a little bit complicated. 我认为您没有找到问题,因为代码有点复杂。 Try to simplify. 尝试简化。

The exercise says: print the number if you are on the edge of the square, SPACE otherwise. 练习说:如果您在正方形的边缘,请打印数字,否则请打印SPACE。

Something like this: 像这样:

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();
        }
    }
}

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

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