JAVA METHOD
发布网友
发布时间:2022-04-30 04:22
我来回答
共2个回答
热心网友
时间:2023-04-28 21:19
根据楼上的修改后的代码 符合你的要求 你试试
/**
* 功能:演示菱形打印 显示于控制台 思路:递归原理
*
* @author tenglian
*/
public class Rhombus {
static int y = 0;
public static void main(String[] args) {
try {
System.out.print(" How long do you want each side to be?");
BufferedReader bf = new BufferedReader(new InputStreamReader(
System.in));
String str = bf.readLine();
int shu = Integer.parseInt(str);
System.out
.print(" Please enter the character you want it to be made of:");
String str1 = bf.readLine();
Rhombus t = new Rhombus();
t.output(shu, str1);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 打印菱形
*/
public void output(int shu, String str1) {
int i, j;
for (i = 0; i < shu; i++) {
for (j = 0; j < shu; j++) {
if (j <= i) {
if (y == 0) {
System.out.print(str1);
} else {
System.out.print(" ");
}
} else {
if (y == 0) {
System.out.print(" ");
} else {
System.out.print(str1);
}
}
}
System.out.println();
}
y++;
if (y < 2 && i == shu) {
i = 0;
j = 0;
output(shu, str1);
} else {
return;
}
}
}
热心网友
时间:2023-04-28 21:19
直接运行看看,自己跟踪下代码 不知道是否符合你需要的
/**
* 功能:演示菱形打印 显示于控制台
* 思路:递归原理
* @author tenglian
*/
public class Test{
static int y = 0;
public static void main(String []args){
Test t = new Test();
t.output(0,0);
}
/**
* 打印菱形
*/
public void output(int i,int j){
for(i = 0; i < 10; i ++){
for(j = 0; j < 10; j++){
if(j <= i){
if(y == 0){
System.out.print("*");
} else {
System.out.print(" ");
}
} else {
if(y == 0){
System.out.print(" ");
} else {
System.out.print("*");
}
}
}
System.out.println();
}
y++;
if(y < 2 && i == 10){
i = 0;
j = 0;
output(i,j);
} else {
return;
}
}
}