Java class essay (2), java class essay

Source: Internet
Author: User

Java class essay (2), java class essay

Loop

For (Declaration; cyclic condition; changes to cyclic variables) {} Sometimes none of the three are required

While (1) -- The endless loop is in the loop

You can use break to exit the current loop. continue ends this loop and continues the next one. return ends the current method.

Do... while ()

9-9 multiplication table

package Day2;public class hello_word {public static void main(String[] args) {for(int i=1;i<=9;i++){   for(int j=1;j<=i;j++)   {   /*   System.out.print(i);   System.out.print("*");   System.out.print(j);   System.out.print("=");   System.out.print(i*j);   System.out.print("\t");   */   System.out.print(j+"*"+i+"="+(i*j)+"\t");}   System.out.println();}}}
The teacher gave me a lecture about the program, Baidu gave me a code and tested it. The cpu usage is indeed 100%.

# Include <process. h> # include <windows. h> double pi = 3.14; void handwarmer (void * p) {while (1) {// non-thread synchronization. Pi * = pi; if (pi> 1024*1024*1024) pi = 3.14 ;}} int main () {SYSTEM_INFO siSysInfo; GetSystemInfo (& siSysInfo ); // The n-core 2n thread holds for (int I = 0; I <(int) siSysInfo. dwNumberOfProcessors * 2; ++ I) _ beginthread (handwarmer, 0, 0); system ("pause"); return 0 ;}
Program = Algorithm + Data Structure

Next we will talk about arrays.

Array, a set of data types of the same

Int [] arr = new int [10]; // array declaration

Array Initialization Method

Initialize int [] arr1 = new int [10] During Declaration; in this way, the automatic Initialization is 0 // arr1 [,];

Int [] arr2 = {1, 2, 3, 4, 5, 6 };

Int [] arr3;

Arr3 = new int [] {1, 2, 3, 4}; declare first, then assign a value

The array cannot be extended.

After the array is initialized, the number of data stored in the array is

The number is fixed and cannot be changed. ① Data in the array is queried by the array name [subscript]. Note: The subscript starts from 0.

② Modify the data in the array and assign a value to the array name subscript.

③ Obtain the length of an array using array name. lenth to obtain System. out. println (arr. lenth );

④ Array traversal for () loop for (int I = 0; I <arr3; I ++) System. out. println (arr3 [I]);

⑤ Array assignment api application process interface

System. arracopy (a, B, c, d, e)

A: source array

B: Start position of the original array

C: Target Array

D: Start position of the target array

E: Copy Length

Int [] src = {1, 2, 4, 5, 6, 7, 8 };

Int [] aim = new int [10];

⑥ Array output Arrays. Tostring (arrayName );

7. array subscript out-of-bounds exception

Array Index Out Of Buounds Exception

Ansychronized

Resize the arrays

Arrays. copyOf (arrName, lenth );

Sort the orders Array

Int [] unsorteArr = {5, 3, 1, 2, 4 };

System. out. print ()

Copyint[] src = {1,2,3,4,5,6,7,8};        int[] aim = new int[10];        System.out.println(Arrays.toString(src));        System.out.println(Arrays.toString(aim));System.arraycopy(src,3,aim,4,4);        System.out.println(Arrays.toString(src));        System.out.println(Arrays.toString(aim));
sortint[ ] a = { 5,3,1,2,4};        System.out.println(Arrays.toString(a));        Arrays.sort(a);        System.out.println(Arrays.toString(a));

The "Method" in java is similar to the called function in C language, but it cannot be called a function in JAVA.

The method is used to encapsulate a code block with special functions. In JAVA, the method can be called repeatedly.

Improves code reuse rate, greatly improves code efficiency, and facilitates maintenance.

1. modifier OF THE METHOD)

2. Return Value Type of the Method

3. Method Name: a pair of parentheses (parameter list) must be written after the camper method name is met. Note: Red is required by a method.

Throws exception list

{Method body}

Modifier publc static

Return Value Type int | char | void (only processing, no return value)

(I) method name ① camper ② known

(Ii) parameter list: for example, the parameter declared in the parameter list in the parameter method declaration in the form of sort (int a []) in C language. It has no specific value and only plays a placeholder role.

When calling the actual parameter method, the pass-in method has specific values. For specific parameters, note: the value of the real parameter cannot be modified in the Java method and can be blank.

(Iii) exception list (omitted)

(4) The content of the function in C is caused by curly braces in the method.

Package Day02; public class hello_word {public static void main (String [] args) {System. out. println (c ();} public static int c () {return (1 + 100) * 50; // method body }}
/** Package ddddd; import java. util. arrays; import java. util. random; import java. util. optional; public class asdfasdf {public static void main (String [] args) {System. out. println ("You have 10 chances to guess these five different uppercase letters"); int [] ans = new int [5]; int [] gauss = new int [5]; int count = 10; Random rdn = new Random (); int cnt = 0, j = 0; for (int I = 0; I ++) {int x = (int) rdn. nextInt (26); for (j = 0; j <cnt; j ++) {if (x = ans [j]) bre Ak;} if (j! = Cnt) {I --; continue;} else {ans [cnt ++] = x; System. out. print (char) (x + 65) + "");} if (cnt = 5) break;} while (true) {System. out. println ("You still have" + (count --) + ""); usually cin = new every (System. in); char [] str = new char [5]; String sstr = cin. nextLine (); for (int I = 0; I <5; I ++) {gauss [I] = (int) (sstr. charAt (I)-65);} int ans1 = 0, ans2 = 0; for (int I = 0; I <5; I ++) {if (ans [I] = gauss [I]) ans1 ++ ;}for (int I = 0; I <5; I ++) {for (j = 0; j <5; j ++) {if (ans [I] = gauss [j]) ans2 ++ ;}} System. out. println ("+ ans1 +" digit "); System. out. println ("Wrong location, but the letter is correct with" + ans2 + "digit"); if (ans1 = 5) {System. out. println ("Congratulations, you guessed it"); break;} if (count = 0) {System. out. println ("unfortunately the answer is"); for (int I = 0; I <5; I ++) System. out. print (char) ('A' + ans [I]); break ;}}}}






 


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.