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

私はあなたの質問に答えるだろうが、まずは提案をすることから始めよう:機能を使う!あなたの1つの大きなメインを機能に分解してください。コードをより小さな関数に分割すると、理解しやすく維持しやすくなります(特に物事を見つけるために)。どこでもコメントを使うと良い機能になります。たとえば、か、getInput()は、あなたの質問のために今printFilledRectangle(int型の幅、int型の高さ、文字記号)など

は非満たされた長方形を印刷するためのルールについて考えてみてください。

1)これが最初の行である場合、n倍(n =幅)の を印刷します。2)これが最後の行である場合は、シンボルをn回印刷します 3)1つの記号を印刷してn-2スペースを印刷し、別の記号を印刷する

これで、これらのルールを非塗りつぶしのループに組み込むようになりました。希望が役立ちます。

EDIT:

[OK]を - まあ、私はあなたが現在のコードで間違って何

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

関連する問題