- Download, install, and use the JDK Help documentation
Download the document is very simple, just Baidu to find the Google Translate version (English version can also) the JDK help document is OK, after the download is a. chm file on your computer can open directly
The specific use of the method, is also relatively simple, find the current CHM reader search function, in the class needs to find the class name can be easily found, generally we can see a lot of content, such as this scanner class
The introduction and examples are very detailed, it can be said that the help document is very important learning materials.
- Basic data types and expressions
Topics known: a=21, b=5, c=5.0, d=5.2 , the values of the following expressions are evaluated programmatically:
A/b a%b A/C a%d
The code is as follows
Packagepractise1; Public classdata1 { Public Static voidMain (string[] args) {//observe that all the data found are not integers, in order to save the whole double. DoubleA = +, B = 5, c = 5.0, d = 5.0; //Direct Output Calculation resultsSystem.out.println (A/b); System.out.println (A%b); System.out.println (A/c); System.out.println (A%d); }}/*The output results are as follows 4.21.04.21.0*/
Topic 1:
To find the maximum value of two numbers, the experimental steps are as follows
(1) define two variables a and bof int type;
(2) enter the values of variables a and b through the keyboard;
(3) using the if-else statement to compare two integers, find the maximum value, and output the result;
(4) use the conditional operator (?:) to compare two integers, find the maximum value, and output the result
Packagepractise1;ImportJava.util.Scanner; Public classMax_num { Public Static voidMain (string[] args) {intA, B; Scanner SC=NewScanner (system.in); System.out.println ("Please enter two integers separated by a space"); A=Sc.nextint (); b=Sc.nextint (); Sc.close ();
//use If-else to compare and output the following if(A >b) {System.out.println ("The biggest number is" +a); } Else{System.out.println ("The biggest number is" +a); } //The following is a comparison operation using the second methodSystem.out.println ((a > B?)a:b)); }}/*output Please enter two integers separated by a space 45 55 the largest number is 4555*/
Topic 2:
Determines whether a positive integer entered by the user is divisible by 9 (n<1000)until the user enters 1 . Practice looping statements, break, and continue statements.
Experimental steps:
(1) cyclic reading into positive integer n;
(2) if n=1, abort the execution of the cycle, otherwise continue;
(3) if n<0 or n>1000, the error is reported and returned to (1) Re- read into n , or continue;
(4) determine Whether n can be divisible by 9 and output The judging result
(5) continue the next cycle
Code
Packagepractise1;ImportJava.util.Scanner; Public classCanDisabedBy9 { Public Static voidMain (string[] args) {intN; Scanner SC=NewScanner (system.in); Do{System.out.println ("Please enter a value greater than 0 less than 100 to determine whether it is a multiple of 9"); N=Sc.nextint (); if(n<0 | n>1000) {System.out.println ("Error The number you entered is not within the specified range, please try again"); Continue;//the method here is to start the next loop directly when the error is entered to try again } //The input detected is 1 jump out of the loop directly if(n = = 1) { Break; } //after the previous exclusion, 9 of the divide is judged if(n%9 = = 0) {System.out.println ("The number you entered is a multiple of 9."); } Else{System.out.println ("Obviously the number you entered is not a multiple of 9."); } } while(N! = 1); Sc.close (); System.out.println ("Judgment is over."); }}/*Run the result please enter a greater than 0 less than 100 to determine whether it is a multiple of 9 8 it is obvious that the number you entered is not a multiple of 9 please enter a value greater than 0 less than 100 to determine whether it is a multiple of 9 5 It is obvious that the number you entered is not a multiple of 9 please enter a number greater than 0 less than 100 is a multiple of 9 please enter a greater than 0 less than 100 to determine whether it is a multiple of 9 9999 error The number you entered is not within the specified range, please retry please enter a greater than 0 less than 100 to determine whether it is a multiple of 9 1 judgment has ended*/
Topic 3:
Defines a method that calculates the area of a square (assuming that the value of the square side length is a positive integer).
Experimental purpose
(1) Know how to define the method.
(2) Learn how to invoke methods.
(3) Mastering the value-based transfer relationship of formal parameters and arguments.
(4) Learn how to return a value in a method.
Experimental steps
(1) Definition method area of the square is calculated;
(2) in the main method, read into the square side length;
(3) The method area is called in the main method ;
(4) The value of the square area is output in the main method.
The code is as follows
Packagepractise1;ImportJava.util.Scanner; Public classarea1 {//defines a method for calculating the area to invoke in the Main method Public Static intSqrare (intwidth) { intArea = width*width; returnArea ; } //The main method calls the area calculation method and outputs the result Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.println ("Please enter the side length of the square"); inti =Sc.nextint (); Sc.close (); System.out.println ("Area is" +Area1.sqrare (i)); }/*Sample Output Please enter the side length of the square 666 area is 443556*/
Topic 4:
Use recursive method to find n! (n≥0).
Experimental purpose
(1) Understand the definition and use of recursive methods.
(2) familiar with recursive "recursion" and "regression" process.
(3) Mastering the value-based transfer relationship of formal parameters and arguments.
Experimental steps
(1) Define the recursive method FAC, calculate the factorial of the integer n;
(2) The value of the integer n is read into the main method;
(3) The FAC method is called in the main method to find the factorial of the integer n ;
(4) Outputs the result of the calculation in the main method.
Code
Packagepractise1;ImportJava.util.Scanner; Public classDg_jiecheng {//define our recursive factorial method first Public Static intJiecheng (intN) {if(n<0) {System.out.println ("I'm sorry, there's no factorial in negative numbers."); return0; } if(n==0) { return1; } if(n>2) {n= N*dg_jiecheng.jiecheng (n-1); } returnN; } Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.println ("Please enter a positive integer to calculate his factorial."); System.out.println ("The factorial of the number is" +Dg_jiecheng.jiecheng (Sc.nextint ())); Sc.close (); }}/*Output Example enter a positive integer to calculate his factorial 4 the factorial of the number is 24 please enter a positive integer to calculate his factorial-4 I'm sorry, there's no factorial. The factorial of the number is 0.*/
Topic 5:
The ordering of two integers or two floating-point numbers is implemented by overloaded methods, and the sorted results are output in order from small to large.
Experimental purpose
(1) Master the definition and invocation of overloaded methods.
(2) to be familiar with the matching and binding process of method-form participation in real parameters.
Experimental steps
(1) Define the overloaded method sort, order two integers, and the method contains two int parameters for passing the integer to be sorted;
(2) Define another overloaded method sort, order two floating-point numbers, and the method contains two double -type parameters for passing the floating-point number to be sorted;
(3) define two int variables and initialize them in the main method;
(4) define two double type variables in the main method and initialize them;
(5) in the main method, depending on the type and number of parameters, different methods are called to implement overloading.
Code
Packagepractise1; Public classChongzai {//since there's no order, then we're going to choose the order of the small size. Public Static voidSortintAintb) {if(a>b) {System.out.println ("(" +a+ "," +b+ ")"); } Else{System.out.println ("(" +b+ "," +a+ ")"); } } Public Static voidSortDoubleADoubleb) {if(a>b) {System.out.println ("(" +a+ "," +b+ ")"); } Else{System.out.println ("(" +b+ "," +a+ ")"); } } Public Static voidMain (string[] args) {intA = ten, B = 15; Doublec = 2.5, D = 5.5; //the test uses three different forms here, to see what the overloads do.Chongzai.sort (A, b); Chongzai.sort (A, c); Chongzai.sort (c, D); }}/*Run Results (15,10) (10.0,2.5) (5.5,2.5)*/
The contents of the computer class in Java