Twelve Classical Examples of Java Programming

  • 2021-12-04 10:11:29
  • OfStack

Directory Example 1: Character Type Variable Example 2: Data Type Conversion Example 3: Encryption and Decryption of Characters Using XOR Example 4: Difference between Short Circuit Logical OR () and Bit Operation () Example 5: Sort the values of a, b and c with if statement from small to large Example 6: Use if statement to judge whether a given score passes Example 7: Use switch statement Example 8: Use for loop to calculate 5 + 55 + 555 +. . . The sum of the first 10 items of Example 9: Using the while loop, calculate 1 + 1/2! + 1/3! + 1/4! + + 1/20! Example 10: Calculate the sum of the numbers of a given integer Example 11: break and continue use examples, respectively, calculate the odd sum of numbers within 10, calculate prime numbers within 50 Example 11: 1-dimensional array Example, output the element with the smallest value in the 1-dimensional integer array and its subscript Example 12: Calculate the sum of the elements in the 2-dimensional array and find the row with the largest value

Example 1: Character variable


public class  CharacterTest {

    public static void main (String args[ ]) {

char chinaWord=' You ',japanWord=' Shiri ';

      int  p1=36328,p2=38358;

      System.out.println(" Chinese characters ' You ' In unicode Sequential positions in a table :"+(int)chinaWord);

      System.out.println(" Japanese ' Shiri ' In unicode Sequential positions in a table :"+(int)japanWord);

      System.out.println("unicode Table number 20328 The character in the position is :"+(char)p1);

      System.out.println("unicode Table number 12358 The character in the position is :"+(char)p2);

    }

}



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;   // Lead to a loss of accuracy .

   System.out.print("c=  "+c);  

System.out.println("  d=  "+d);

      System.out.println("f=  "+f);

 System.out.println("g=  "+g);

    }

}



Example 3: Encrypting and Decrypting Characters Using XOR


class XORTest {

   public static void main(String args[]){

   char a1='10',a2=' Point ',a3=' Advance ',a4=' Attack ';

     char secret='8';

     a1=(char)(a1^secret);  

     a2=(char)(a2^secret);

     a3=(char)(a3^secret);  

     a4=(char)(a4^secret);

     System.out.println(" Ciphertext :"+a1+a2+a3+a4);

     a1=(char)(a1^secret);  

     a2=(char)(a2^secret);

     a3=(char)(a3^secret); 

     a4=(char)(a4^secret);

     System.out.println(" Original text :"+a1+a2+a3+a4);

    }

}

Example 4: The difference between short-circuit logical OR () and bit operation ()


class OrTest {

    public static void main(String args[]) {

  int x,y=10;

       if(((x=0)==0)||((y=20)==20)) {

   System.out.println(" Now y The value of is :"+y);

       }

       int a,b=10;

       if(((a=0)==0)|((b=20)==20)) {

   System.out.println(" Now b The value of is :"+b);

       }

    }

}



Example 5: Using if statement to sort the values of a, b and c from small to large


 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);

   }

    }



Example 6: Use if statement to judge whether a given grade passes or not


public class Score {

  public static void main(String args[]){

  int math=65 ,english=85;

      if(math>=60) {

   System.out.println(" Pass the math ");

      }

      else {

    System.out.println(" Fail math ");

      }

      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 ");

    }

}



Example 7: Use of switch statement

When the main program executes, if the first command-line argument is numeric, lowercase, and uppercase, the first character will be displayed. If the input is not a number or letter, it 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.");

        }

    }

}

Example 8: Calculate 5 + 55 + 555 + using the for loop. . . The sum of the first 10 items of


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);

     }

}



Example 9: Using the while loop, calculate 1 + 1/2! + 1/3! + 1/4! + + 1/20! Value of


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);

     }

}



Example 10: Calculating the sum of the numbers 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);

    }

}



Example 11: break and continue are used as examples to calculate odd sums within 10 and prime numbers within 50 respectively


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;   // Lead to a loss of accuracy .

   System.out.print("c=  "+c);  

System.out.println("  d=  "+d);

      System.out.println("f=  "+f);

 System.out.println("g=  "+g);

    }

}



0

Example 11: 1-Dimensional Array for example, output the element with the smallest value in 1-Dimensional Integer Array and its subscript)


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;   // Lead to a loss of accuracy .

   System.out.print("c=  "+c);  

System.out.println("  d=  "+d);

      System.out.println("f=  "+f);

 System.out.println("g=  "+g);

    }

}



1

Example 12: Calculate the sum of the elements of each row in a 2-dimensional array and find the row with the largest value


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;   // Lead to a loss of accuracy .

   System.out.print("c=  "+c);  

System.out.println("  d=  "+d);

      System.out.println("f=  "+f);

 System.out.println("g=  "+g);

    }

}



2

Related articles: