ループのある直角三角形のチェッカーボードを作成しようとしていますが、どうすればよいかわかりません。ユーザーが2つの整数と1つの文字を入力して、三角形を埋める長さ、サイズ、文字を示すようにしようとしています。入力3 5wは次のようになります。
w
ww
www
wwww
wwwww
w w
ww ww
www www
wwww wwww
wwwwwwwwww
w w w
ww ww ww
www www www
wwww wwww wwww
wwwwwwwwwwwwwww
これまでに直角三角形を1つ作成しましたが、それがこれを開始する正しい方法であるかどうかはわかりません。市松模様の作り方にもこだわっています。私はJavaを初めて使用するので、プログラムを開始する方法を理解するのは困難です。
public class Triangle {
public static void main( String[] args ) {
for( int i = 1; i <= 10; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print("w");
}
System.out.println();
}
}
}
public class Triangle {
public static void main(String[] args) {
print(5, 3, 'w');
}
private static void print(int h, int H, char x) {
//h for height of the triangle, H for the height of pattern and x the char.
for(int n = 1; n <= H; n++) {
printTriangle(n, h, x);
}
}
private static void printTriangle(int numTriangles, int h, char x) {
for(int i = 0; i < h; i++) {
//Let's print the line in the triangles with spaces.
for(int n = 0; n < numTriangles - 1; n++) {
for(int j = 0; j <= i; j++) {
System.out.print(x);
}
for(int j = i+1; j < h; j++) {
System.out.print(" ");
}
}
//The last one has no spaces so:
for(int j = 0; j <= i; j++) {
System.out.print(x);
}
System.out.println();
}
}
}
はい、どうぞ。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加