Double loops and functions (personal note seven)

double loop function

1 double-layer cycle

//Example prints the following graph
01 2 3456 7 89
01 2 3456 7 89
01 2 3456 7 89
0 1 2 3456 7 89
01 2 3456 7 89

for(int i=0;i<5;i++){
for(int j=0;j<10;j++){
System.out.print(j+"\t");
}
System.out.println(); //After printing a group, wrap the line
}

//Example prints the following graph
**********
**********
**********
**********
for(int i=0;i<4;i++){
for(int j=0;j<10;j++){
System.out.print("*");
}
System.out.println();
}

//Example prints the following graph
  ##########
  #########
  ########
  #######
  ######
  #####
  ####
  ###
  ##
  #
  
int count=10;
for (int i = 0; i <10; i++) {
for(int j=0;j<count;j++){
System.out.print("#");
}
count--;
System.out.println();
}

2 break 和 continuous

break is used to end a switch case statement or loop statement
//Determine whether all the numbers between 1=10000 that are divisible by 99 are more than 20
int count=0;
for (int i = 0; i <10000; i++) {
if(i%99==0){
count++;
if(count>20){
System.out.println("Report: more than 20");
break; // used to end the loop
}
}
}

if(count<20){
System.out.println("Report: no more than 20");
}

//Example This example demonstrates that break interrupts the loop closest to itself
for(int i=0;i<5;i++){
for(int j=0;j<10;j++){
System.out.print(j+" ");

if(j>6){
break; // interrupts the loop closest to you
}
}
System.out.println();

}


0 1 2 3 4 5 6 7 
0 1 2 3 4 5 6 7 
0 1 2 3 4 5 6 7 
0 1 2 3 4 5 6 7 
0 1 2 3 4 5 6 7 

//Use of example labels
AAA:for(int i=0;i<5;i++){
BBB:for(int j=0;j<10;j++){
System.out.print(j+" ");
if(j>6){
break AAA; // interrupt the loop closest to you
}
}
System.out.println();
}

The result is 0 1 2 3 4 5 6 7 and then there is no more, because the outer loop is also interrupted

//The use of example cuntinue 
for (int i = 0; i < 1000; i++) {
if(i%5 !=0){
continue; //end the current loop and continue to the next loop
}
System.out.println(i);
System.out.println("Hello, everyone from the annual class of Xiaodoubao");
}

 3 functions

a program that can run independently
 
Modifier return type function name (parameter type parameter name, parameter type parameter name, ....) {
function body 
return return value
}
 
public static void main(String [] args) {
...
return; // can be omitted
}
 
//Example, write a function to find the sum of two numbers  
class Test{
public static void main(String[] args) {
System.out.println("Haha");
System.out.println("Haha");
int a=888777; //This is the actual parameter to be passed to the function
int b =99000;
add(a,b); //Call the function and pass parameters
System.out.println("Haha");
System.out.println("Haha");
}

//This is a function that does addition
static void add(int a,int b) { //a , b are formal parameters of the function
System.out.println(a+b);
}
}

//Example computes addition, function with return value
public static void main(String[] args) {
System.out.println("Haha");
System.out.println("Haha");
int a=5000;
int b =3000;
int c =add(a,b);
System.out.println("The result of the calculation is "+c);
System.out.println("Haha");
System.out.println("Haha");
}

static int add(int a,int b) { //This function will have a return value of type int This method must return a result of type int
return a+b;
}

//The example writes a function that receives two parameters and a calculation symbol, can calculate according to the symbol, and returns the calculation result
class Test{
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("Please enter the first number:");
int a=scan.nextInt();
System.out.println("Please enter the second number");
int b=scan.nextInt();
System.out.println("Please enter the calculation symbol");
String c=scan.next(); //Receive a string from the keyboard
int result=jisuan(a,b,c.charAt(0));
System.out.println("Calculation result is: "+result);
}

static int jisuan(int a ,int b, char c){
int result=0;
switch(c){
case '+':
result=a+b;
break;
case '-':
result=a-b;
break;
case '*':
result=a*b;
break;
case '/':
result=a/b;
break;
default :
System.out.println("Input error: Error code: 99999");
result= -99999;
}
return result;
}
}

//Example laying the floor
  ##################
  ##################
  ##################
  ##################
  ##################
//method one  
class Test {
public static void main(String[] args) {
System.out.println("Quickly come to a worker to help me with some work");
System.out.println();

pudiban ();

System.out.println();
System.out.println("The job is done");
}

static void pudiban() {
for (int j = 0; j < 5; j++) {
for (int i = 0; i < 20; i++) {
System.out.print("#");
}
System.out.println();
}
}
}


    //Method 2 (with parameters)
    class Test {
public static void main(String[] args) {
System.out.println("Quickly come to a worker to help me with some work");
System.out.println();
pudiban(5,10,"@");
pudiban(10,8,"$");
System.out.println();
System.out.println("The job is done");
}

static void pudiban(int row,int cols,String style) {
for (int j = 0; j < row; j++) {
for (int i = 0; i < cols; i++) {
System.out.print(style);
}
System.out.println();
}
}
}


//Method three (with return value)
class Test {
public static void main(String[] args) {
System.out.println("Quickly come to a worker to help me with some work");
System.out.println();
pudiban(5,10,"@");
pudiban(10,8,"$");
float gongzi=pudiban(7,12,"%",2.99f);
System.out.println("Payable salary"+gongzi);
System.out.println();
System.out.println("The job is done");
}

  // This function does not return a value
static void pudiban(int row,int cols,String style) {
for (int j = 0; j < row; j++) {
for (int i = 0; i < cols; i++) {
System.out.print(style);
}
System.out.println();
}
}

//This function can calculate the salary and return
static float pudiban(int row,int cols,String style,float money) {
for (int j = 0; j < row; j++) {
for (int i = 0; i < cols; i++) {
System.out.print(style);
}
System.out.println();
}
float salary=row*cols*money;
return salary; 
}
}

4 Overloading of functions

In a class, there can exist functions with the same name, but the number of parameters of the function, or the types of the parameters are different, such functions can exist at the same time
It is called overloading of functions. Whether the function is overloaded or not, it does not look at the return value (it has nothing to do with the return value)
Which of the following functions void a (int a,char b, String c) is an overload
   void a(int x,char y,String z) //No, same as the original function
   int a (int a, String b, char c) // is overloaded
   void a (int a, String b, String c) // is overloaded
   int a (int a,String b) //是
   int a () // yes
   boolean a (int a, char b, String name) //No, because whether the function is overloaded or not has nothing to do with the return value

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325560900&siteId=291194637