【问题标题】:Creating a Hollow Rectangle Using Asterisks in Java在 Java 中使用星号创建空心矩形
【发布时间】:2015-03-12 04:15:42
【问题描述】:

这是家庭作业。但是,我已经完成了几乎所有的代码,并且只漏掉了一部分。该程序应该根据用户指定的高度和宽度值打印出一个矩形。矩形需要是空心的,但事实并非如此。我是 Java 新手,所以我不确定在我的代码中实现什么来使矩形空心。任何帮助,将不胜感激。谢谢! 例子。

高度 =4 宽度 =8

我得到了什么

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

我需要什么

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

下面是我的代码。感谢您的帮助!

import java.util.Scanner;
public class Rectangle 
{
public static void main(String[] args)
{
int height;
int width;
Scanner keyboard = new Scanner(System.in); 
System.out.println("Please enter the height of the rectangle.");
height = keyboard.nextInt();
if (height < 0)
{
    height = 1;
}
System.out.println("Please enter the width of the rectangle.");
width = keyboard.nextInt();
if(width < 0)
{
    width = 1;
}

for(int h = 0; h < height; h++) 
{ 
    for(int w = 0; w < width; w++) 
    { 
        System.out.print("*"); 
    } 
    System.out.println(" "); 
} 
}
}

【问题讨论】:

  • 在打印 *.条件应检查 h 和 w 是否为 0 或 max(height-1 或 width-1)。给出比这更多的提示需要我把修改后的代码放在这里。我相信你不会想要它。

标签: java draw shapes


【解决方案1】:
   /**
    * Created by stanfordude on 3/11/15.
    *  These methods should work... I tested them     
    */
  public class HelpNewb {
     public void drawLine(int length, String s) {
          for(int i=0; i<length; i++)
              System.out.print(s);
        }
 public void drawHollowLine(int length) {

    System.out.print("*");
    drawLine(length-2, " ");
    System.out.print("*");
}


public void drawRectangle(int height, int length) {
    drawLine(length, "*");
    System.out.println();
    for(int i=0; i<height-2; i++) {
        drawHollowLine(length);
        System.out.println();
    }
    drawLine(length, "*");
}

public static void main(String[] args) {
    new HelpNewb().drawRectangle(6, 10);
}

  }

【讨论】:

    【解决方案2】:

    解决方案是包含一个 if 语句来控制程序何时应该打印星号或空格。在这种情况下,我们只想在第一行和最后一行以及第一列和最后一列打印星号。

    if(h==0 || h==height-1 || w==0 || w==width-1) {
        System.out.print("*");
    } else {
        System.out.print(" ");
    }
    

    【讨论】:

      【解决方案3】:

      //您好,我有一个简单的代码可能对您有所帮助

        import java.util.Scanner;
      
        public class Practice{
        static Scanner sc= new Scanner (System.in);
      
       public static void main(String [] args)        
      {
        System.out.print("Enter the height of the rectangle: ");
        int h = sc.nextInt();
      
        System.out.print("Enter the width of the rectangle: ");
        int w = sc.nextInt();
      
        for(int j=1; j<=h; j++)
        {  
          for(int i=1; i<=w; i++)
          {
            if(j ==1 || j==h || i==1 || i==w)  
            {
              System.out.print("*");
            }
            else
            {
                 System.out.print(" ");
            }
          }
           System.out.println();
        } 
       }
      }
      

      【讨论】:

        【解决方案4】:

        如果这是 Java How to Program(第 10 版)一书中的练习,并且您只能使用 while 循环和 if..else 语句,那么您可以这样做:

        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int sides;
            System.out.print("Enter the length of the side of the square: ");
            sides = input.nextInt();
        
            int count = 0;
            int counter = 1;
        
            // Prints out the first row 
            while (count < sides) {
                System.out.print("*");
                count++;
            }
            System.out.println();
            count = 0;
        
            // Prints out the hollow part
            while (counter < sides-1) {
                System.out.print("*");  // Prints the '*' at the beginning of the row
                while (count < sides - 2) { 
                    System.out.print(" "); // Prints the spaces
                    count++;
                }
                System.out.println("*"); // Prints the '*' at the end of the row
                count=0;
                counter++;
            }
        
            // Prints out the last row(same as the first one)
            while (count < sides) {
                System.out.print("*");
                count++;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2016-05-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-26
          相关资源
          最近更新 更多