在 Java 中使用星号创建空心矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29001777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:11:10  来源:igfitidea点击:

Creating a Hollow Rectangle Using Asterisks in Java

javadrawshapes

提问by Wyatt Bush

This is a homework assignment. However, I've completed almost all the code and am missing just one piece. The program is supposed to print out a rectangle according to user specified height and width values. The rectangle needs to be hollow, however it is not. I'm new to Java, so I'm not sure what to implement in my code to make the rectangle hollow. Any help would be appreciated. Thanks! Example.

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

Height =4 Width =8

高度 =4 宽度 =8

What I get

我得到的

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

What I need

我需要的

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

Below is my code. Thanks for the help!

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

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

回答by Tobi

The solution is to include an if-statement to control when the program should print an asterisk or an empty space. In this case, we only want to print the asterisk at the first & last row, as well as first & last column.

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

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

回答by stanfordude

   /**
    * 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);
}

  }

回答by YNX_BARGZ

//Hi I have a simple code that might help you

//嗨,我有一个简单的代码可以帮助你

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

回答by ved269

If this is for the exercise from the book Java How to Program (10th edition) and you can only use whileloops and if..elsestatements then you might do it like this:

如果这是用于 Java How to Program (10th edition) 一书中的练习,并且您只能使用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++;
    }
}