java编程,输入n,输出2*n+1的菱形
2个回答
展开全部
编程,重点是你要观察规律
import java.util.Scanner;
public class Diamond {
public static void main(String[] args) {
//2*n+1
System.out.print("Please input a number:");
int n = new Scanner(System.in).nextInt();
//print first part
for(int space = n; space >=0; space--){
print(" ", space);
print("*", 1+2*(n-space));
System.out.println();
}
for(int space = 1; space <= n; space++){
print(" ", space);
print("*", 1+2*(n-space));
System.out.println();
}
}
private static void print(String str, int count) {
for(int i = 0; i < count; i++){
System.out.print(str);
}
}
}
---------------------------测试结果
Please input a number:3
*
***
*****
*******
*****
***
*
import java.util.Scanner;
public class Diamond {
public static void main(String[] args) {
//2*n+1
System.out.print("Please input a number:");
int n = new Scanner(System.in).nextInt();
//print first part
for(int space = n; space >=0; space--){
print(" ", space);
print("*", 1+2*(n-space));
System.out.println();
}
for(int space = 1; space <= n; space++){
print(" ", space);
print("*", 1+2*(n-space));
System.out.println();
}
}
private static void print(String str, int count) {
for(int i = 0; i < count; i++){
System.out.print(str);
}
}
}
---------------------------测试结果
Please input a number:3
*
***
*****
*******
*****
***
*
展开全部
public static void LingXing(int number) {
if (number < 0){
System.out.println("input is error!");
return;
}
for(int a = 1 ; a <= number ; a++) {
for(int b = number - a ; b > 0 ; b--) {
String m = " ";
System.out.print(m);
}
for(int c = 1 ; c <= a; c++) {
String n = " *";
System.out.print(n);
}
System.out.println(" ");
}
for(int a = 1 ; a < number ; a++) {
for (int b = 0 ; b < a; b++) {
String m = " " ;
System.out.print(m);
}
for (int c =number-a ; c>0 ; c--) {
String n = " *";
System.out.print(n);
}
System.out.println(" ");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询