1. Variables and basic data types Case 1: variable Declaration and assignment
1. Declaration of a variable int A; Declare a variable of integral type aint b,c,d; Declares three integer variable B,C,D//2. The initialization of the variable is int a = 250; Declare an integer variable A and assign a value of 250//to declare an integer variable B, and then assign a value of 250int b;b = 250;//3. The use of the variable int a = 5;int B = b+10; Remove the value of a of 5, plus 10 to assign a value to BSystem.out.println (b); The value of output B is 15system.out.println ("B"); Output string ba=a+10; Remove the value of a of 5, plus 10 and then assign the value to ASystem.out.println (a); 15int C = 3.14; Compilation error, data type mismatch System.out.println (m); Compile error, m not declared int m; System.out.println (m); Compile error, M uninitialized//4. variable named int a1,a_5$,$_; correctly named int a*b; Compilation errors, except _ and $, cannot contain special symbols such as * int 1c; Compilation error, cannot start with a number int static; Compile error, cannot use the keyword int a = 6; System.out.println (A); Compile errors, strictly case-sensitive int age; Correct, but not recommended int age; Correct, suggested "see the name of the meaning int score,myscore,myjavascore; Correct, it is recommended that "hump nomenclature"//declare integer variable A and assign a value of 25.67int a = 25.67; Compilation error, type mismatch
Case 2: Data type conversions
1.int integer int a = 3000000000; Compilation error, 3 billion out of int range System.out.println (5/2); 2 Integer Direct amount default to int, two int operation or int type, decimal place unconditional Discard System.out.println (2/5); 0system.out.println (5/2.0); 2.5 Automatic type conversion occurs, int is converted to doubleint C = 2147483647; int range maximum value C +=1; System.out.println (c); -2147483648 overflow, overflow is required to avoid//2.long long a = 10000000000; Compilation error, 10 billion defaults to int, but over range is long B = 10000000000L; 10 billion L is a long-integer direct-value long c = 10000000*2*10l; System.out.println (c); 20 billion long d = 10000000*3*10l; System.out.println (d); Overflow, 1 billion * The default is int type, but 3 billion has exceeded int range long e = 100000000l*3*10; System.out.println (e); 30 billion, in the calculation of possible overflow when the proposed first number plus llong f = system.currenttimemillis (); From 1970.1.1 0 o'clock to present the number of milliseconds is a long type//3.double floating-point double pi = 3.14; 3.14 is the direct amount of floating-point number, the default is double float pi = 3.14f;double a=6.0,b=4.9; System.out.println (a); 1.0999999999999996, rounding error, exact calculation not recommended double
2. Operator Case 1: Demonstration of operator
public class Test {public static void main (string[] args) {int a = 2, B = 7, c=5; System.out.println (a++); 2system.out.println (a); 3system.out.println (++b); 8/* * When the variable participates in the operation, the ++x, the first self-added operation * When the variable participates in the operation, X + +, the first operation in the self-added */c = c + +; /* Operation Order: * 1.c take out the value of your own memory 5 * 2. The overall value of the participating operation is 5 * 4. Then the value of the (c + +) value to c * 5.c is then changed to 5 */system.out.println (c); 5}}
Case 2: String connection
public class Test {public static void main (string[] args) {int a =1, b = 2; String o = "Qwer"; String P = a+b+o+a+b; /* * concatenation of any type and string type, the resulting string type is still calculated in the order of calculation: * *. A+B is calculated first, two int is added to int, and the result is 3. Then +o the string, get the string 3qwer and then +a+b, get the string, the result is 3qwer12 */system.out.println (P); 3qwer12}}
Case 3: Three mesh operation output three number of maximum value
Ublic class Test {public static void main (string[] args) {int a = +, B = all, c = 20;int max = (a>b?a:b) >c? ( A>B?A:B): C; SYSTEM.OUT.PRINTLN (max);}}
Case 4: Logical operator Demo
public class Test {public static void main (string[] args) {int a = 1;int b = 2;boolean c = a>1 && ++a>1;bool Ean d = a++>1 | | B--<2;boolean e = a<1 | | ++b==3; System.out.println (a); System.out.println (b); System.out.println (c); System.out.println (d); System.out.println (e);}} /*22falsefalsefalse*/
3. Branch structure Case 1: Determine how many days a certain January of a year
1 ImportJava.util.Scanner;2 3 Public classTest {4 Public Static voidMain (string[] args) {5 //user Input value6Scanner scan =NewScanner (system.in);7System.out.print ("Please enter the year of the query:");8 intYear =scan.nextint ();9System.out.print ("Please enter the month of the query:");Ten intmonth =scan.nextint (); One intDays//used to record days A Switch(month) { - Case2: - if(Year%400==0 | | (year%4==0&&year%100!=0)){ thedays = 29; - Break; -}Else{ -Days = 28; + Break; - } + Case4: A Case6: at Case9: - Case11: -Days = 30; - Break; - default: -days = 31; in Break; - } toSystem.out.println (year+ "+month+" month "+days+" Day "); + } -}View Code
4. Loop structure Case 1: Print 99 multiplication table
public class Test {public static void main (string[] args) {for (int. i=1;i<10;i++) {for (int j=1;j<=i;j++) { System.out.print (j+ "*" +i+ "=" +j*i+ "\ T");} System.out.println ();}}}
Case 2: Enter an int integer to figure out the number of each digit of this integer and
For example, input 321, the output 3+2+1=6
1 Importjava.util.Arrays;2 /*3 1.3212/10=321...24 2.321/10=21....15 3.21/10=2....16 4.2/10=0....07 rule: A/10 The final result is 0 when the stop cycle, that is, the digital <108 So when a number >0 after 10, it loops.9 */Ten ImportJava.util.Scanner; One A Public classTest { - Public Static voidMain (string[] args) { -System.out.print ("Enter an integer:"); theScanner scan =NewScanner (system.in); -String input =scan.nextline (); - intnum = integer.parseint (input);//Converts the input string to a number - intsum = 0;//used to record each number of the and + Char[] arr = Input.tochararray ();//converts the input string to a character array - while(true){ +Sum + = num%10; A if(num<10){ at Break; - } -Num/= 10; - } - for(inti=0;i<arr.length-1;i++){ -System.out.print (arr[i]+ "+"); in } -System.out.print (arr[arr.length-1]); toSystem.out.println ("=" +sum); + } -}View Code
5. Arrays
6. Methods
Java Foundation case