Java 我的嵌套for循环编号模式没有';I don’我没有拿出正确的图案
我正试图打印出此图案,但我在排列数字和间距以达到预期输出时遇到问题。Java 我的嵌套for循环编号模式没有';I don’我没有拿出正确的图案,java,Java,我正试图打印出此图案,但我在排列数字和间距以达到预期输出时遇到问题。 Expected Output: 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 6 5 4 3 2 1 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 这是我的密码。我不知道如何将数字隔开,并按照输出指定的顺序输入正确的数字 import java.uti
Expected Output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
这是我的密码。我不知道如何将数字隔开,并按照输出指定的顺序输入正确的数字
import java.util.Scanner;
public class patterns{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
for (int i = 6; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= 6; j++)
{
System.out.print(j);
}
System.out.println();
}
System.out.println();
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= 6; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
下面是为您的模式修改的代码
public class Patterns {
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < (5 - i) * 2; j++) {
System.out.print(" ");
}
for (int j = i + 1; j >= 1; j--) {
System.out.print(j + " ");
}
System.out.println();
}
System.out.println();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < i * 2; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 6 - i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
更新:修复了空格的错误
工作=>考虑模式的前半部分
1 line 0: 10 spaces = (5 - 0) * 2 spaces
2 1 line 1: 8 spaces = (5 - 1) * 2 spaces
3 2 1 line 2: 6 spaces = (5 - 2) * 2 spaces
4 3 2 1 line 3: 4 spaces = (5 - 3) * 2 spaces
5 4 3 2 1 line 4: 2 spaces = (5 - 4) * 2 spaces
6 5 4 3 2 1 line 5: 0 spaces = (5 - 5) * 2 spaces
1 2 3 4 5 6 line 0: 0 spaces = 0 * 2 spaces
1 2 3 4 5 line 1: 2 spaces = 1 * 2 spaces
1 2 3 4 line 2: 4 spaces = 2 * 2 spaces
1 2 3 line 3: 6 spaces = 3 * 2 spaces
1 2 line 4: 8 spaces = 4 * 2 spaces
1 line 5: 10 spaces = 5 * 2 spaces
考虑模式的后半部分
1 line 0: 10 spaces = (5 - 0) * 2 spaces
2 1 line 1: 8 spaces = (5 - 1) * 2 spaces
3 2 1 line 2: 6 spaces = (5 - 2) * 2 spaces
4 3 2 1 line 3: 4 spaces = (5 - 3) * 2 spaces
5 4 3 2 1 line 4: 2 spaces = (5 - 4) * 2 spaces
6 5 4 3 2 1 line 5: 0 spaces = (5 - 5) * 2 spaces
1 2 3 4 5 6 line 0: 0 spaces = 0 * 2 spaces
1 2 3 4 5 line 1: 2 spaces = 1 * 2 spaces
1 2 3 4 line 2: 4 spaces = 2 * 2 spaces
1 2 3 line 3: 6 spaces = 3 * 2 spaces
1 2 line 4: 8 spaces = 4 * 2 spaces
1 line 5: 10 spaces = 5 * 2 spaces
您应该认为每个三角形块实际上都有6x6个方形单元。这意味着您需要2个嵌套循环,每个循环将迭代6次 然后,您将根据某些条件决定是否打印数字或空格
for (int i = 6; i >= 1; i--)
{
for (int j = 1; j <= 6; j++)
{
if (j < i)
{
System.out.print(" ");
} else
{
System.out.print(7 - j + " ");
}
}
System.out.println("");
}
System.out.println("");
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j <= 6; j++)
{
if (j < i)
{
System.out.print(" ");
} else
{
System.out.print(j-i+1 + " ");
}
}
System.out.println("");
}
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
用于(int i=6;i>=1;i--)
{
对于(int j=1;j一种方法是:
import java.util.Scanner;
public class pattern{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 6; i++)
{
String val = "";
for (int j = i; j >= 1; j--)
{
val += j+ " ";
}
System.out.printf("%12s", val);
System.out.println();
}
System.out.println();
for (int i = 6; i >= 1; i--)
{
String val = "";
int ii=1;
for (int j = i; j >= 1; j--)
{
val += ii + " ";
ii++;
}
System.out.printf("%12s", val);
System.out.println();
}
}
要记住的一点是,当您试图像现在这样格式化文本时,应该使用printf
,而不是添加打印行。最好的方法是使用数组->减少循环次数。干杯
int a = 654321;
String str = new Integer(a).toString();
for (int i = str.length() - 1; i >= 0 ; i--) {
String g = String.format("%6s", str.substring(i));
System.out.println(g.replaceAll(".(?=.)", "$0 "));
}
System.out.println();
str = new StringBuffer(str).reverse().toString();
for (int i = str.length() - 1; i >= 0 ; i--) {
String g = String.format("%6s", str.substring(0, i+1));
System.out.println(g.replaceAll(".(?=.)", "$0 "));
}
import java.util.Scanner;
public class patterns{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[][] arr = new int[6][6];
for(int i = 0; i<6; i++)
arr[i][5] = 1;
for(int i = 1; i<6; i++)
for (int j = 4; j>=5-i; j--)
{
arr[i][j] = arr[i-1][j+1] + 1;
}
for(int i = 0; i<6; i++)
{
for (int j = 0; j<6; j++)
{
if(arr[i][j] > 0)
System.out.print(arr[i][j] + " ");
else
System.out.print(" ");
}
System.out.println("");
}
System.out.println("");
int z = 0;
for(int i = 5; i>=0; i--)
{
for(int k = 0; k<z; k++)
System.out.print(" ");
for (int j = 5; j>=0; j--)
{
if(arr[i][j] > 0)
System.out.print(arr[i][j] + " ");
}
System.out.println("");
z++;
}
}
}
我相信您会发现我下面的解决方案很有帮助:)
import java.util.Scanner;
public class patterns{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[][] arr = new int[6][6];
for(int i = 0; i<6; i++)
arr[i][5] = 1;
for(int i = 1; i<6; i++)
for (int j = 4; j>=5-i; j--)
{
arr[i][j] = arr[i-1][j+1] + 1;
}
for(int i = 0; i<6; i++)
{
for (int j = 0; j<6; j++)
{
if(arr[i][j] > 0)
System.out.print(arr[i][j] + " ");
else
System.out.print(" ");
}
System.out.println("");
}
System.out.println("");
int z = 0;
for(int i = 5; i>=0; i--)
{
for(int k = 0; k<z; k++)
System.out.print(" ");
for (int j = 5; j>=0; j--)
{
if(arr[i][j] > 0)
System.out.print(arr[i][j] + " ");
}
System.out.println("");
z++;
}
}
}
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1