Jeffrey27:
自然数nを求めて、私は、この形式では、コンソールに印刷します:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
.
.
.
n . . . 5 4 3 2 1
4を入力し、これは私がこれまで持っているものです。
1
21
321
4321
私は数字の間にスペースを追加します。これは私のコードです:
import java.util.Scanner;
public class PatternTwo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int userInput;
System.out.println("Please enter a number 1...9 : ");
userInput = in.nextInt();
String s="";
int temp = userInput;
for(int i=1; i<=userInput; i++ ) {
for (int k= userInput; k>=i; k-- ) {
System.out.printf(" ");
}
for(int j =i; j>=1; j-- ) {
System.out.print(j);
}
System.out.println("");
}
}
}
Mr.Yellow:
それはピラミッドにならないように印刷するための番号の前にスペースを追加し、上記のダブルスペース。このようなもの:
import java.util.Scanner;
public class PatternTwo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int userInput;
System.out.println("Please enter a number 1...9 : ");
userInput = in.nextInt();
String s="";
int temp = userInput;
for(int i=1; i<=userInput; i++ ) {
for (int k= userInput; k>i; k-- ) { // <- corrected condition
System.out.printf(" ");
}
for(int j = i; j>=1; j-- ) {
System.out.print(j);
// check if not 1 to avoid a trailing space
if (j != 1) {
System.out.print(" ");
}
}
System.out.println("");
}
}
}
EDIT
おかげで/ U / shash678私は、すべての不要なまたは間違ったスペースを削除するために私の解決策を修正しました