Java Language Basic Grammar Practice

Topic 1 : Conversion operation from value of char type to int type    

It is known that the value of the int type variable a is 100, and the value of the char type variable c is 'medium'. After adding a and c, assign the value to the int type variable x. Please use binary (base 2) unsigned according to the output format requirements Outputs the string representation of a in integer form, the string representation of c in hexadecimal (base 16) unsigned integer form, and the decimal integer value corresponding to c and x in string representation.

Output format requirements: For example, the value of a is 500, and the value of c is 'country', the output content is as follows:

Outputs the string representation of a as a binary (base 2) unsigned integer: 111110100

The string representation of c in hexadecimal (base 16) unsigned integer form is: 56fd

Output the decimal integer value corresponding to a in string representation: 500

Output the decimal integer value corresponding to c in string representation: 22269

Output the decimal integer value corresponding to x in string representation: 22769

package test1;

public class work_01 {
    public static void main(String[] args) {
        int a=500;
        char c='国';
        String s="国";
        int x=a+c;
        System.out.print("以二进制(基数 2)无符号整数形式输出a的字符串表示形式为:");
        print(a);
        System.out.println("以十六进制(基数 16)无符号整数形式c的字符串表示形式为:"+Integer.toHexString(c));
        System.out.println("以字符串表示形式输出a对应的十进制整数值为:"+a);
        System.out.println("以字符串表示形式输出c对应的十进制整数值为:"+(int)c);
        System.out.println("以字符串表示形式输出x对应的十进制整数值为:"+x);
    }
    public static void print(int num) {
        for(int i=31;i>=0;i--) {
            System.out.print((num&(1<<i))==0?"0":"1");/*1左移某个位,再和num相与,最终得到的就是num在该位上的数值*/
        }
        System.out.println();
    }
    public static String toHexString(String s){
        String str="";
        for(int i=0;i<s.length();i++){
            int ch=(int)s.charAt(i);
            String s4=Integer.toHexString(ch);
            str=str+4;
        }
        return "0x:"+str;
    }
}

The resulting output is as follows :

以二进制(基数 2)无符号整数形式输出a的字符串表示形式为:00000000000000000000000111110100
以十六进制(基数 16)无符号整数形式c的字符串表示形式为:56fd
以字符串表示形式输出a对应的十进制整数值为:500
以字符串表示形式输出c对应的十进制整数值为:22269
以字符串表示形式输出x对应的十进制整数值为:22769

Process finished with exit code 0

Topic 2 : Use the for loop to output the elements in the array  

Define an array of int type {320, 871, 33, 5895, 126, 10761, 20001, 83, 6262, 1287}, and use the for loop to output the elements in the array.

output:

320 871 33 5895 126 10761 20001 83 6262 1287

public class work_08 {
    public static void main(String[] args) {
        int[]arr={320, 871, 33, 5895, 126, 10761, 20001, 83, 6262, 1287};
        for(int i=0;i<arr.length;i++)
        {
            System.out.print(arr[i]+" ");
        }
    }
}

The resulting output is as follows :

320 871 33 5895 126 10761 20001 83 6262 1287 
Process finished with exit code 0


Topic three :

Using Two-Dimensional Array Programming to Output Nine-Nine Multiplication Table  

output:

1*1=1

2*1=2   2*2=4

3*1=3   3*2=6   3*3=9

4*1=4   4*2=8   4*3=12  4*4=16

5*1=5   5*2=10  5*3=15  5*4=20  5*5=25

6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36

7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49

8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64

9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81

public class work_12 {
    public static void main(String[] args) {
        for(int i=1;i<10;i++){
            for(int j=1;j<=i;j++) {
                int p = i * j;
                System.out.print(i +"*"+ j + "=" + p+"   ");
            }
            System.out.println();
        }
    }
}

The resulting output is as follows :

1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12   4*4=16   
5*1=5   5*2=10   5*3=15   5*4=20   5*5=25   
6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36   
7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49   
8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64   
9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81   

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/weixin_64260325/article/details/127692015