Algorithm 有人能告诉我这个方法的工作原理吗:倒半金字塔模式
给定模式:Algorithm 有人能告诉我这个方法的工作原理吗:倒半金字塔模式,algorithm,Algorithm,给定模式: G H I J D E F B C A 我想知道为什么我使用这个方法,为什么我这样写,我想从一开始就知道 这是我使用的代码。我想解释一切,以便我清楚地理解它 此外,如果您能给我一个简单的代码,我将不胜感激。请给出除此之外的相同输出:)谢谢 public class Main { public static void main(String[] args) { int x = 0;
G H I J
D E F
B C
A
我想知道为什么我使用这个方法,为什么我这样写,我想从一开始就知道
这是我使用的代码。我想解释一切,以便我清楚地理解它
此外,如果您能给我一个简单的代码,我将不胜感激。请给出除此之外的相同输出:)谢谢
public class Main
{
public static void main(String[] args)
{
int x = 0;
int alphabet = 71;
for (int i = 3; i>= 0; i--)
{
for (int j = 0; j <= i; j++)
{
System.out.print((char) alphabet + " ");
alphabet++;
x=j;
}
alphabet=alphabet-(x+1+i);
System.out.println();
}
}
}
公共类主
{
公共静态void main(字符串[]args)
{
int x=0;
int字母=71;
对于(int i=3;i>=0;i--)
{
对于(int j=0;j,Java中的字符可以转换为整数。您可以通过从Google搜索“字符整型值Java”来查找更多信息
下面的示例演示了字符、整数值和其他版本的代码
public class Main {
public static void main(String[] args) {
char[] alphabets = new char[] {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'
};
int A = 65; // Integer value of character 'A'
// Print characters as integers
for (int i = 0; i < alphabets.length; ++i) {
System.out.print((int) alphabets[i] + " ");
}
System.out.println();
// Print integers as characters
for (int i = 0; i < alphabets.length; ++i) {
System.out.print((char) (A + i) + " ");
}
System.out.println();
System.out.println();
// Array index of first character (G) is 6
int first = 6;
for (int width = 4; width > 0; --width) { // Four rows
// Print characters of current row
for (int x = 0; x < width; ++x) {
char current = alphabets[first + x];
System.out.print(current + " ");
}
System.out.println();
// Calculate first character of next row
first -= width - 1;
}
}
}
公共类主{
公共静态void main(字符串[]args){
字符[]字母表=新字符[]{
“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”、“I”、“J”
};
int A=65;//字符“A”的整数值
//将字符打印为整数
对于(int i=0;i0;--width){//4行
//打印当前行的字符
对于(int x=0;x
你的问题与设计模式无关。请改用算法。也许没有人比你自己更适合回答你为什么那样编写代码的问题。顺便说一句,我不是自己写的