Java Process Control
The syntax of Java process control is similar to C/C++, and there are also if...else, while, do...while, for, switch...case, etc. The specific syntax will not be discussed here, but only examples will be given.
Output nine-nine multiplication table (upper right triangle):
public class Demo { public static void main(String[] args){ int i, j; for(i=1; i<=9; i++){ for(j=1; j<=9; j++){ if( j<i){ //Print eight spaces, remove the spaces and then the upper left triangle System.out.print(" "); }else{ System.out.printf("%d*%d=%2d", i, j , i*j); } } System.out.print("/n"); } }}
Running results:
1*1= 1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9 2*2= 4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*3= 9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 6*6=36 6*7=42 6*8=48 6*9=54 7*7=49 7*8=56 7*9=63 8*8=64 8*9=72 9*9=81
There are also printf() statements in Java to control the output format, but they are not commonly used in actual development because the data output to the console rarely requires strict formats. Generally, println() and print() are enough.
println() outputs the content and breaks the line, print() does not break the line.
For example, how many days are there in a certain month of a certain year:
import java.util.*;public class Demo { public static void main(String[] args){ int days = 0; // Get user input Scanner sc = new Scanner(System.in); System.ou t.print(" Enter year: "); int year = sc.nextInt(); System.out.print("Input month: "); int month = sc.nextInt(); switch(month){ case 1: case 3: case 5 : case 7: case 8: case 10: case 12: days=31; break; case 4: case 6: case 9: case 11: days=30; break; case 2: // Judge leap year if(year%4= =0 && year%100!=0 || year%400==0) days=29; else days=28; break; default: System.out.println("Month input error!"); System.exit(0 ); // Force ending the program} System.out.printf("Date: %d/n", days); }}
Running results:
Enter year: 2014 Enter month: 02 Days: 28
There is no scanf() statement in Java like C. It is a bit troublesome to get input from the console. I recommend using the Scanner class. Please check the API for the specific syntax.
Java operators
Operators in Java are almost the same as C/C++.
Mathematical operators
Mathematical operation, the result is a numerical value. See the table below:
Relational operators
Relational operator, the result is a Boolean value. See the table below:
bit operator
The bit operator performs logical operations bit by bit on the binary form of an integer to obtain an integer. See the table below:
Conditional operator
There is also a conditional operator in Java (trigonomic operator):
condition ? x1 : x2
condition is a boolean value. According to the condition, take the value of x1 or x2.
Here is a comprehensive example of operators:
public class Demo { public static void main(String[] args){ int a=10; int b=10; int x=10; int y=21; int z=10; System.out.println("Add to add it a=" + (a++)); System.out.println("a's value a=" + a); System.out.println("prefixed b=" + (++b)); System.out .println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ); System.out.println("Think x>y and x<y, right?" + ( (x>y) && (x<y) )); System.out.println("Think x>=y Or x==y, right? " + ( (x>=y) || (x==y) )); System.out.println("Does it think x<y or x=z, right?" + ( (x<y) || (x==z) )); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- out.println("a&x result is: " + (a&x)); System.out.println("a|x result is: " + (a|x)); System.out.println("y^z The result is: " + (y^z)); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ "a The result of shifting 2 bits left is: " + (a<<2)); System.out.println("y The result of shifting 3 bits right is: " + (y>>3)); }}
Running results:
Add the value of a=10a after adding a=11 before adding b=11------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ false thinks x>y and x<y, right? false thinks x>=y or x==y, right? false thinks x<y or x=z, right? true-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------a The result of shifting 2 bits left is: 44y The result of shifting 3 bits right is: 2