0

私が現在取り組んでいるラボでは、これが助けになります。

output = ""
output += word

loop i that runs 
{
output += word.charAt(i)

loop j
{    
  output += " "

}

output += word.charAt( ______ )+ "\n"
}

StringBuffer sb = new StringBuffer(word);

output += word backwards

これが私のコードです。

import static java.lang.System.*;

class BoxWord
{
 private String word;

public BoxWord()
{
    word="";
}

public BoxWord(String s)
{
    word = s;
}

public void setWord(String w)
{
    word = w;
}

public String toString()
{
    String output="";
    output += word + "\n";
    for(int i =1; i<word.length(); i++)
    {
        output += word.charAt(i);
        for(int j =i+1; j<word.length()-i; j++)
        {
            output += " ";
        }
        output += word.charAt((word.length()-i)) + "\n";
    }
    StringBuffer sb = new StringBuffer(word);
    output += sb;
            
    return output+"\n";
}
}

相互参照用のランナー クラスは次のとおりです。

import static java.lang.System.*;

import java.util.Scanner;

public class Lab11f
{
 public static void main( String args[] )
   {
   Scanner keyboard = new Scanner(System.in);
    String choice="";
        do{
            out.print("Enter the word for the box : ");
            String value = keyboard.next();
            

                //instantiate a BoxWord object
         BoxWord bw = new BoxWord(value );
            //call the toString method to print the triangle
            System.out.println( bw );

            System.out.print("Do you want to enter more data? ");
            choice=keyboard.next();
        }while(choice.equals("Y")||choice.equals("y"));
}
}

現在、次のように出力しています。

四角

あなたは

ああ

四角

次のようにする必要がある場合:

四角

Q>>>>R

う>>>>あ

あ>>>>う

か>>>>か

ERAUQS (">" は空白を表し、最後の列は整列する必要があります)

ラボシートの Google ドキュメント バージョンへのリンクは次のとおりです: https://docs.google.com/open?id=0B_ifaCiEZgtcVTAtT2t0ajRPLUE

更新

わかりました、それはほとんどの部分で機能しています。私は今これを取得しています:

四角

q>>>>へ

u>>>>r

あ>>>>あ

r>>>>u

へ>>>>q

四角

更新#2 今、私の結果は次のようになります:

四角

q>>>>へ

u>>>>r

あ>>>>あ

r>>>>u

えろく

4

1 に答える 1

0

あなたの問題は2番目のループにあります。に等しいスペースを追加しますword.length - 2

for(int j = 0; j < word.length()-2; j++)
{
     output += " ";
}

それ以外の

for(int j =i+1; j<word.length()-i; j++)
{
     output += " ";
}

編集:
あなたがあまり得たことがわかりませんでした。下の四角は反転していません。

for(int i =1; i<word.length()-1; i++) // -1 so the last letter is not added
{
    output += word.charAt(i);
    for(int j =i+1; j<word.length()-i; j++)
    {
        output += " ";
    }
    output += word.charAt((word.length()-i-1)) + "\n";
}

文字列を反転するには、使用する必要がありますStringBuffer.reverse()

output += sb.reverse();
于 2012-11-24T00:55:30.383 に答える