import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;
public class Huiwen {//java回文数显示!!
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int max=0;
System.out.println("请输入最大数:");
max=sc.nextInt();
Vector
int x=1;
for(;x<=max;x++)
{
String string="";
for(int y=1;y<=2*x-1;y++)
{
if(y<=x)
string+=String.valueOf(y);
else
string+=String.valueOf(2*x-y);
}
stringarray.add(string);
}
Iterator
while(it.hasNext())
System.out.println(it.next());
}
}
其实你可以试下api里面的方法
public class MM {
public static void main(String[] args) {
int num = 20;
for(int i = 2; i < num; i++){
StringBuilder sb = new StringBuilder();
for(int j = 1; j < i; j++)
sb.append(j);
System.out.println(sb.toString()+sb.reverse().toString().substring(1));
}
}
}
数字够大的话运行效率要好一点
运行结果和楼上一样
public class CCC {
public static void main(String[] args) {
final int Num=5;
for(int i=1;i<=Num;i++){
for(int j=1;j<=Num-i;j++){
System.out.print(" ");
}
for(int j=1;j<=i;j++){
System.out.print(j);
}
for(int k=i-1;k>=1;k--){
System.out.print(k);
}
System.out.println();
}
}
}
运行结果:
1
121
12321
1234321
123454321