2011-10-18 21 views
0

我的任务是分配打印矩形。用户将输入长度,宽度和他们选择打印的符号。由此矩形将被输出。这部分任务已完成。我的下一个任务是从负值打印一个矩形,只打印矩形的轮廓(内部区域为空白)。我很难创建一个在这种情况下使用的算法。在JAVA中打印带有符号的矩形

任何帮助,将不胜感激。

干杯。

import java.util.Scanner; 

    public class Rectangle 
{ 
public static void main(String[] args) 
{ 

    boolean loopExecuted; 
    String userContinue = "NO"; 

    // Loop to ask to do another rectangle 
    do 
    { 

    // Variables 
    int length; 
    int width; 
    String symbol; 
    char symbolz; 
    int xLength; 
    int yWidth; 


        // Name Scanner 
    Scanner input = new Scanner(System.in); 

        // What program will do 
    System.out.println("This program will print a rectangle with the symbol of your choice. If you enter a positve integer the rectangle" 
      + "will be filled, if you enter a negative integer, it will print the outline of the rectangle.\n"); 


        // Ask for user input and check for validity 

     do 
     { 
      System.out.println("Please enter the length of the rectangle that is less than or equal to -10 and less than or equal to 10."); 
      length = input.nextInt(); 
     } 
     while (length < -10 || length > 10 || length == 0); 

     do 
     { 
      System.out.println("Please enter the width of the rectangle that is less than or equal to -10 and less than or equal to 10."); 
      width = input.nextInt(); 
     } 
     while (width < -10 || width > 10 || width == 0); 


     System.out.println("Please enter the symbol to be used for the rectangle"); 
     symbol = input.next(); 
     symbolz = symbol.charAt(0); 

     System.out.println("You have entered the following values for length, width, and symbol: " + length + " ," + width + " ," + symbolz +"\n"); 


       // Algorithm to print filled in rectangle. 

     for (yWidth = 1; yWidth <= width; yWidth++) { 
      for (xLength = 1; xLength <= length; xLength++) { 
       System.out.print(symbolz); 
      } 

      System.out.println(" "); 
     } 

       // Algorithm to print outline of rectangle 

     //TODO 
     for(yWidth = 1; yWidth >= width; yWidth--){ 
      for (xLength = 1; xLength >= length; xLength--){ 
       System.out.print(symbolz); 
      } 

      System.out.println(" "); 
     } 


        // Repeat the program 

    loopExecuted = false; 
    do 
      { 
       if (!loopExecuted) 
       { 
        System.out.println("\n\nWould you like to continue? Please either Yes or No"); 
       } 
       else 
       { 
        System.out.println("Please enter a valid response (Yes/No)"); 
       } 
       userContinue = input.next(); 
       userContinue = userContinue.toUpperCase(); 
       loopExecuted = true; 
      } 

    while (!"YES".equals(userContinue) && !"NO".equals(userContinue)); 

    // Case Insensitive 

    userContinue = userContinue.toUpperCase(); 

    } 
    while (userContinue.equals("YES")); 

    } 
    } 
+0

有什么不对当前的代码?你怎么知道它不能正常工作?你怀疑什么是错的,你有什么试图解决它? – bdares

回答

2

我会回答你的问题,但首先我会从提出建议开始:使用函数!把你的一个大主体分解成功能。将代码分解为更小的函数有助于使代码更容易理解并易于维护(尤其是查找内容)。在任何地方你使用评论将是一个很好的功能。例如,getInput(),printFilledRectangle(INT宽度,高度INT,字符符号)等

现在对于你的问题:

想想打印非填充矩形的规则。在我看来,有三个:

1)如果这是第一行,打印符号n次(其中n =宽度) 2)如果这是最后一行,打印符号n次 3)否则打印一个符号,打印n-2个空格,然后打印另一个符号

因此,现在将这些规则合并到您的未填充循环中。希望有所帮助。

编辑:

好 - 好,我可以让你开始

for (row = 0; row < height; row++) 
    if (row == 0 || row == height - 1) // first or last row 
     // print your symbol n times, where n = width 
    else // otherwise, this is an "internal" row 
     // print 1 symbol, n-2 spaces, then 1 symbol again 
+0

经过很多小时以及感觉像僵尸的开始阶段,我还没有找到如何实施这个建议的算法。你介意对此进行阐述吗? – fisherml