Java用户输入,打印菱形,要求,判断输入是否为整数,提示输入奇数
2个回答
展开全部
这个不知为何要求要行数,行数应该是计算出来的呀,您看看我这个,只要求输入奇数
public class LingXing {
public static void main(String[] args) {
int num = 0;
while(num%2==0) {
System.out.println("请输入奇数:");
Scanner s = new Scanner(System.in);
String str = s.next();
if (str.matches("^[0-9]*$")) {
num = Integer.parseInt(str);
}
}
for (int i = 1; i<=num;i=i+2){
int spaceNum = (num-i)>>1;
print(spaceNum," ", false);
print(i,"*", true);
}
for (int i = num-2; i>=1;i=i-2){
int spaceNum = (num-i)>>1;
print(spaceNum," ", false);
print(i,"*", true);
}
}
public static void print(int count, String info, boolean endBreak){
while(count-->0){
System.out.print(info);
}
if (endBreak)
System.out.println();
}
}
展开全部
public static void main(String[] args) {
try {
print2();
} catch (Exception e) {
// TODO: handle exception
System.out.println("输入错误,请重试");
print2();
}
}
private static void print2() {
Scanner sc=new Scanner(System.in);
System.out.print("请输入菱形行数:");
int i = sc.nextInt();
while(i%2!=0){
System.out.print("请输入偶数:");
Scanner sc2=new Scanner(System.in);
i=sc2.nextInt();
}
for(int j=1;j<=i;j++)//控制行数
{
for(int k=i-1;k>=j;k--)//打印空格
{
System.out.print(" ");
}
for(int m=1;m<=2*j-1;m++)//打印*
{
System.out.print("*");
}
System.out.println();
}
for(int j = 1; j < i; j++)
{
for(int x = 0; x < j; x++)
{
System.out.print(" ");
}
for(int y = j; y < 2 * i - j - 1; y++)
{
System.out.print("*");
}
System.out.println();
}
}
随便写了下,你看看
try {
print2();
} catch (Exception e) {
// TODO: handle exception
System.out.println("输入错误,请重试");
print2();
}
}
private static void print2() {
Scanner sc=new Scanner(System.in);
System.out.print("请输入菱形行数:");
int i = sc.nextInt();
while(i%2!=0){
System.out.print("请输入偶数:");
Scanner sc2=new Scanner(System.in);
i=sc2.nextInt();
}
for(int j=1;j<=i;j++)//控制行数
{
for(int k=i-1;k>=j;k--)//打印空格
{
System.out.print(" ");
}
for(int m=1;m<=2*j-1;m++)//打印*
{
System.out.print("*");
}
System.out.println();
}
for(int j = 1; j < i; j++)
{
for(int x = 0; x < j; x++)
{
System.out.print(" ");
}
for(int y = j; y < 2 * i - j - 1; y++)
{
System.out.print("*");
}
System.out.println();
}
}
随便写了下,你看看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询