/*这是一个用“*”打印任意菱形的程序 但是通过rows=System.in.read() 来接受输入的时候 我是用rows=rows-48;结果只能接受1-9的数字 请高手指点 还有什么好的方法阿 摆脱阿
*/
import java.io.*;
public class J_square
{
static int rows;
static int half;
static int sum;
J_square() throws Exception
{
System.out.print("请输入您要输出的菱形的行数:");
rows=InputStream.read();
rows=rows-48;
if(rows%2==0)
{
System.out.println("请输入奇数!");
rows=0;
System.exit(-1);
}
half=rows/2;
sum=rows*2-2;
} public static void main(String args[]) throws Exception
{
J_square f=new J_square();
int i=0,j,k;
System.out.println(f.rows);
for(;i<f.rows;i++)
{
if(i<=f.half)
{
for(j=0;j<f.half-i;j++)
{
System.out.print(" ");
}
for(k=0;k<2*i;k++)
{
System.out.print("*");
if(i==f.half&&k==f.half) System.out.print("\b+");
}
System.out.println("*");
} else
{
for(j=f.half;j<i;j++) System.out.print(" ");
for(k=0;k<f.sum-2*i;k++) System.out.print("*");
System.out.println("*");
}
}
}
}
*/
import java.io.*;
public class J_square
{
static int rows;
static int half;
static int sum;
J_square() throws Exception
{
System.out.print("请输入您要输出的菱形的行数:");
rows=InputStream.read();
rows=rows-48;
if(rows%2==0)
{
System.out.println("请输入奇数!");
rows=0;
System.exit(-1);
}
half=rows/2;
sum=rows*2-2;
} public static void main(String args[]) throws Exception
{
J_square f=new J_square();
int i=0,j,k;
System.out.println(f.rows);
for(;i<f.rows;i++)
{
if(i<=f.half)
{
for(j=0;j<f.half-i;j++)
{
System.out.print(" ");
}
for(k=0;k<2*i;k++)
{
System.out.print("*");
if(i==f.half&&k==f.half) System.out.print("\b+");
}
System.out.println("*");
} else
{
for(j=f.half;j<i;j++) System.out.print(" ");
for(k=0;k<f.sum-2*i;k++) System.out.print("*");
System.out.println("*");
}
}
}
}
String line = in.readLine();
int i = Integer.parseInt(line);
可以这样写吗?
但要知道怎么摆啊!