【问题标题】:Printing a Square with loops用循环打印正方形
【发布时间】:2013-11-27 09:14:30
【问题描述】:

//大学工作

您好,我正在尝试使用循环打印正方形,它还需要用户输入高度和宽度。 输出应该是这样的

.... . . ....

如有任何帮助,将不胜感激

import java.util.Scanner;
public class Ex1 {
    public static void main(String[] args) {

        Scanner input = new Scanner(System. in );
        System.out.print("Please enter the height of the box: ");
        int x = input.nextInt();

        System.out.println("Please enter a width for the box: ");
        int y = input.nextInt();
        drawbox(x, y);

    }

    static void drawbox(int x, int y) {

        for (int j = 0; j < y; j++) {
            System.out.println("*");

            System.out.println();
            for (int i = 0; i < x - 2; i++) {
                System.out.print("*");
                for (int z = 0; z < y - 2; z++) {
                    System.out.print(" ");
                }
                System.out.println("*");

                for (int i = 0; j < y; j++) {
                    System.out.println("*");
                }

            }
        }
    }
}

【问题讨论】:

  • 为什么要使用两个参数来打印正方形?一个参数(用户输入)你可以做到
  • 在正方形中:高度 == 宽度。如果用户可以输入高度和宽度,则必须打印一个矩形。请说明您究竟需要做什么。
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建 minimal reproducible example。使用edit 链接改进您的问题 - 不要通过 cmets 添加更多信息。谢谢!
  • "任何帮助" ...在这里不是有效的请求。请清楚地描述您的代码的预期和实际行为。不要让你的读者先消化你的需求,然后弄清楚你的代码到底在做什么来然后找出问题!
  • 而且不相关:您可能在谈论矩形,而不是正方形。然后:x/y 通常用于命名坐标!所以改为调用你的变量长度/高度!

标签: java loops


【解决方案1】:

改变你的循环
for(int i = 0; j<y ; j++){
      System.out.println("*");
 }

 for(int j = 0; j<y ; j++){
      System.out.println("*");
 }

要绘制矩形,改变你的drawbox代码:

static void drawbox(int x, int y) {
    for (int i = 0; i < y; i++) {
        System.out.print("*");
    }
    System.out.println();
    for (int i = 0; i < x - 2; i++) {
        System.out.print("*");
        for (int j = 0; j < y - 2; j++) {
            System.out.print(" ");
        }
        System.out.println("*");
    }
    for (int i = 0; i < y; i++) {
        System.out.print("*");
    }
    System.out.println();
}

【讨论】:

    【解决方案2】:

    改变

      for(int i = 0; j<y ; j++){
          System.out.println("*");
     }
    

      for(int i = 0; i<y ; i++){
          System.out.println("*");
     }
    

    【讨论】:

    • 也许添加一个简短的解释原因。
    【解决方案3】:

    你可以使用这个(见下文)。这将为第一行和最后一行打印 *。对于中间的行,它会打印一个星号,后跟空格并以星号结束。它还有另一种方法allStars,它在第一行和最后一行重用代码。

    static void drawbox(int height, int width){
        for (int i = 1; i <= height; i++) {
            if(i==1 || i==height){
                allStars(width);
            }else{
                System.out.print("* ");
                for(int k = 2; k < width; k++){
                    System.out.print("  ");
                }
                System.out.print("*");
                System.out.print("\n");
            }
        }
    }
    static void allStars(int width){
        for(int k = 1; k <= width; k++){
            System.out.print("* ");
        }
        System.out.print("\n");
    }
    

    【讨论】:

      【解决方案4】:

      解决问题后,您的解决方案已经足够好了。但是把这个当作简单的方法,希望你能从中得到一些想法

      private static void printSquare(int width,int length){
            for(int i=0;i<width;i++){
                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.append("* ");
                for (int j=2;j<length;j++){
                    if(i==0){
                      stringBuilder.append("* ");
                    }else if(i==width-1){
                      stringBuilder.append("* ");
                    }else {
                      stringBuilder.append("  ");
                    }
                }
                stringBuilder.append("* ");
                System.out.println(stringBuilder);
            }
      }
      

      printSquare(5,5);时,输出

      * * * * * 
      *       * 
      *       * 
      *       * 
      * * * * * 
      

      【讨论】:

      • 在我看来像 printSquare(7,5)
      【解决方案5】:

      一个简单的循环绘制正方形的算法

      public void draw(int length){
          for(int i = 0; i < length; i++){
              for(int j = 0;j < length; j++){
                  if(i!= 0 && j > 0 && i!= length - 1 && j!= length-1){
                      System.out.print(" ");
                  }
                  else System.out.print("*");
              }
              System.out.println();
          }
      }
      

      【讨论】:

        【解决方案6】:

        试试这个:

        import java.util.Scanner;
        
            public class Rectangle {
                public static void main(String args[]){
                     Scanner input = new Scanner(System. in );
                        System.out.print("Please enter the height of the box: ");
                        int x = input.nextInt();
        
                        System.out.print("Please enter a width for the box: ");
                        int y = input.nextInt();
                        drawbox(x, y);
        
                    }
        
                    static void drawbox(int x, int y) {
        
                        for (int j = 0; j < y; j++) {
                            System.out.print("* ");
                        }
                        System.out.println();
                            for (int i = 0; i < x; i++) {
                                System.out.print("* ");
                                for (int z = 0; z < y - 2; z++) {
                                    System.out.print("  ");
                                }
                                System.out.println("*");
                                }
        
                                for (int k = 0; k < y; k++) {
                                    System.out.print("* ");
                                }
        
                        }    
        
                }
        

        输出:

        Please enter the height of the box: 10
        Please enter a width for the box: 10
        * * * * * * * * * * 
        *                 *
        *                 *
        *                 *
        *                 *
        *                 *
        *                 *
        *                 *
        *                 *
        *                 *
        *                 *
        * * * * * * * * * * 
        

        【讨论】:

        • 您忘记提供任何代码 sn-ps 或导致此答案。
        【解决方案7】:

        试试这个

        公共类 HelloWorld{

         public static void main(String []args){
            System.out.println("Hello World");
        int row = 15;
        int clmn =50;
        for(int i= 0;i<row; i++ ){
            for(int j= 0;j<clmn; j++ ){
                if(i == 0 || i == row-1 ||j == 0 || j == clmn){
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
        
            }
            System.out.println("*");
            
        }
         }
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多