4-java Learning-Loop Statement, Control Jump Statement, Method-Case Code

4-java Learning-Loop Statement, Control Jump Statement, Method-Case Code

1. for Loop Statement

1. Use the for loop statement to output "Pluto fish" 10 times in the console
public class MyTest { public static void main(String[] args) { // 1. int i=0; the starting value of the loop executes only once//2. i<10 Whether the loop continues or not conditional i<10 is true loop execution is false loop end//3. {System.out.println("I love you");} {}The code in braces is the loop body, and the code that needs to be executed repeatedly is put in the loop body//4. i++ statements that control the loop increase I by 1 for each loop (int).I=0; i<10; i++) {System.out.println ("Pluto");}} 
2. Use for loop statements to output data 1 - 10, 10 - 1 in the console
public class MyTest2 { public static void main(String[] args) { //Please output data 1 - 10 for (int i=1; i<=10; i+){System.out.println(i);} System.out.println ("-----------------------------------"); //Please output data 10 - 1 for (int i=10; i>=1; i--) {System.out.println(i);}} 
3. Use for loop statements to sum data between 1 and 10
public class MyTest3 { public static void main(String[] args) { int sum = 0; //Define a variable to receive the result for (int I = 1; I <= 10; i+) {sum = sum + i;} System.out.println ("and yes" + sum);}} 
4. Use for loop statements to find the sum of odd and even numbers between 1 and 100
public class MyTest4 { public static void main(String[] args) { int sum=0; //Define a variable to receive the final result int ou=0; int ji=0; for (int i=1; i<=100; i+){sum=sum+i; if (i%2==0) {//is even ou=ou+i;} else{ji=ji+i;} System.out.println ("sum"+sum); System.out.println("odd sum"); System.out.println ("even sum";}+ou} 
5. Use the for loop statement to output all the "daffodils" in the console (daffodils are three digits whose cube is equal to the number itself.)
public class MyTest { public static void main(String[] args) { //Define a statistical variable int count=0; for (int i=100; i<=100; i<=999; i++) {int ge=i<=999; i++) {int ge=i/1%10; int shi=i/10%10; int bai=i/100%10; int flowerNum=ge*ge+shi*shi*shi+bai*bai; if (i==flowerNum) ==flowerNum) {System.out.println {System println System.out.println} ("There are"+count++ countcountcounts"+countcountcountcountcountcounts"+""+ countcountcountcountcounts"""+ countcountcountcountcountcounts""""""");}} 
6. Output a star (*) pattern in 4 rows and 5 columns with a for loop statement
public class MyTest { public static void main(String[] args) { /* Figure: ***** ***** ***** ***** ***** */ for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { System.out.print("*"); //Print No Line Break} System.out.println(); //Just for Line Break} //Nested Features of Two-Layer Loop: 1. Number of Rows Controlled by Outer Loop 2. Number of Columns Controlled by Inner Loop} 
7. Output triangle shape with for loop statement
public class MyTest2 { public static void main(String[] args) { /* Requirements: Please output the following shapes * ** *** ** ******/ for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } } } 
8. Print the 99 multiplication table with 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 Escape character blank one space table bit} System.out.print("\n"); // \n Escape character \n can also wrap}} 

2. while Loop Statement

1. Use while loop statements to output data 1 - 10, 10 - 1 in the console
public class MyTest3 { public static void main(String[] args) { //While //output 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. Use the while loop statement to find the sum of odd and even numbers between 1 and 100
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("Odd sum"+ji); System.out.println("Even Sum" + ji); } } 
3. Use the while loop statement to output all the "daffodils" in the console (daffodils are three digits whose cube is equal to the number 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("The number of daffodils is" + i);
                count++;
            }
            i++;
        }
        System.out.println("The number of daffodils is" + count + "individual");
    }
}

3. do...while loop statement

1. Use do...The while loop statement outputs "Pluto fish" 10 times in the console
public class MyTest { public static void main(String[] args) { //Do {Loop Body} while // Execute the code inside the do once, and then judge if the condition is valid. If it is valid, continue the cycle. If it is not valid, the loop ends at int i = 1; do {System.out.println ("Pluto Fish"); i++;} while("i < 10); //You know that the number of cycles is generally for //You don't know the number of cycles to select the loop, but you need to know the conclusion of the cycleBeam condition}} 

4. Control Jump Statements

1. break: Used in switch or loop statements, it makes no sense to leave this scene

Use to end a switch statement, use a loop statement to end a loop

public class MyTest { public static void main(String[] args) { for (int i = 0; i <10; i++) { System.out.println("Fish in the Pluto"); if(i==4){ break; //End this loop statement}} System.out.println ("-----------------------------------------"); int j=1; while (true) {if(j>10){break;} System.out.println ("11"); j+;} System.out.println ("22"); and}} Run the result: 
2. continue: Used in a loop statement to jump out of a loop, continue with the next loop in a loop scenario, leaving a loop scenario makes no sense
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); } } } Run result: 1 2 3 4 6 7 8 9 10 ------------------------ 2 4 6 8 10 
3. Where breaks are added to loops, then what loops end is what. If there are multiple loops nested, we can mark loops, and then we can end a loop by marking it
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; //Which level of loop to end by tag} System.out.print ("*");} System.out.println ();}}} 
4. Output 2 times from console: "Pluto 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("Fish in the Pluto"); } } } 
5. Output 7 times from console: "Pluto 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("Fish in the Pluto"); } } } 
6. Output 13 times from console: "Pluto fish"
public class MyTest4 { public static void main(String[] args) { for (int x = 1; x <= 10; x++) { if (x % 3 == 0) { } System.out.println("Fish in the Pluto"); continue; } } } 

5. Methods

1. Method: Encapsulate a piece of function code to achieve multiple calls and improve code reuse

Syntax for method definition:
1. Method is defined in class
2. Method and method are horizontal and cannot be defined nestedly
3. Syntax of method: permission modifier status modifier returns value type method name (parameter type parameter name) {method body}
4. Method call does not execute

public class MyTest { //The main method is the entry to the program.Public static void main (String[] args) {System.out.println ("main method called"); //main method is called by JVM, we can call our own defined method//execute sendBullet (); sendBullet (); sendBullet (); sendBullet (); sendBullet (); sendBullet (); sendBullet ();} public void ();} sendBullet public voidEndBullet(){//function to launch bullets System.out.println("launch bullets");}} 
2. When invoking a method with parameters, the parameters corresponding to the type and number of parameters must be passed in one by one. Then the parameters passed in are called actual parameters, short for actual parameters. If there is a return value for this method, we can receive it. What type does the method return, and what type do you use to receive it?
public class MyTest2 { public static void main(String[] args) { //Call our own method add(); int result= add2(); System.out.println (result); System.out.println (result); System.out.println (result); System.out.println ("==================================="//call parameterized method int sum2=add3 (5,9); system.out.println (sum2); int sum3=50,50); System.out.println (sum3), System.out.out.println (system.out.50), system.println (========================"); double sum4= add3 (2,3.4,20); System.out.println (sum4); retUrn;} // parameterless, no return value //void means public static void add () {int num1=10; int num2=20; int sum=num1+num2; System.out.println(sum); return; //end method, void then return can omit write} //no reference return value once a method explicitly returns value type.The return keyword must bring back a result public static int add2(){int num1 = 10; int num2 = 20; int sum = num1 + num2; return sum; //return end method and return the result} //public static int add3 (int a, int b) {int sum = a + b; return sum;} //parameter defined in method parentheses is called formParameter, short for formal parameter Multiple parameters You separate public static double add3 (double a, double b, int c) with commas {double sum=a+b+c; return sum;} public static void add4 (double a, double b, int c) {double sum = a+b+c; return;} 
3. Call a method to maximize two numbers

There are two things to be clear when defining a method:

1. Method does not require parameters and the type and number of parameters.

2. Do you want the method to return a value?

public class MyTest3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter the first integer"); //alt+Enter Universal Magic key int a = scanner.nextInt()); System.out.println ("Please enter the second integer"); System.out.println ("Please enter second integer"); int b = scanner.nextInt(); int max= getMax(a,b); System.out.println("Maximum is"+max);} public static int getMax (int num1, int num1, int num2) {int max=0; if (num1>=num2) {num2) {max = system.out.println} = scanner.nextInt {scanner.nextInt(); int max;} return max;}} 
4. Call a method to determine if two numbers are equal
public class MyTest4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter the first integer"); int a = scanner.nextInt(); System.out.println("Please enter the second integer"); int b = scanner.nextInt(); //Return value Boolean Boolean equals = isEquals (a, b); if (equals) {System.out.println ("equal");} else {System.out.println ("unequal");}} public static Boolean isEquals (int a, int b) {// Boolean flag=a==b? True:false; return a==b;}} 
5. Call a method to sum two numbers
public class MyTest5 { public static void main(String[] args) { //By returning a value, this method can output calls to System.out.println (add (30,60);} public static int add (int a, int b) {return a+b;}} 
6. Call a method to print star, number of rows and columns for user input
public class MyTest6 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter the number of lines"); int hs = scanner.nextInt(); System.out.println("Please enter the number of columns"); int ls = scanner.nextInt(); //Define method: Parameter parameter type Several parameter return values return type//Call method 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 overload: Allow multiple methods with the same name in a class, as long as they have different number or types of parameters, be careful not to distinguish between return value types
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("Two int Called"); return a + b; } public static double add(int a, double b) { System.out.println("One int Called, one double Called"); 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; } } 
Published 8 original articles. Praise 0. Visits 133
Private letter follow

Tags: Java jvm

Posted on Mon, 10 Feb 2020 22:57:09 -0500 by ericbangug