如何用java编程出这个X图案?求高手详细打出来吧!还有一个由*组成的菱形图案。
1个回答
展开全部
public class test {
public static void main(String[] args) {
int n = 7;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(j == i || j == n-1-i){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
n为宽度
菱形图案
public class test {
public static void main(String[] args) {
int n = 7;
int k = 0;
boolean falg = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == n / 2 + k || j == n / 2 - k) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
if (k == -3) {
falg = true;
}
if (falg) {
k++;
} else {
k--;
}
System.out.println();
}
}
}
public static void main(String[] args) {
int n = 7;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(j == i || j == n-1-i){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
n为宽度
菱形图案
public class test {
public static void main(String[] args) {
int n = 7;
int k = 0;
boolean falg = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == n / 2 + k || j == n / 2 - k) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
if (k == -3) {
falg = true;
}
if (falg) {
k++;
} else {
k--;
}
System.out.println();
}
}
}
更多追问追答
追问
嗯嗯,非常感谢,那那个菱形怎么弄?
追答
菱形哈!
public class test {
public static void main(String[] args) {
int n = 7;
int k = 0;
boolean falg = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == n / 2 + k || j == n / 2 - k) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
if (k == -n/2) {
falg = true;
}
if (falg) {
k++;
} else {
k--;
}
System.out.println();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询