如何在 Java 中打印 descending/ascending 个空格?
How to print a descending/ascending number of spaces in Java?
我正在尝试打印一个数字,打印 3 spaces,然后打印星号,然后下一行打印 2 spaces,/ 和星号,然后打印 1 space, // 和星星等等。
我有打印所有斜杠和星号的代码,但我不知道如何让 space 以降序打印。这是一个作业,我必须使用嵌套的 for 循环。有什么想法吗?
PS 我不想要一个确切的答案(即 "type in this code"),我只想要一个能为我指明正确方向的建议。
我目前的情况是(H 是尺度):
public class Pattern { //program that prints a boxed-in design
public static final int H = 9;
public static void main(String[] args) {
line();
design();
}
public static void line() {
System.out.print("+");
for (int d=1; d <=H-2; d++) {
System.out.print("-");
}
System.out.println("+");
}
public static void design() {
top();
}
public static void top() {
for (int row = 1; row <=H-2; row++){
System.out.print("|");
for (int sp =3; sp>=1; sp--){
System.out.print(" ");
}
for (int fsl=1; fsl<=row-1; fsl++){
System.out.print("/");
}
for (int star=1; star<=1; star++){
System.out.print("*");
}
for (int bsl=1; bsl<=row-1; bsl++){
System.out.print("\");
}
System.out.print("|");
System.out.println();
}
}
}
所以如果我理解正确的话,你的 row
变量从 1 到 4,你想要第 1 行中的 3 spaces,第 2、1 行中的 2 spaces space 在第 3 行和 0 spaces 在第 4 行?我建议你应该能够找到一个算术表达式(就像你已经找到 row-1
的斜线数一样)来为每个 line/row 提供正确的 space 数。如果我需要说更多,请随时对此答案添加评论。
package com.acme;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.Comparator.reverseOrder;
public class PrintIt {
public static void main(String[] args) {
printSpacesWithStar(10, Order.ASC);
printSpacesWithStar(10, Order.DESC);
}
private static void printSpacesWithStar(int numbers, Order order) {
Stream<Integer> streamOfInt = IntStream.rangeClosed(1, numbers)
.boxed();
switch (order) {
case ASC:
streamOfInt
.sorted(reverseOrder())
.forEach(Test::printingLogic);
break;
case DESC:
streamOfInt
.forEach(Test::printingLogic);
break;
}
}
private static void printingLogic(Integer currentValue) {
for (int k = 1; k < currentValue; k++) {
System.out.print(" ");
}
System.out.println("*");
}
enum Order {
ASC, DESC;
}
}
我正在尝试打印一个数字,打印 3 spaces,然后打印星号,然后下一行打印 2 spaces,/ 和星号,然后打印 1 space, // 和星星等等。 我有打印所有斜杠和星号的代码,但我不知道如何让 space 以降序打印。这是一个作业,我必须使用嵌套的 for 循环。有什么想法吗? PS 我不想要一个确切的答案(即 "type in this code"),我只想要一个能为我指明正确方向的建议。
我目前的情况是(H 是尺度):
public class Pattern { //program that prints a boxed-in design
public static final int H = 9;
public static void main(String[] args) {
line();
design();
}
public static void line() {
System.out.print("+");
for (int d=1; d <=H-2; d++) {
System.out.print("-");
}
System.out.println("+");
}
public static void design() {
top();
}
public static void top() {
for (int row = 1; row <=H-2; row++){
System.out.print("|");
for (int sp =3; sp>=1; sp--){
System.out.print(" ");
}
for (int fsl=1; fsl<=row-1; fsl++){
System.out.print("/");
}
for (int star=1; star<=1; star++){
System.out.print("*");
}
for (int bsl=1; bsl<=row-1; bsl++){
System.out.print("\");
}
System.out.print("|");
System.out.println();
}
}
}
所以如果我理解正确的话,你的 row
变量从 1 到 4,你想要第 1 行中的 3 spaces,第 2、1 行中的 2 spaces space 在第 3 行和 0 spaces 在第 4 行?我建议你应该能够找到一个算术表达式(就像你已经找到 row-1
的斜线数一样)来为每个 line/row 提供正确的 space 数。如果我需要说更多,请随时对此答案添加评论。
package com.acme;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.Comparator.reverseOrder;
public class PrintIt {
public static void main(String[] args) {
printSpacesWithStar(10, Order.ASC);
printSpacesWithStar(10, Order.DESC);
}
private static void printSpacesWithStar(int numbers, Order order) {
Stream<Integer> streamOfInt = IntStream.rangeClosed(1, numbers)
.boxed();
switch (order) {
case ASC:
streamOfInt
.sorted(reverseOrder())
.forEach(Test::printingLogic);
break;
case DESC:
streamOfInt
.forEach(Test::printingLogic);
break;
}
}
private static void printingLogic(Integer currentValue) {
for (int k = 1; k < currentValue; k++) {
System.out.print(" ");
}
System.out.println("*");
}
enum Order {
ASC, DESC;
}
}