Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

Example 1: Character variable

Public class CharacterTest {public static void main (String args[]) {char chinaWord=' you ',japanWord=' work '; int p1=36328,p2=38358; System.out.println(" Position of the character 'you' in the Unicode table :"+(int)chinaWord); System.out.println(" Japanese 'work for' in the unicode table order position :"+(int)japanWord); Println (" +(char)p1); system.out. println(" +(char)p1); Println (" +(char)p2); system.out. println(" +(char)p2); }}Copy the code

Example 2: Data type conversion

public classDataTypeTest { public static void main (String args[ ]) { int c=2200; long d=8000; float f; Double g = 123456789.123456789; c=(int)d; f=(float)g; // Result in a loss of accuracy. System.out.print("c= "+c); System.out.println(" d= "+d); System.out.println("f= "+f); System.out.println("g= "+g); }}Copy the code

example3: uses xOR to encrypt and decrypt characters

Class XORTest {public static void main(String args[]){class XORTest {public static void main(String args[]){class XORTest {public static void main(String args[]){class XORTest {public static void main(String args[]){class XORTest {public static void main(String args[]){ char secret='8'; a1=(char)(a1^secret); a2=(char)(a2^secret); a3=(char)(a3^secret); a4=(char)(a4^secret); System. The out. Println (" cipher text: "+ a1 + a2, a3, a4); a1=(char)(a1^secret); a2=(char)(a2^secret); a3=(char)(a3^secret); a4=(char)(a4^secret); System. The out. Println (", "+ a1 + a2, a3, a4); }}Copy the code

example4: short circuit logic or (| |) and bit operations (|)

class OrTest { public static void main(String args[]) { int x,y=10; If (((x = 0) = = 0) | | (20) (20) y = = =) {System. Out. Println (" now the value of y is: "+ y); } int a,b=10; If (((a = 0) = = 0) | (20) (= = = 20 b)) {System. Out. Println (" now the value of b is: "+ b); }}}Copy the code

example5: Use the if statement to achieve the values of a, B, C in ascending order

   

public class SortABC{ public static void main(String args[]){ int a=9,b=5,c=7,t; if(a>b) { t=a; a=b; b=t; } if(a>c) { t=a; a=c; c=t; } if(b>c) { t=b; b=c; c=t; } System.out.println("a="+a+",b="+b+",c="+c); }}Copy the code

example6: Use the if statement to determine whether a given grade is passing or not

public class Score { public static void main(String args[]){ int math=65 ,english=85; If (math>=60) {system.out.println (" math pass "); } else {system.out.println (" math failed "); } if(English >90) {system.out.println (" English is excellent "); } else {system.out.println (" English is not excellent "); } system.out. println(" I'm learning control statements "); }}Copy the code

example7: The use of the switch statement. When the main program is executed, if the first character of the command line argument is a digit, a lowercase letter, and a uppercase letter, the system will display the first character. If the input is a non-number or letter, the display is not a number or letter.

class Ex2_07 { public static void main(String[] args) { char ch = args[0].charAt(0); switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': System.out.println("The character is digit " + ch); break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': System.out.println("The character is lowercase letter " + ch); break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': System.out.println("The character is uppercase letter " + ch); break; default: System.out.println("The character " + ch + " is neither a digit nor a letter."); }}}Copy the code

example8: Use the for loop to calculate 5+ 55 + 555 +… The sum of the first 10 terms of phi

public class Example3_6{ public static void main(String args[]){ long sum=0,a=5,item=a,n=10,i=1; for(i=1; i<=n; i++) { sum=sum+item; item=item*10+a; } System.out.println(sum); }}Copy the code

example9: Use the while loop to calculate 1 + 1/2! + a third! + a quarter! + + 1/20! The value of the

class Example3_7 { public static void main(String args[]) { double sum=0,a=1; int i=1; while(i<=20) { sum=sum+a; i=i+1; A = a * (1.0 / I); } System.out.println("sum="+sum); }}Copy the code

Example 10: Compute the sum of the digits of a given integer

class Ex2_10 {

    public static void main(String args[]) {

         int x = 12345;

         int y=x;

         int r=0;

         int sum = 0;

         while(y!=0) {

             r = y % 10;

             sum += r;

             y = y / 10;          

         }

         System.out.println("x -> " + sum);

    }

}
Copy the code

example11: Break and continue use examples to calculate the sum of odd numbers up to 10 and prime numbers up to 50, respectively

class Example3_8 { public static void main(String args[]){ int sum=0,i,j; for( i=1; i<=10; I ++){if(I %2==0) {if(I %2==0); sum=sum+i; } System.out.println("sum="+sum); for(j=2; j<=50; J++) {for(I =2; i<=j/2; i++) { if(j%i==0) break; } if(I >j/2) {system.out.println (""+j+" is prime "); }}}}Copy the code

Example 11: One-dimensional array example, output the element with the smallest value and its subscript.)

public class ArrayTest { public static void main(String args[]) { int a[] = { 52, 78, 90, 34, 16, 34, 67 }; int indexOfMinElement = 0; for (int i = 1; i < a.length; i++) { if (a[indexOfMinElement] > a[i]) { indexOfMinElement = i; } } System.out.println("a[" + indexOfMinElement + "] = " + a[indexOfMinElement]); }}Copy the code

Example 12: Compute the sum of the rows in a two-dimensional array and find the row with the largest value

public class TableTester { public static void main(String args[]) { int myTable[][] = { {23, 45, 65, 34, 21, 67, 78}, {46, 14, 18, 46, 98, 63, 88}, {98, 81, 64, 90, 21, 14, 23}, {43, 54, 55, 76, 22, 43, 33}}; int sum, max, maxRow=0; max = 0; //Assume all numbers are positive for (int row=0; row<4; row++) { sum = 0; for (int col=0; col<7; col++) sum += myTable[row][col]; if (sum > max) { max = sum; maxRow = row; } } System.out.println("Row " + maxRow + " has the highest sum of " + max); }}Copy the code

\