寻求自然数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("");
}
}
}
答案 0 :(得分:4)
在要打印的数字前面添加一个空格,并将上面的空格加倍,以使其不是金字塔。像这样:
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("");
}
}
}
编辑
感谢/u/shash678,我更正了解决方案,以删除所有不必要或错误的空格
答案 1 :(得分:1)
一个更干净的解决方案如何避免使用嵌套的for循环呢?
public static void main(final String[] args) {
final Scanner scanner = new Scanner(System.in);
System.out.print("Please enter a number 1...9 : ");
final int n = scanner.nextInt();
final ArrayList<String> suffixes = new ArrayList<>();
for (int i = 1; i <= n; i++) {
suffixes.add(0, i + " ");
final String prefix = String.join("", Collections.nCopies(n - i, " "));
final String suffix = String.join("", suffixes);
System.out.println(prefix + suffix);
}
}
答案 2 :(得分:1)
以下是对答案的修改,当k
等于i
时,通过修改k个for循环退出条件,并且类似地当j
等于{{时,不会打印不必要的多余空白1}},将其分开处理。
主要的总体变化是,在k循环中,您需要打印2个空格而不是1个空格才能实现所需的右侧对齐:
i
用法示例:
import java.util.Scanner;
class PatternTwo {
private static void printPatternTwo(int n) {
for (int i = 1; i <= n; i++) {
for (int k = n; k > i; k--) {
System.out.print(" ");
}
for (int j = i; j >= 1; j--) {
System.out.print(j == i ? j : " " + j);
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter an integer between 1 and 9 inclusive: ");
int userNum = -1;
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
userNum = scanner.nextInt();
if (userNum >= 1 && userNum <= 9) {
scanner.close();
break;
} else {
System.out.println("ERROR: Input number was not between 1 and 9");
System.out.print("Enter a single digit number: ");
}
} else {
System.out.println("ERROR: Invalid Input");
System.out.print("Please enter an integer between 1 and 9 inclusive: ");
scanner.next();
}
}
printPatternTwo(userNum);
}
}
答案 3 :(得分:0)
打印数字时,请在数字之间留一个空格。
// this works up n == 9
System.out.print(" " + j);
对于n> 9,问题是您需要在固定数量的空间上打印数字和空格,但是数字的字符数包含更改。一种解决方案是使用制表符(如果想要很大的数字,则使用制表符)。
// when printing blank spaces use a tab instead
for (int k= userInput; k>=i; k-- ) {
System.out.printf("\t");
}
// then print number with tab in front
for(int j =i; j>=1; j-- ) {
System.out.print("\t"+j);
}
答案 4 :(得分:0)
如果您对流感到满意:
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;
revRange(1, temp).forEach(n -> printNumbers(n, temp));
}
static IntStream revRange(int from, int to) {
return IntStream.range(from, to)
.map(i -> to - i + from - 1);
}
private static void printNumbers(int n, int max) {
IntStream.rangeClosed(0, n -1)
.forEach(num -> System.out.print(" "));
revRange(1, max - n)
.forEach(num -> System.out.print(num + " "));
System.out.println();
}
}
答案 5 :(得分:0)
另一种替代方法是使用printf
分隔每行的间距
并对每个数字使用String.format
。
String.format("%1$" + length + "s", inputString)
如果inputString
> = inputString.length
返回length
,否则在inputString中填充空格,使长度等于给定的长度。
示例:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int userInput;
System.out.println("Please enter a number 1...9 : ");// you can use this also for numbers > 9
userInput = in.nextInt();
int digits = String.valueOf(userInput).length()+1;
for(int i = 1; i<=userInput; i++){
System.out.printf("%1$"+(digits*(userInput-i+1))+"s","");
for(int k = userInput - (userInput-i); k >= 1; k--){
System.out.print(String.format("%1$"+digits+"s", k));
}
System.out.println();
}
}