[英]Printing spaces to align numbers according to my pyramid pattern
聽起來比看起來容易得多。 基本上我有我的代碼完成這是我的輸出,其中前導數字是程序接收的任何整數作為輸入。 在這種情況下,n = 5:
1
21
321
4321
54321
但這就是它的樣子:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
在保持這種模式的同時,我應該如何在我的數字之間添加空格? 我已經嘗試過這里和那里的編輯,但它不斷出現如下:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
import java.util.Scanner;
public class DisplayPattern {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer and I will display a pattern for you: ");
int n = input.nextInt();
displayPattern(n);
}
public static void displayPattern(int n) {
final int MAX_ROWS = n;
for (int row = 1; row <= MAX_ROWS; row++) {
for (int space = (n - 1); space >= row; space--) {
System.out.print(" ");
}
for (int number = row; number >= 1; number--) {
System.out.print(number + " "); /*<--- Here is the edit.*/
}
System.out.println();
}
}
編輯:@weston讓我在第二次嘗試時顯示我的代碼的樣子。 這真的不是一個很大的變化。 我所做的只是在數字的打印聲明后添加一個空格。 我將編輯上面的代碼以反映這一點。 由於它似乎可能更接近我的結果,我將從那里開始並繼續絞盡腦汁。
我設法使該程序正常工作,但這僅適用於單個數字(即最多9個)。
import java.util.Scanner;
public class Play
{
public static class DisplayPattern
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer and I will display a pattern for you: ");
int n = input.nextInt();
displayPattern(n);
}
public static void displayPattern(int n)
{
final int MAX_ROWS = n;
final int MAX_COLUMNS = n + (n-1);
String output = "";
for (int row = 1; row <= MAX_ROWS; row++)
{
// Reset string for next row printing
output = "";
for (int space = MAX_COLUMNS; space > (row+1); space--) {
output = output + " ";
}
for (int number = row; number >= 1; number--) {
output = output + " " + number;
}
// Prints up to n (ignore trailing spaces)
output = output.substring(output.length() - MAX_COLUMNS);
System.out.println(output);
}
}
}
}
n
。 在第i
行打印(n-1 - i) * length(n)
空格中,然后打印i+1
數字,因此它以1與length(n)
空格分開結束。
public static void printPiramide(int n) {
int N = String.valueOf(n).length();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) {
for (int k = 0; k < N; k++)
System.out.print(" ");
}
for (int j = i+1; j > 0; j--) {
int M = String.valueOf(j).length();
for (int k = 0; k < (N - M)/2; k++) {
System.out.print(" ");
}
System.out.print(j);
for (int k = (N - M)/2; k < N +1; k++) {
System.out.print(" ");
}
}
System.out.println();
}
}
public class DisplayPattern{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer and I will display a pattern for you: ");
int n = input.nextInt();
List<Integer> indentList = new ArrayList<Integer>();
int maxLength= totalSpace(n) + (n-1);
for(int i = 1; i <= n; i++ ){
int eachDigitSize = totalSpace(i);
int indent = maxLength - (eachDigitSize+i-1);
indentList.add(indent);
}
for(int row = 1; row<=n; row++){
int indentation = indentList.get(row-1);
for(int space=indentation; space>=0; space--){
System.out.print(" ");
}
for(int number = row; number > 0; number--){
System.out.print(number + " ");
}
System.out.println();
}
}
private static int totalSpace(int n) {
int MAX_ROWS = n;
int count = 0;
for(int i = MAX_ROWS; i >= 1; i--){
int currNum = i;
int digit;
while(currNum > 0){
digit=currNum % 10;
if(digit>=0){
count++;
currNum = currNum/10;
}
}
}
return count;
}
}
它適用於任意數量的行(n) 。
java-8解決問題的方法:
IntStream.rangeClosed(1, MAX)
.forEach(i -> IntStream.range(0, MAX)
.map(j -> MAX - j)
.mapToObj(k -> k == 1 ? k + "\n" : k <= i ? k + " " : " ")
.forEach(System.out::print)
);
MAX = 5
輸出:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
對於底行,您有正確的空格數。 但是對於從底部開始的下一行,您在左側缺少一個空格(4個空格超出1個空格)。 在下一行中,您在左側缺少兩個空格(3個在2個空格之外)......依此類推。
您在每行的開頭添加了一些空格,但您只考慮了要打印的位數。 但是,您還需要考慮前一行中打印的空格數。
一旦你使這個部分工作,你也可以考慮當你開始達到兩位數的數字以及它如何影響空間數時會發生什么。 你真正想做的是在左邊填充字符串,使它們的長度與最長的行相同。 您可以檢查String.format()方法來執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.