鱼C论坛

 找回密码
 立即注册
查看: 818|回复: 2

[学习笔记] JAVA练习:2.编写程序输出字符

[复制链接]
发表于 2023-2-22 21:59:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 划句顾 于 2023-2-22 21:59 编辑

1.编写程序输出字符
                *
                *  *
                *  *  *
                *  *  *  *
                *  *  *  *  *


代码如下:
  1. public class StarDemo {
  2. public static void main(String args[]) {
  3.          int i,j;
  4.          for(i = 0;i < 5;i++) {
  5.                  for(j = 0;j < i+1;j++) {
  6.                          System.out.print("*");
  7.                  }
  8.                  System.out.println();
  9.          }
  10. }
  11. }
复制代码

输出结果:
  1. *
  2. **
  3. ***
  4. ****
  5. *****
复制代码
1677071252444.jpg


                               
登录/注册后可看大图

2.更改第一题的形状
                *  *  *  *  *
                *  *  *  *
                *  *  *
                *  *
                *


代码如下:
  1. public class StarDemo {
  2. public static void main(String args[]) {
  3.          int i,j;
  4.          for(i = 0;i < 5;i++) {
  5.                  for(j = 0;j < 5-i;j++) {
  6.                          System.out.print("*");
  7.                  }
  8.                  System.out.println();
  9.          }
  10. }
  11. }
复制代码

运行结果:
第二题的图.jpg

                               
登录/注册后可看大图

3.继续更改第一题的形状
                *  *  *  *  *
                    *  *  *  *
                       *  *  *
                           *  *
                              *


代码如下:
  1. public class StarDemo {
  2. public static void main(String args[]) {
  3.          int i,j;
  4.          for(i = 0;i < 5;i++) {
  5.                  for(j = 0;j <5;j++) {
  6.                          if(i>j) {
  7.                                  System.out.print(" ");
  8.                          }
  9.                          else
  10.                          {
  11.                                  System.out.print("*");
  12.                          }
  13.                         }
  14.                  System.out.println();
  15.          }
  16. }
  17. }
复制代码

输出那两行代码可以多打几个空格,结果输出的时候,会比较好看,如图二
可以写成这样:

  1. public class StarDemo {
  2. public static void main(String args[]) {
  3.          int i,j;
  4.          for(i = 0;i < 5;i++) {
  5.                  for(j = 0;j <5;j++) {
  6.                          if(i>j) {
  7.                                  System.out.print("   ");
  8.                          }
  9.                          else
  10.                          {
  11.                                  System.out.print("*  ");
  12.                          }
  13.                         }
  14.                  System.out.println();
  15.          }
  16. }
  17. }
复制代码

运行结果:
图一:

图一

图一

图二:

图二

图二



                               
登录/注册后可看大图

4.更改第一题的形状
                            *
                        *  *
                    *  *  *
                *  *  *  *
            *  *  *  *  *


代码如下:
  1. public class StarDemo {
  2.          public static void main(String args[]) {
  3.                  int i,j;
  4.                  for(i = 0;i < 5;i++) {
  5.                          for(j = 0;j <5;j++) {
  6.                                  if(j>=4-i) {
  7.                                          System.out.print("*  ");
  8.                                  }
  9.                                  else
  10.                                  {
  11.                                          System.out.print("   ");
  12.                                  }
  13.                                 }
  14.                          System.out.println();
  15.                  }
  16.          }
  17. }
复制代码

运行结果:
第四题.jpg

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-22 22:22:13 | 显示全部楼层
鱼币
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-2-23 20:46:57 From FishC Mobile | 显示全部楼层
sfqxx 发表于 2023-2-22 22:22
鱼币

Σ()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-22 23:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表