当前位置:网站首页 > Java教程 > 正文

java教程if语句



用for和if绘制一个图形

* * * * * * * * * * * * *

* @ *

* @ @ *

* @ @ *

* @ @ *

* @ @ *

* @ @ *

* @ @ *

* @ @ *

* @ @ *

* @ @ *

* @ *

* * * * * * * * * * * * *

import java.io.*; import java.util.Scanner; public class IfelseP { public void draw(int h) { for (int m = 0; m <= h + 1; m++) { for (int n = 0; n <= h + 1; n++) { if ((m == 0 && n >= 0) || (n == 0 && m > 0) || (m == h + 1 && h + 1 > n && n > 0) || (n == h + 1 && m > 0)) { System.out.print(" "); System.out.print("*"); } else if (n == (h + 3) / 2 - m || n == (h - 1) / 2 + m || n == m - (h - 1) / 2 || n == (3 * h + 1) / 2 - m) { System.out.print(" "); System.out.print("@"); } else { System.out.print(" "); System.out.print(" "); } } System.out.println(); } /* * for (int i=1;i<=h;i++){ //逐行打印 for (int j=1;j<=h;j++){ * //每行打印个数数行数保持一致 //下面是菱形四天便的函数,在边上的坐标点打印*,否则打印空格 if * (j==(h+3)/2-i||j==(h-1)/2+i||j==i-(h-1)/2||j==(3*h+1)/2-i){ * System.out.print("*"); }else { System.out.print(" "); } } * System.out.println(); } */ } public static void main(String[] args) { IfelseP p = new IfelseP(); System.out.println("请输入大于1的奇数:"); Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); p.draw(a); } }

版权声明


相关文章:

  • java区块链开发教程2025-01-13 13:58:05
  • Java Web开发实践教程2025-01-13 13:58:05
  • gradle教程 java2025-01-13 13:58:05
  • java com bridge 教程2025-01-13 13:58:05
  • mac安装java教程2025-01-13 13:58:05
  • java程序设计实用教程第四版2025-01-13 13:58:05
  • 427集java教程2025-01-13 13:58:05
  • java编程教程142025-01-13 13:58:05
  • 高级java2大学教程2025-01-13 13:58:05
  • 菜鸟教程java经典2025-01-13 13:58:05