Java: basic syntax
LilianChenJava: the basic syntax Java language is composed of classes and objects. Its objects and classes are composed of variables and methods, and the methods also contain statements and expressions. 1. the variable Java language provides two variables: member variables and local variable member variables: they are declared and defined in the class of the method in vitro and can be automatically initialized: it is declared and defined in the method and cannot be automatically initialized. After the method is executed, the local variable does not exist in Java. before using any variable, you must create the variable, creating a variable is actually a process of declaring a variable. You must specify the variable type and name. 1 int a; 2 boolean B; 3 char c = 'a ';
1 public class DataDemo { 2 3 int a; 4 public void test() 5 { 6 boolean b=false; 7 char c='\0'; 8 } 9 10 public static void main(String[] args) {11 float f=0;12 String s=null;13 14 }15 16 }
The automatic initialization value corresponding to the member variable: type Variable Initial Value byte 0 integer int 0 single precision fload 0.0f character type char '\ u0000' String type String null short integer short 0 long integer long 0L double 0.0d boolean false 2. four basic data types: Java data type: Boolean INTEGER: integer, short integer, long integer, byte type floating point type: single precision type, Double Precision String is not the basic data type. The variable defined by the String class is an object, not a simple type. Different from simple types, class objects contain their own methods and are complex types. Boolean is used for logical condition judgment. It contains only two values, true and false ). In C, values 1 and true are equivalent. values 0 and false are equivalent. However, in Java, values of boolean variables can only be true or false. 3. In the modulo operation (%) of the arithmetic operator, if the operand contains positive and negative numbers, the positive and negative numbers of the result are the same as those of the left operand. Example:-8% 3 =-2, 8% (-3) = 2 4. switch statement
public class SwitchTest { public static void main(String[] args) { int student[] = {95, 85, 75, 65, 55}; for(int i=0; i<5; i++) { switch(student[i]/10) { case 9: System.out.println("Student" + i + "'s result is A!"); break; case 8: System.out.println("Student" + i + "'s result is B!"); break; case 7: System.out.println("Student" + i + "'s result is C!"); break; case 6: System.out.println("Student" + i + "'s result is D!"); break; default: System.out.println("Student" + i + "'s result is F!"); } } }}
Student0's result is! Student1's result is B! Student2's result is C! Student3's result is D! Student4's result is F!
public class SwitchTest { public static void main(String[] args) { int student[] = {95, 85, 75, 65, 55}; for(int i=0; i<5; i++) { switch(student[i]/10) { case 9: System.out.println("Student" + i + "'s result is A!"); case 8: System.out.println("Student" + i + "'s result is B!"); case 7: System.out.println("Student" + i + "'s result is C!"); case 6: System.out.println("Student" + i + "'s result is D!"); default: System.out.println("Student" + i + "'s result is F!"); } } }}
Student0's result is! Student0's result is B! Student0's result is C! Student0's result is D! Student0's result is F! Student1's result is B! Student1's result is C! Student1's result is D! Student1's result is F! Student2's result is C! Student2's result is D! Student2's result is F! Student3's result is D! Student3's result is F! Student4's result is F! Note: If you have already entered a case Branch and the case statement does not use break, you can directly access each case in the future without matching. 5. Practical exercises use the for loop to implement 1 ~ Summation of odd numbers between 99
import javax.swing.JOptionPane;public class OddSum { public static void main(String[] args) { int sum=0; for(int i=1; i<=99; i++) { if(i%2!=0) { sum=sum+i; } } JOptionPane.showMessageDialog(null, "The sum is "+ sum, "Total Even Integer from 1 to 99", JOptionPane.INFORMATION_MESSAGE); }}
The switch statement and for loop are combined to copy the code public class TestSwitch {int I = 0, w = 0; public TestSwitch () {for (; I <= 5; I ++) {switch (I) {case 3: w + = 1; case 0: w + = 1; case 1: w + = 1; continue; case 2: w + = 1; case 4: w + = 1; default: w + = 2;} System. out. print ("" + w) ;}} public static void main (String [] args) {TestSwitch testSwitch = new TestSwitch ();}}
7 13 15 draw a right triangle with "*" using multiple for loops and display it in the message dialog box
import javax.swing.JOptionPane;public class MultipleLoop1 { public static void main(String[] args) { String out=""; loop: for(int row=1; row<=5; row++) { out+="\n"; for(int column=1; column<+6; column++) { if(column>row) continue loop; out+="* "; } } JOptionPane.showMessageDialog(null, out, "Test multiply loop 1", JOptionPane.INFORMATION_MESSAGE); }}
Use "*" to output a diamond and display it
public class MultiplyLoop2 { public static void main(String[] args) { MultiplyLoop2 multiplyLoop2=new MultiplyLoop2(); multiplyLoop2.print(11); } public void print(int n) { int temp=0; for(int i=0; i< n; i++) { for(int j=0; j<Math.abs(n/2-i);j++) { System.out.print(" "); } if(i<=n/2) { temp=i; } else { temp=n-i-1; } for(int k=0; k<(2*temp +1); k++) { System.out.print("*"); } System.out.println(); } }