4-java learning - loop control jump statements, methods - Case Code

4-java learning - loop control jump statements, methods - Case Code

A, for loop

1. for loop output in the console 10 "North deep fish"
public class MyTest { public static void main(String[] args) { // 1. int i=0; 循环的起始值 只执行一次 // 2. i<10 循环是否继续执行的条件 i<10 是true 循环执行 是 false 循环结束 // 3. {System.out.println("我爱你们");} {} 大括号里面的代码就是循环体,需要重复执行的代码,就放到循环体里面 //4. i++ 控制循环的语句 每循环一次 i自增1 for(int i=0;i<10;i++){ System.out.println("北冥有鱼"); } } } 
2. Data for loop console output 1-- 10,10 --1
public class MyTest2 { public static void main(String[] args) { //请在控制台输出数据1 - 10 for(int i=1;i<=10;i++){ System.out.println(i); } System.out.println("-----------------------------"); //请在控制台输出数据10 - 1 for(int i=10;i>=1;i--){ System.out.println(i); } } } 
3. used for obtaining a loop - between 10 and the sum data
public class MyTest3 { public static void main(String[] args) { int sum = 0; //定义一个变量,用来接收结果 for (int i = 1; i <= 10; i++) { sum = sum + i; } System.out.println("和是" + sum); } } 
4. seeking odd and even numbered between 1-100 and a for loop and
public class MyTest4 { public static void main(String[] args) { int sum=0; //定义一个变量来接收最后的结果 int ou=0; int ji=0; for (int i=1;i<=100;i++){ sum=sum+i; if(i%2==0){//是偶数 ou=ou+i; }else{ ji=ji+i; } } System.out.println("和是"+sum); System.out.println("奇数和是" + ji); System.out.println("偶数和是" + ou); } } 
The output of all of the "Narcissus number" (refer to a three-digit number daffodils, which is equal to the digits of the number and the cube itself.) With the console for loop
public class MyTest { public static void main(String[] args) { //定义一个统计变量 int count=0; for(int i=100;i<=999;i++){ int ge=i/1%10; int shi=i/10%10; int bai=i/100%10; int flowerNum=ge*ge*ge+shi*shi*shi+bai*bai*bai; if(i==flowerNum){ System.out.println("水仙花数是"+i); count++; } } System.out.println("水仙花数有"+count+"个"); } } 
6. The output of a star with four rows and five columns for loop (*) pattern
public class MyTest { public static void main(String[] args) { /* 如图: ***** ***** ***** ***** */ for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { System.out.print("*"); //打印不换行 } System.out.println();//只是为了换行 } //两层循环的嵌套特点:1.外层循环控制的是行数 2.内层循环控制列数 } } 
7. The triangular shape of the output for loop
public class MyTest2 { public static void main(String[] args) { /* 需求:请输出下列的形状 * ** *** **** ******/ for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } } } 
8. Print multiplication table for loop statement
public class MyTest3 { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int j = 1; j <=i; j++) { System.out.print(j+"*"+i+"="+(j * i)+"\t"); // \t 转义字符 空一格 table位 } System.out.print("\n"); // \n 转义字符 \n 也可换行 } } } 

Two, while loop

1. while loop console output data 1-- 10,10 --1
public class MyTest3 { public static void main(String[] args) { //while //输出1---10 int i = 1; while (i <= 10) { System.out.println(i); i++; } System.out.println("-------------------"); int j = 10; while (j >= 1) { System.out.println(j); j--; } } } 
2. The while loop statement is evaluated and the odd and even 1 to 100 and
public class MyTest4 { public static void main(String[] args) { int ou = 0; int ji = 0; int i = 1; while (i <= 100) { if (i % 2 == 1) { ji += i; } else { ou += i; } i++; } System.out.println("奇数和"+ji); System.out.println("偶数和" + ji); } } 
3. All the while loop output "Narcissus number" in the console (refer to a three-digit number daffodils, which is equal to the digits of the number and the cube itself.)
public class MyTest5 {
    public static void main(String[] args) {
        int i=100;
        int count=0;
        while (i<1000){
            int ge = i / 1 % 10;
            int shi = i / 10 % 10;
            int bai = i / 100 % 10;
            int flowerNum = ge * ge * ge + shi * shi * shi + bai * bai * bai;
            if (i == flowerNum) {
                System.out.println("水仙花数是" + i);
                count++;
            }
            i++;
        }
        System.out.println("水仙花数有" + count + "个");
    }
}

Three, do ... while loop

1. do ... while loop output 10 times in the console "North deep fish"
public class MyTest { public static void main(String[] args) { //do{循环体} while(条件) //先执行do里面的代码一次,然后去判断条件是否成立,如果成立就继续循环,如果不成立,循环结束 int i = 1; do { System.out.println("北冥有鱼"); i++; } while (i < 10); // 你知道循环次数一般选for // 你不知道循环次数选while循环,但是你要知道循环的结束条件 } } 

Fourth, control jumps statement

1. break: a switch statement or loop statement, leave the scene does not make sense

By using the switch statement to the end of the switch statement is used in the loop end of the loop

public class MyTest { public static void main(String[] args) { for (int i = 0; i <10; i++) { System.out.println("北冥有鱼"); if(i==4){ break; //结束这个循环语句 } } System.out.println("-------------------------------"); int j=1; while (true){ if(j>10){ break; } System.out.println("11"); j++; } System.out.println("22"); } } 运行结果: 
2. continue: with a statement in the loop, to jump out of a cycle, the cycle continues with the next cycle in the scene, the scene does not make sense to leave the cycle
public class MyTest2 { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if(i==5){ continue; } System.out.println(i); } System.out.println("------------------------"); for (int i = 1; i <= 10; i++) { if (i%2==1) { continue; } System.out.println(i); } } } 运行结果: 1 2 3 4 6 7 8 9 10 ------------------------ 2 4 6 8 10 
3. break the cycle layer which is applied, then the end of the cycle is the layer which, if there are multiple layers of nested cycle, we can add a tag to loop, then you can go to the end of a cycle according to standard
public class MyTest3 { public static void main(String[] args) { wc:for (int i = 0; i < 5; i++) { nc:for (int j = 0; j < 5; j++) { if(j==2){ break wc; //根据标记去结束哪层循环 } System.out.print("*"); } System.out.println(); } } } 
4. Output twice in the console: "North deep fish"
public class MyTest4 { public static void main(String[] args) { for (int x = 1; x <= 10; x++) { if (x % 3 == 0) { break; } System.out.println("北冥有鱼"); } } } 
In the console output seven times: "North deep fish"
public class MyTest4 { public static void main(String[] args) { for (int x = 1; x <= 10; x++) { if (x % 3 == 0) { continue; } System.out.println("北冥有鱼"); } } } 
6. Output 13 times in the console: "North deep fish"
public class MyTest4 { public static void main(String[] args) { for (int x = 1; x <= 10; x++) { if (x % 3 == 0) { } System.out.println("北冥有鱼"); continue; } } } 

V. Methods

1. Method: The function code section of the package to achieve multiple calls to improve reusability of code

The method defined syntax:
1. The method defined in class
2. The method with the method same level relationship is not nested definitions
3. The method of syntax: Permission Status Modifier Modifier return type method name (parameter type parameter name) { } method body
4. The method does not call does

public class MyTest { //主方法 是程序的入口。JVM来调用的 public static void main(String[] args) { System.out.println("主方法调用了"); //main方法由JVM 调用,我们可以在main方法中,调用我们自己定义的方法 //通过方法名 调用方法执行 sendBullet(); sendBullet(); sendBullet(); sendBullet(); sendBullet(); sendBullet(); sendBullet(); } public static void sendBullet(){ //发射子弹的功能 System.out.println("发射子弹"); } } 
2. When a call that has parameters, the parameter must correspond to the type and number eleven incoming parameter type and corresponding parameters, then the parameters passed, we called the actual parameter, referred to as arguments, this method returns if there is value, we can receive the return value, the method returns what type, you can use what type of income
public class MyTest2 { public static void main(String[] args) { //调用我们自己的方法 add(); int result= add2(); System.out.println(result); System.out.println("==================="); //调用有参数的方法 int sum2=add3(5,9); System.out.println(sum2); int sum3=add3(50,50); System.out.println(sum3); System.out.println("===================="); double sum4= add3(2,3.4,20); System.out.println(sum4); return; } //无参数,无返回值 //void 表示无明确类型的返回值 public static void add(){ int num1=10; int num2=20; int sum=num1+num2; System.out.println(sum); return; //结束方法,方法上是void 那么return可以省略不写 } //无参有返回值 一旦一个方法明确返回值类型。就必须由return 关键字 带回一个与返回值类型一致的结果 public static int add2(){ int num1 = 10; int num2 = 20; int sum = num1 + num2; return sum; //return 结束方法,并返回结果 } //有参有返回值的方法 public static int add3(int a,int b){ int sum=a+b; return sum; } //方法括号中定义的参数,叫做形式参数,简称形参 多个形参你用逗号隔开 public static double add3(double a, double b,int c) { double sum=a+b+c; return sum; } public static void add4(double a, double b, int c) { double sum = a + b + c; return; } } 
3. calling a method, the maximum average of two numbers

When two things have clearly defined methods:

1. To do parameters and the type and number of parameters,

2. To not return a value.

public class MyTest3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入第一个整数"); //alt+Enter 万能神键 int a = scanner.nextInt(); System.out.println("请输入第二个整数"); int b = scanner.nextInt(); int max= getMax(a,b); System.out.println("最大值是"+max); } public static int getMax(int num1,int num2){ int max=0; if(num1>=num2){ max=num1; }else{ max=num2; } return max; } } 
4. call a method, it is determined whether the two numbers are equal
public class MyTest4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入第一个整数"); int a = scanner.nextInt(); System.out.println("请输入第二个整数"); int b = scanner.nextInt(); //返回值 boolean boolean equals = isEquals(a, b); if(equals){ System.out.println("相等"); }else{ System.out.println("不相等"); } } public static boolean isEquals(int a,int b){ // boolean flag=a==b?true:false; return a==b; } } 
5. call a method, and the number of the sum of two
public class MyTest5 { public static void main(String[] args) { //这个方法由返回值,就可以输出调用 System.out.println(add(30,60)); } public static int add(int a,int b){ return a+b; } } 
6. call a method, star print, the number of rows and columns allows users to input
public class MyTest6 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入行数"); int hs = scanner.nextInt(); System.out.println("请输入列数"); int ls = scanner.nextInt(); //定义方法:参数 参数类型 几个参数 返回值 返回类型 //调用方法 showStar(hs,ls); } public static void showStar(int a,int b){ for (int i = 1; i <= a; i++) { for (int j = 1; j <= b; j++) { System.out.print("*"); } System.out.println(); } } } 
7. Method overloading: allowing a class, there are multiple methods of the same name, as long as they have different number of parameters, or parameter types can, take care not to distinguish Return Type
public class MyTest { public static void main(String[] args) { double sum1 = add(1, 2.0); int sum2 = add(1,2, 3); int sum3 = add(1, 2, 3, 4); System.out.println(sum1); System.out.println(sum2); System.out.println(sum3); } public static int add(int a, int b) { System.out.println("两个int调用了"); return a + b; } public static double add(int a, double b) { System.out.println("一个int调用了,一个double调用了"); return a + b; } public static int add(int a, int b,int c) { return a + b+c; } public static int add(int a, int b, int c,int d) { return a + b + c+d; } } 
Released nine original articles · won praise 0 · Views 162

Guess you like

Origin blog.csdn.net/weixin_42401546/article/details/104260425