=======================3种循环语句(for、while、do while) ======================= public static void main(String[] args) { int count = 0; for(int i = 0; i < 10; i++) { System.out.println("你好"); System.out.println(count++); } int x = 0; while(x < 10) { System.out.println("我好"); x++; System.out.println(count++); } int y = 0; do { System.out.println("他好"); y++; System.out.println(count++); }while(y < 10); int sum = 0; for(int xx = 0; xx < 5; xx++) { if( xx % 2 == 0) { System.out.println(xx); sum += xx; } } System.out.println("结果是:" + sum); } =======================3种方式调用方法======================= public staticvoid main(String[] args) { //单独调用:方法名(参数); print(); //打印调用:System.out.println(方法名称(参数)); System.out.println(getSum(5,6)); //赋值调用:数据类型 变量名 = 方法名(参数) String sum =getSum("5","6"); System.out.println(sum); } //单独调用 public static void print() { System.out.println("单独调用"); } //打印调用 public static int getSum(int a, int b) { return a + b; } //赋值调用 public static String getSum(String a,String b) { return a + b; } =======================3种方式比较3个数的最大值======================== public staticvoid main(String[] args) { int a = 10; int b = 20; int c = 30; //用第三方变量 int temp = a > b ? a : b; int max = temp > c ? temp : c; System.out.println(max); //用三元运算嵌套 int max2 = a > b ? (a > c ? a :c) : (b > c ? b : c); System.out.println(max2); //用for循环嵌套 int max3; if (a >= b && a >= c) { max3 = a; } else if (b >= c && b >=a) { max3 = b; } else { max3 = c; } System.out.println(max3); }
方法调用图解:
|