JAVA-day02-array of cyclic Functions

Source: Internet
Author: User

JAVA-day02-array of cyclic Functions

Class Demo1 {public static void main (String [] args) {// calculates the sum of 1--10 and int sum = 0; for (int I = 1; I <= 10; I ++) {sum = sum + I;} System. out. println ("sum =" + sum);/* sum = sum + 1; sum = sum + 2; sum = sum + 3; * //} class Demo2 {public static void main (String [] args) {// count the number of 7 divisible values between 1-, int count = 0; for (int I = 1; I <= 100; I ++) {if (I % 7 = 0) count ++;} System. out. println ("count =" + count) ;}} class Demo3 {public static void main (Stri Ng [] args) {// 3000 m long rope, halved every day. How many days will the rope be less than 5 meters? Decimal places are not considered. Int day = 0; for (int I = 3000; I> = 5; I/= 2) {day ++;} System. out. println ("day =" + day) ;}} import java. util. imports; import java. util. random; class Demo4 {public static void main (String [] args) {// generates a Random number between 1 and 10. You can guess this number, until you guess about it, then SC = new pipeline (System. in); // generate Random number random Random = new random (); int r = Random. nextInt (10) + 1; int guess; do {System. out. println ("guess the number between 1 and 10"); guess = SC. nextInt (); if (r> guess) System. out. println ("too small"); if (r
 
  
= 1; I --) {jieCheng = jieCheng * I;} System. out. println ("jieCheng =" + jieCheng); System. out. println ("Continue? 1/0 )? "); Flag = SC. nextInt () ;}while (flag = 1) ;}} import java. util. iterator; class Demo6 {public static void main (String [] args) {// calculate the number of integers and 1234 10 percentile SC = new percentile (System. in); System. out. println ("enter a number"); int num = SC. nextInt (); int sum = 0; while (num! = 0) {sum = sum + num % 10; num = num/10;} System. out. println ("sum =" + sum);/* sum = sum + num % 10; num = num/10; sum = sum + num % 10; num = num/10; sum = sum + num % 10; num = num/10; sum = sum + num % 10; num = num/10; */} class Demo7 {public static void main (String [] args) {/********************************/for (int I = 1; I <= 3; I ++) {for (int I = 1; I <= 10; I ++) System. out. print ("*");}}} /*************** 112123123412345 9 multiplication table 1*1 = 11*2 = 2 2*2 = 41*3 = 3 2*3 = 6 3*3 = 9 */class Demo8 {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 ();}/* 112123123412345 */for (int I = 1; I <= 5; I ++) {for (int j = 1; j <= I; j ++) System. out. print (j); System. out. println ();} /* 9 multiplication table 1*1 = 11*2 = 2 2*2 = 41*3 = 3 2*3 = 6 3*3 = 9 */for (int I = 1; I <= 9; I ++) {for (int j = 1; j <= I; j ++) System. out. print (j + "*" + I + "=" + j * I + ""); System. out. println ();}}} /* print a solid diamond pattern ***********************/class Demo9 {public static void main (String [] args) {for (int I = 1; I <= 4; I ++) {for (int j = 1; j <= 4-i; j ++) System. out. print (""); for (int j = 1; j <= 2 * I-1; j ++) System. out. print ("*"); System. out. println () ;}for (int I = 1; I <= 3; I ++) {for (int j = 1; j <= I; j ++) System. out. print (""); for (int j = 1; j <= 7-2 * I; j ++) System. out. print ("*"); System. out. println () ;}}/ * 3. print a hollow diamond pattern problem: print the following hollow diamond pattern: * ***********/class Demo10 {public static void main (String [] args) {for (int I = 1; I <= 4; I ++) {for (int j = 1; j <= 4-i; j ++) System. out. print (""); for (int j = 1; j <= 2 * I-1; j ++) {if (j = 1 | j = 2 * i-1) system. out. print ("*"); else System. out. print ("");} System. out. println () ;}for (int I = 1; I <= 3; I ++) {for (int j = 1; j <= I; j ++) System. out. print (""); for (int j = 1; j <= 7-2 * I; j ++) {if (j = 1 | j = 7-2 * I) System. out. print ("*"); else System. out. print ("");} System. out. println () ;}}/ * break: End loop continue: end a loop */class Demo11 {public static void main (String [] args) {for (int I = 1; I <= 5; I ++) {if (I = 3) break; System. out. println (I) ;}for (int I = 1; I <= 3; I ++) {break; System. out. println (I); // inaccessible statement} for (int I = 1; I <= 4; I ++) {for (int j = 1; j <= 3; j ++) {if (j = 2) break; // The end is the internal loop} m: for (int I = 1; I <= 4; I ++) {n: for (int j = 1; j <= 3; j ++) {if (j = 2) break m; // The end is an External Loop }}for (int I = 1; I <= 10; I ++) {if (I % 2 = 0) continue; // end this loop and directly enter the next loop, that is, to execute I ++ System. out. println (I) ;}} Function import java. util. classes; class Demo12 {public static void main (String [] args) {// write a program and receive a maximum of 10 numbers. Evaluate the sum of these numbers, // The user can terminate the program by entering 999, and display the number of input and limit SC = new limit (System. in); int sum = 0, num; for (int I = 1; I <= 10; I ++) {System. out. println ("enter a number"); num = SC. nextInt (); if (num = 999) break; sum = sum + num;} System. out. println ("sum =" + sum) ;}} import java. util. sums; class Demo13 {// defines a function with the summation function public static void add () {int num1, num2, sum = 0; sums SC = new sums (System. in); System. out. println ("Enter the first number"); num1 = SC. nextInt (); System. out. println ("enter the second number"); num2 = SC. nextInt (); sum = num1 + num2; System. out. println ("sum =" + sum);} public static void main (String [] args) {// calculate the sum of two numbers. enter int num1 on the keyboard, num2, sum = 0; Serial SC = new round (System. in); System. out. println ("Enter the first number"); num1 = SC. nextInt (); System. out. println ("enter the second number"); num2 = SC. nextInt (); sum = num1 + num2; System. out. println ("sum =" + sum); for (int I = 1; I <= 9; I ++) {for (int j = 1; j <= I; j ++) System. out. print (j + "*" + I + "=" + j * I + ""); System. out. println ();} System. out. println ("Enter the first number"); num1 = SC. nextInt (); System. out. println ("enter the second number"); num2 = SC. nextInt (); sum = num1 + num2; System. out. println ("sum =" + sum); System. out. println ("********************"); System. out. println ("Enter the first number"); num1 = SC. nextInt (); System. out. println ("enter the second number"); num2 = SC. nextInt (); sum = num1 + num2; System. out. println ("sum =" + sum) ;}/ * from the above procedure, we can see that every time we use the sum function, we have to write the sum code again and there are repeated codes, that is, code with the same function needs to be written multiple times to solve this problem using the function */import java. util. summary; class Demo14 {// defines a function with the summation function // function: a separate code segment with a specific function, when will this function be used? When will this function be called? public static void add (int a, int B) // form parameter, which is used to receive the {int sum = a + B; system. out. println ("sum =" + sum);} // The main function is called by JVM to public static void main (String [] args) {// generally, the data used in the program is provided by the main function. // The udfs can be int num1 and num2 as long as they are implemented; using SC = new using (System. in); System. out. println ("Enter the first number"); num1 = SC. nextInt (); System. out. println ("enter the second number"); num2 = SC. nextInt (); add (num1, num2); // actual parameter // add (1, 2) ;}} import java. util. extends; class Demo15 {public static int add (int a, int B) // The caller {int sum = a + B; return sum; // returns it to the caller, return to the location where the function is called. int c = 4;} // The main function is called by JVM. void indicates that no return value is public static void main (String [] args) // caller {int num1, num2; Consumer SC = new consumer (System. in); System. out. println ("Enter the first number"); num1 = SC. nextInt (); System. out. println ("enter the second number"); num2 = SC. nextInt (); // how does the caller obtain the result value of the called function int sum = add (num1, num2); // receives if (sum> 200) System. out. println ("this number is greater than 200");} import java. util. extends; class Demo16 {public static int add (int a, int B) {int sum = a + B; return sum;} public static void main (String [] args) {int num1, num2; Serial SC = new serial (System. in); System. out. println ("Enter the first number"); num1 = SC. nextInt (); System. out. println ("enter the second number"); num2 = SC. nextInt (); int sum = add (num1, num2); if (sum> 200) System. out. println ("this number is greater than 200 ");}}
 
 

Class Demo18 {public static void main (String [] args) {fun1 (12) ;}// if a function does not return a value, you can also use return, it indicates the position of the function call. The function call ends with public static void fun1 (int num) {if (num> 10) return; elseSystem. out. println (num);} // If a function returns a value, the public static int fun2 (int a, int B) must be returned in all circumstances) {if (a> B) return 1; else if (
 
  
B? A: B;} // print the rectangle // Result: No result // parameter: the length and width of the rectangle are uncertain data. public static void ju (int row, int col) {for (int I = 1; I <= row; I ++) {for (int j = 1; j <= col; j ++) System. out. print ("*"); System. out. println () ;}/// print 99 multiplication table // Result: No result // parameter: public static void jiuJiu () {for (int I = 1; I <= 9; I ++) {for (int j = 1; j <= I; j ++) System. out. print (j + "*" + I + "=" + j * I + ""); System. out. println () ;}}// overload: in the same class, the function name is the same but the parameters are different./* the parameter is different: number of parameters different types different numbers different types */class Demo20 {public static void main (String [] args) {add );} public static int add (int a, int B) {return a + B;} public static int add (int a, int B, int c) {return a + B + c;} public static double add (double a, double B) {return a + B;} public static void jiuJiu () {jiuJiu (9 );} // print 99 copies of the multiplication table. The number of rows is determined by the parameter public static void jiuJiu (int row) {for (int I = 1; I <= row; I ++) {for (int j = 1; j <= I; j ++) System. out. print (j + "*" + I + "=" + j * I + ""); System. out. println () ;}} class Demo21 {public static void main (String [] args) {}}/* public static void show (int a, boolean B, double d) a: public static int show (int a, boolean B, double d) // It is not an overload. The overload is irrelevant to the return value. B: public static int show (int v, boolean n, double m) // not overload C: public static int show (int a) // is d: public static void show (int a, double B, boolean d) // e: public static void show (int a, boolean B) // Yes * // array: it is a data type and can store multiple data, the referenced data type // The length of the array class Demo22 {public static void main (String [] args) must be specified when defining the array. {// data type variable name = initialization value; int a; // defines an array that can store 5 Data // new int [5]; // to use an array, you need to name the array int [] arr = new int [5]; // 4*5 = 20 // The array is stored or retrieved by subscript, the subscript starts from 0. // char '\ u0000' System. out. println (arr [0]);/* When a java program is running, jvm divides the memory into five stacks, heap, method zone, local method zone, and register stack: local variables are stored. After the range of variables defined in functions and statements ends, they will immediately disappear from the stack: Objects (arrays and objects) are stored ), the entity can store multiple data features: 1: memory first address will be allocated 2: Default Value 3: Garbage collection mechanism */}}
 




// Differences between referenced data types and basic data types class Demo23 {public static void main (String [] args) {/* int a = 12, B = 21; System. out. println ("a =" + a + ", B =" + B); huan (a, B); System. out. println ("a =" + a + ", B =" + B); */int [] arr = new int [2]; arr [0] = 12; arr [1] = 21; System. out. println ("arr [0] =" + arr [0] + ", arr [1] =" + arr [1]); huan (arr); System. out. println ("arr [0] =" + arr [0] + ", arr [1] =" + arr [1]);} public static void huan (int [] arr) {int c; c = arr [0]; arr [0] = arr [1]; arr [1] = c ;} // exchange the values of two variables // Result: No result // parameter: two numbers public static void huan (int a, int B) {int c; c =; a = B; B = c ;}}





<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + cjxwpda-vcd4kphbyzsbjbgfzcz0 = "brush: java;"> class Demo24 {public static void main (String [] args) {String ss = "haha "; char [] arr = new char [3]; arr [0] = 'n'; fun (ss, arr); System. out. println (ss + "" + arr [0]);} public static void fun (String str, char [] ch) {str = "hehe "; ch [0] = 'M' ;}// array: It can be stored continuously for multiple numbers, the same array can only store data of the same type class Demo25 {public static void main (String [] args) {int [] arr = new int [3]; // System. out. println (arr [3]); // ArrayIndexOutOfBoundsException // arr = null; // System. out. println (arr [0]); // NullPointerException // array initialization // a = 45; // int [] a = new int }; int [] B = {23,4, 56,1, 567}; // array traversal for (int I = 0; I <5; I ++) System. out. print (B [I] + ""); for (int I = 0; I




// Calculate the maximum value of the array class Demo26 {public static void main (String [] args) {int [] arr = {56,123 }; int max = max (arr); System. out. println ("max =" + max);} // calculate the maximum value in the array // result: maximum value // parameter: array public static int max (int [] arr) {int max = arr [0]; for (int I = 1; iarr [j + 1]) {huan (arr, j, j + 1 );}}}} // select sort // Result: No result // parameter: array public static void select (int [] arr) {for (int I = 0; I



// Search: the position where a number appears for the first time in the array.-1 class Demo28 {public static void main (String [] args) {int [] arr = {23,4, 9,123, 89,9}; int key = 99; int index = find (arr, key); System. out. println (index);} // search // result: the following value or-1 // parameter: array, and the number of searched public static int find (int [] arr, int key) {for (int I = 0; iarr [mid]) min = mid + 1; if (keymax) return-1; mid = (min + max) /2;} return mid;} // Binary Search // result: the bottom mark value or-1 // parameter: array, and the number of searched public static int halfSearch (in T [] arr, int key) {int min = 0, max = arr. length-1, mid; while (min <= max) {mid = (min + max)> 1; if (key> arr [mid]) min = mid + 1; else if (key "[34,12, 65]" import java. text. decimalFormat; import java. util. extends; public class Test {public static void main (String [] args) {int [] arr = {32, 65, 12, 89, 41}; System. out. print ("\" ["); for (int I = 0; I <arr. length; I ++) {System. out. print (arr [I]); if (I! = Arr. length-1) System. out. print (",");} System. out. println ("] \" ") ;}} 3. for an ordered array, if you want to insert an element and ensure that the array is still ordered, ask how to get the element position. (Using the bipartite method) import java. text. decimalFormat; import java. util. extends; public class Test {public static void main (String [] args) {int [] arr = {12, 23, 34, 45, 56, 67, 78 }; int key = 90; System. out. println (halfSearch (arr, key);} public static int halfSearch (int [] arr, int key) {int min = 0, max = arr. length-1, mid; while (min <= max) {mid = (min + max)> 1; if (key> arr [mid]) min = mid + 1; else if (key <Rr [mid]) max = mid-1; elsereturn mid;} return min ;}use functions to implement the following: 1. Compile a program to test whether a given year is a leap year? (The year that can be divisible by 4 but cannot be divisible by 100, or 400 is a leap year). 2. A positive integer is decomposed into a prime factor. For example, enter 90 and print 90 = 2*3*3*5. Import java. util. role; public class Test {public static void main (String [] args) {role in = new role (System. in); int I = in. nextInt (); test (I);} public static void test (int I) {for (int j = 2; j <= I; j ++) {if (I % j = 0) {System. out. print (j); test (I/j); j = I ;}}} 3. calculate the prime number? (The number that can only be divisible by 1 and itself is a prime number.) tip: Let this number be divided by the previous number from 2 to this number. If neither can be divisible, it indicates that it is a prime number imported java. text. decimalFormat; import java. util. secret; public class Test {public static void main (String [] args) {for (int I = 101; I <200; I ++) {boolean flag = false; for (int j = 2; j <I; j ++) {if (I % j = 0) {flag = true ;}} if (! Flag) {System. out. println (I) ;}}} 4. there are four numbers: 1, 2, 3, and 4, which can form three numbers that are different from each other and have no duplicates. Import java. util. extends; public class Test {public static void main (String [] args) {for (int I = 1; I <= 4; I ++) {for (int j = 1; j <= 4; j ++) {for (int k = 1; k <= 4; k ++) {if (! (I = j | I = k | j = k) {System. out. print (I + "" + j + "" + k); System. out. println () ;}} System. out. println ();} System. out. println () ;}} 6. enter three integers x, y, and z. Set the three integers from small to large. import java. util. role; public class Test {public static void main (String [] args) {role in = new role (System. in); int max, min; int x = in. nextInt (); int y = in. nextInt (); int z = in. nextInt (); min = x
  
   
Y? X: y; max = max> z? Max: z; int [] arr = new int [3]; arr [0] = min; arr [1] = x + y + z-max-min; arr [2] = max; for (int I = 0; I
   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.