当前位置:编程学习 > JAVA >>

Java 用for循环打出菱形,空菱形,求解!

rt
图形大小最好由键盘输入,,

3q
补充:给点注释号码? 2楼那个if条件实在是太复杂,,(我理解来看...),能不能用三个for写?或者换种方法,,

3楼的貌似更简单,,但是也请注释一下 ,,, thx

加了十分
答案:
import java.util.Scanner;
public class Rhombus {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//可键盘输入图形大小
System.out.print("请输入菱形大小:");
int length = sc.nextInt();//以下是输出空心菱形
for(int i=0;i<2*length-1;i++){
for(int j=0;j<2*length-1;j++){
if(j==length-1-i||-j==length-1-i||i==j-(length-1)||2*length-2-i==j-(length-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();

}//下面是输出实心菱形
System.out.println("----------------");
for(int i=0;i<2*length-1;i++){
for(int j=0;j<2*length-1;j++){
if(j>=length-1-i&&-j<=length-1-i&&i>=j-(length-1)&&2*length-2-i>=j-(length-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();

}
}

}
public class Test {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5 - i; j > 0; j--)
System.out.print(' ');
for (int m = 1; m <= 2 * i - 1; m++)
{
if(m==1||m==2*i-1)
System.out.print('*');
else
System.out.print(' ');
}
System.out.print('\n');
}
for (int n = 4; n >= 1; n--) {
for (int t = 5 - n; t > 0; t--)
System.out.print(' ');
for (int k = 1; k <= 2 * n - 1; k++)
{
if(k==1||k==2*n-1)
System.out.print('*');
else
System.out.print(' ');
}
System.out.print('\n');
}

}

}
......上面都有了.......最好是看得懂思路吧
//这是实心的----------------长度就是那个hang-------键盘输入你应该会吧
int hang = 9;
int i, j, k = 1;
int hang1 = hang / 2 + 1;
for (i = 1; i != 0; i += k) {
if (i > hang / 2) {
k = -1;
}
for (j = 1; j <= (hang1 + i - 1); j++) {
if (j < (hang1 - i + 1)) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println("");
}
//这是空心的
int hang = 9;
int i, j, k = 1;
int hang1 = hang / 2 + 1;
for (i = 1; i != 0; i += k) {
if (i > hang / 2) {
k = -1;
}
for (j = 1; j <= (hang1 + i - 1); j++) {
//System.out.print(j);
if (j < (hang1 - i + 1)) {
System.out.print(" ");
} else {
if(j==hang1 - i + 1||j==hang1 + i - 1){
System.out.print("*");
continue;
}
else
System.out.print(" ");
}
}
System.out.println("");
}

上一个:JAVA中的多态与重载有什么区别?
下一个:要在电脑上装什么软件能运行JAVA程序?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,