标签 java 下的文章

For 循环各种*号

For 循环各种*号

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

    {
        int zz = 5;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < zz; j++) {
                System.out.print("*");
            }
            System.out.println();
            zz--;
        }
    }
    {
        int zz = 0;
        for (int i = 0; i < 5; i++) {
            for (int j = zz; j < 5; j++) {
                System.out.print("*");
            }
            System.out.println();
            zz++;
        }
    }
    {
        for (int i = 0; i < 5; i++) {
            for (int j = i; j < 5; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

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

    {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

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

{

        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j < i; j++) {
                System.out.print(" ");
            }
            for (int k = i; k <= 5; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }

For 循环,技巧

For 循环,技巧

for (int i = 0, j = 9; i < 9 && j>3; i++, j--) {
        System.out.println(i + "+" + j);
}  

Java对两个整数变量的值进行交换

Java对两个整数变量的值进行交换

int a = 1, b = 2;

方法一:

int c;
c = a; a = b; b = c;

方法二:

a = a + b;
b = a - b;
a = a - b;

方法三:一个数^同一个数两次结果还是这个数

a = a ^ b;
b = a ^ b;
a = a ^ b;