更改与 Java 中的嵌套循环相关的输出
Alter output that relates to nested loops in Java
import static java.lang.System.*;
import java.util.Scanner;
public class TriangleFour{
public static void createTriangle(int size, String let){
String output = "";
String x = let;
int y = size;
for (int z = size; z > 0; z--){
int p = z;
output += "\n";
while (p > 0){
output = output + " ";
p--;
}
for (int d = size; d >= y; d--){
output = output + x;
}
y--;
}//End of for loop
out.println(output);
}
}
当在我的程序中执行此语句 TriangleFour.createTriangle(3, "R");
时,此代码会产生以下输出:
R
RR
RRR
我想修改我的代码,基本上像这样翻转输出:
RRR
RR
R
如有任何帮助,我们将不胜感激。
您只需修改打印条件 space[" "] 和字符
如下所示
public static void createTriangle(int size, String let) {
String output = "";
String x = let;
int y = size;
for (int z = size; z > 0; z--) {
int p = y-z;
output = output + "\n";
for(int j=0;j<=p;j++){
output=output+" ";
}
for (int d =z; d>0; d--) {
output = output + x;
}
}
System.out.println(output);
}
这是我的尝试。我精简了您的代码,并重命名了一些变量以使其更清晰。
public static void createTriangle(int size, String let){
for(int i=size;i>0;i--){
int fill = i;
int space = size-i;
while(space>0){
System.out.print(" ");
space--;
}
while(fill>0){
System.out.print(let);
fill--;
}
System.out.println();
}
}
第一个 for 循环几乎决定了将有多少行。如果 size
为 3,则将有 3 行。第一个 while 循环控制每行空白 space 的数量。 space
是要打印的空格数space,fill
是要打印的字母数。每次循环运行时,您都希望 space
+ fill
= size
。
所以在第一个循环中,space
是 0。你不打印任何 spaces。 fill
是 3,所以你打印 3 个字母,0 + 3 = 3.
RRR
第2次循环,space
为1,打印1space。 fill
为2,打印2个字母,1 + 2 = 3.
RRR
RR
第3次循环,space
为2,打印2spaces。 fill
为1,打印1个字母。 2+1=3
RRR
RR
R
希望这是有道理的。
我希望你只是想打印它。在提问之前尝试调试或在编码之前先写算法
public static void createTriangle(int size, String let) {
StringBuilder sb= new StringBuilder();
for(int i=0;i<3;i++){
sb.append(let);
System.out.println(sb.toString());
}
}
请尝试以下代码(我已经测试过了):
public class TriangleFour {
public static void main(String[] args) {
createTriangle(3, "R");
}
public static void createTriangle(int size, String let) {
String output = "";
String x = let;
int y = size;
for (int z = 0; z < size; z++) {
int p = z;
output = output + "\n";
while (p > 0) {
output = output + " ";
p--;
}
for (int d = 0; d < y; d++) {
output = output + x;
}
y--;
}
System.out.println(output);
}
}
输出:
RRR
RR
R
import static java.lang.System.*;
import java.util.Scanner;
public class TriangleFour{
public static void createTriangle(int size, String let){
String output = "";
String x = let;
int y = size;
for (int z = size; z > 0; z--){
int p = z;
output += "\n";
while (p > 0){
output = output + " ";
p--;
}
for (int d = size; d >= y; d--){
output = output + x;
}
y--;
}//End of for loop
out.println(output);
}
}
当在我的程序中执行此语句 TriangleFour.createTriangle(3, "R");
时,此代码会产生以下输出:
R
RR
RRR
我想修改我的代码,基本上像这样翻转输出:
RRR
RR
R
如有任何帮助,我们将不胜感激。
您只需修改打印条件 space[" "] 和字符 如下所示
public static void createTriangle(int size, String let) {
String output = "";
String x = let;
int y = size;
for (int z = size; z > 0; z--) {
int p = y-z;
output = output + "\n";
for(int j=0;j<=p;j++){
output=output+" ";
}
for (int d =z; d>0; d--) {
output = output + x;
}
}
System.out.println(output);
}
这是我的尝试。我精简了您的代码,并重命名了一些变量以使其更清晰。
public static void createTriangle(int size, String let){
for(int i=size;i>0;i--){
int fill = i;
int space = size-i;
while(space>0){
System.out.print(" ");
space--;
}
while(fill>0){
System.out.print(let);
fill--;
}
System.out.println();
}
}
第一个 for 循环几乎决定了将有多少行。如果 size
为 3,则将有 3 行。第一个 while 循环控制每行空白 space 的数量。 space
是要打印的空格数space,fill
是要打印的字母数。每次循环运行时,您都希望 space
+ fill
= size
。
所以在第一个循环中,space
是 0。你不打印任何 spaces。 fill
是 3,所以你打印 3 个字母,0 + 3 = 3.
RRR
第2次循环,space
为1,打印1space。 fill
为2,打印2个字母,1 + 2 = 3.
RRR
RR
第3次循环,space
为2,打印2spaces。 fill
为1,打印1个字母。 2+1=3
RRR
RR
R
希望这是有道理的。
我希望你只是想打印它。在提问之前尝试调试或在编码之前先写算法
public static void createTriangle(int size, String let) {
StringBuilder sb= new StringBuilder();
for(int i=0;i<3;i++){
sb.append(let);
System.out.println(sb.toString());
}
}
请尝试以下代码(我已经测试过了):
public class TriangleFour {
public static void main(String[] args) {
createTriangle(3, "R");
}
public static void createTriangle(int size, String let) {
String output = "";
String x = let;
int y = size;
for (int z = 0; z < size; z++) {
int p = z;
output = output + "\n";
while (p > 0) {
output = output + " ";
p--;
}
for (int d = 0; d < y; d++) {
output = output + x;
}
y--;
}
System.out.println(output);
}
}
输出:
RRR
RR
R