在 java 中打印图案,如半球
Printing pattern in java like halfsphere
尝试打印像下面的半球这样的图案我已经添加了实际输出和预期输出以及我的代码任何人都可以帮助如何做到这一点。提前致谢
我的代码
public class PatternHalfSphere {
public static void main(String[] args) {
int i,j;
for(i = 1;i<=4;i++){
System.out.println();
for(int k=3;k>=i;k--){
System.out.print(" "+"*"+" ");
}
for(j=1;j<=i;j++){
System.out.print(" ");
}
}
for(int k=0;k<=3;k++) {
for(int l = 0; l<k;l++)
{
System.out.print(" "+"*"+" ");
}
System.out.println();
}
}
}
实际产量
* * *
* *
*
*
* *
* * *
预期输出
* * *
* *
*
* *
* * *
此答案采用您的确切代码,并稍作更改即可获得您期望的输出。你很接近,你只需要在每一行上打印一个间隔符,并且还从你的第二个外部 for
循环中切断一个迭代以避免打印 *
两次。
for (int i=1;i <= 3; i++) {
for (int k=3; k >= i; k--) {
// print a new spacer at the start of each line
if (k == 3) System.out.print(" ");
System.out.print(" " + "*" + " ");
}
for (int j=1; j <= i; j++) {
System.out.print(" ");
}
System.out.println();
}
// start at k=2 so as to NOT print a double single asterisk *
for (int k=2; k <= 3; k++) {
for (int l=0; l < k; l++) {
// print a new spacer at the start of each line
if (l == 0) System.out.print(" ");
System.out.print(" "+"*"+" ");
}
System.out.println();
}
* * *
* *
*
* *
* * *
快速而肮脏的解决方案
public static void main(String[] args) {
upperHalf(4);
bottomHalf(4);
}
private static void upperHalf(int size) {
for(int row = 0; row<size; row++){
String rowContent = "";
for(int col=0; col<size-row; col++){
rowContent+= " *";
}
if(!rowContent.equals(""))
System.out.println(rowContent);
}
}
private static void bottomHalf(int size) {
for(int row=2; row<=size; row++) {
String rowContent = "";
for(int col=0; col<row;col++)
{
rowContent+= " *";
}
System.out.println(rowContent);
}
}
首先,用其余变量初始化k。
int i, j, k;
那么你应该决定哪个 for 循环将负责打印单个 '*' 并相应地调整另一个。例如,如果您提前 1 步终止第一个循环,它应该可以修复这两个部分之间的差距。
现在我为第一个 for 循环保留单个 '*',并通过修改步骤在第二个 for 循环中跳过它。
- 正在初始化 k=2 而不是 k=0。修复单个 * 重复以及它们之间的 space。
- 完全删除了使用 j 作为计数器的 for 循环,因为它弄乱了输出中打印不需要的 * 更远的间距。
最后在第二个for循环之前添加了一个System.out.println()
,这样第二个for循环打印的*将从新的一行开始。
import java.util.*;
import java.lang.*;
class Rextester
{
public static void main(String args[])
{
for (int i=1;i <= 3; i++) {
System.out.println();
for (int k=3; k >= i; k--) {
System.out.print(" " + "*" + " ");
}
}
System.out.println();
for (int k=2; k <= 3; k++) {
for (int l=0; l < k; l++) {
System.out.print(" "+"*"+" ");
}
System.out.println();
}
}
}
最终,这是一个通过修改数字或在编写代码之前通过纸笔解决问题来解决的问题。
尝试打印像下面的半球这样的图案我已经添加了实际输出和预期输出以及我的代码任何人都可以帮助如何做到这一点。提前致谢
我的代码
public class PatternHalfSphere {
public static void main(String[] args) {
int i,j;
for(i = 1;i<=4;i++){
System.out.println();
for(int k=3;k>=i;k--){
System.out.print(" "+"*"+" ");
}
for(j=1;j<=i;j++){
System.out.print(" ");
}
}
for(int k=0;k<=3;k++) {
for(int l = 0; l<k;l++)
{
System.out.print(" "+"*"+" ");
}
System.out.println();
}
}
}
实际产量
* * *
* *
*
*
* *
* * *
预期输出
* * *
* *
*
* *
* * *
此答案采用您的确切代码,并稍作更改即可获得您期望的输出。你很接近,你只需要在每一行上打印一个间隔符,并且还从你的第二个外部 for
循环中切断一个迭代以避免打印 *
两次。
for (int i=1;i <= 3; i++) {
for (int k=3; k >= i; k--) {
// print a new spacer at the start of each line
if (k == 3) System.out.print(" ");
System.out.print(" " + "*" + " ");
}
for (int j=1; j <= i; j++) {
System.out.print(" ");
}
System.out.println();
}
// start at k=2 so as to NOT print a double single asterisk *
for (int k=2; k <= 3; k++) {
for (int l=0; l < k; l++) {
// print a new spacer at the start of each line
if (l == 0) System.out.print(" ");
System.out.print(" "+"*"+" ");
}
System.out.println();
}
* * *
* *
*
* *
* * *
快速而肮脏的解决方案
public static void main(String[] args) {
upperHalf(4);
bottomHalf(4);
}
private static void upperHalf(int size) {
for(int row = 0; row<size; row++){
String rowContent = "";
for(int col=0; col<size-row; col++){
rowContent+= " *";
}
if(!rowContent.equals(""))
System.out.println(rowContent);
}
}
private static void bottomHalf(int size) {
for(int row=2; row<=size; row++) {
String rowContent = "";
for(int col=0; col<row;col++)
{
rowContent+= " *";
}
System.out.println(rowContent);
}
}
首先,用其余变量初始化k。
int i, j, k;
那么你应该决定哪个 for 循环将负责打印单个 '*' 并相应地调整另一个。例如,如果您提前 1 步终止第一个循环,它应该可以修复这两个部分之间的差距。
现在我为第一个 for 循环保留单个 '*',并通过修改步骤在第二个 for 循环中跳过它。
- 正在初始化 k=2 而不是 k=0。修复单个 * 重复以及它们之间的 space。
- 完全删除了使用 j 作为计数器的 for 循环,因为它弄乱了输出中打印不需要的 * 更远的间距。
最后在第二个for循环之前添加了一个
System.out.println()
,这样第二个for循环打印的*将从新的一行开始。import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { for (int i=1;i <= 3; i++) { System.out.println(); for (int k=3; k >= i; k--) { System.out.print(" " + "*" + " "); } } System.out.println(); for (int k=2; k <= 3; k++) { for (int l=0; l < k; l++) { System.out.print(" "+"*"+" "); } System.out.println(); } } }
最终,这是一个通过修改数字或在编写代码之前通过纸笔解决问题来解决的问题。