2011-10-18 52 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