基础班期间闲来无聊.根据所学知识,敲图案来玩玩,像下面这个就是个倒三角/
开始敲这个的时候还晕了,为什么如果拼图的字符是单个的就会不成型,感情还有对称性,蓝瘦.
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("输个数字.");
int x = sca.nextInt();
for (int k = 1; k <= x; k++) {
for (int i = 0; i <k; i++) {
System.out.print(" ");
}
for (int j =0; j <=x-k; j++) {
System.out.print("↑↑");
}System.out.println();
}
for (int q =1; q <=x ; q++) {
for (int w =0; w <=x-q; w++) {
System.out.print(" ");
}
for (int e = 0; e < q; e++) {
System.out.print("↓↓");
}
System.out.println();
}
}
不能因为无聊就乱敲图案是吧,总得来电有用的,下面这个就是验证码,
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 10; i++) {
System.out.print("随机验证码");
for (int j = 0; j < 6; j++) {
int w=r.nextInt(3)+1;
switch (w) {
case 1:
int x = r.nextInt(10);
System.out.print(x);
break;
case 2:
char y = (char)(r.nextInt(26) + 65);
System.out.print(y);
break;
case 3:
char z = (char) (r.nextInt(26) + 97);
System.out.print(z);
break;
}
}
System.out.println();
}
}
敲出这个后面总觉得不够好,可以优化.却不懂得哪里下手.
再来一个万年历的
public static void main(String[] args) {
System.out.println("请输入年:");
Scanner scanner = new Scanner(System.in);
int nYear = scanner.nextInt();
System.out.println("请输入月:");
int nMounth = scanner.nextInt();
int nflag = 0;
if (nYear % 4 == 0)
{
if (nYear % 100 == 0)
{
if (nYear % 400 == 0)
{
nflag = 1;
System.out.println(nYear + "年是润年");
}
else
{
System.out.println(nYear + "年不是润年");
}
}
else
{
nflag = 1;
System.out.println(nYear + "年不是润年");
}
}
else
{
System.out.println(nYear + "年不是润年");
}
}
|
|