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

时间:2015-03-12 04:15:42

标签: java draw shapes

这是一项家庭作业。但是,我已经完成了几乎所有的代码,而且我只缺少一件。程序应该根据用户指定的高度和宽度值打印出一个矩形。矩形需要是空心的,但事实并非如此。我是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(" "); 
} 
}
}

4 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

   /**
    * 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 :(得分:0)

//您好我有一个可以帮助您的简单代码

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

答案 3 :(得分:0)

如果这是“Java如何编程”(第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++;
    }
}