This is the 30th day of my participation in the Wenwen Challenge

Front knowledge

Identifiers: The Java language uses the Unicode character set, which uses two bytes to represent a symbol, a total of 65536 different symbols, which can describe virtually all the characters in the world. Therefore, in addition to naming identifiers in the usual way, Chinese, Japanese, and Korean can also be used as identifiers. However, for programmers: it is best to follow the general specification.

The Java language specifies a fixed number of bytes for each basic type of data. They are as follows:

Boolean 1 byte
byte 1 byte
short 2 bytes
char 2 bytes
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes

In contrast to C, the Java language has added Boolean types, which are logical types (Booleans) whose variables have only two constant values and must not be confused with C, where zero is true and zero is false.

Operation rules, selection structure, loop structure and C language are basically the same, no longer described.

Arrays are special classes, and each array is an object. Objects are Pointers.

The declaration, definition, assignment (create array), access (including using foreach, new Java traversal), and the number of elements in the array

Two-dimensional arrays: C two-dimensional arrays are stored in stack space.

In Java, two-dimensional arrays are stored in heap space.

The length of a two-dimensional array in C must be the same as the length of the first dimension, while the length of a two-dimensional array in Java need not be the same as the length of the first dimension.

Basic questions

The first reference code

import java.util.Scanner;

public classExperiment 2_1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String num = scanner.next();
        String[] str = num.split("\ \.");
        System.out.println("Integral part"+str[0]);
        System.out.println("Decimal part 0."+str[1]); }}Copy the code

Number two reference code

import java.util.Scanner;

public classExperiment 2_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] a = new int[10];
        for(int i=0; i<10; i++){ a[i] = scanner.nextInt(); }for(int i=0; i<10-1; i++){for(int j=0; j<10-i-1; j++){if(a[j]>a[j+1]) {int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp; }}}for(int k=0; k<10; k++){ System.out.print(a[k]+"");
        }

        System.out.println("The maximum is"+a[9]);
        System.out.println("The minimum is"+a[0]); }}Copy the code

Number three reference code

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public classExperiment 2_3 {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the year, month and day in order.");
        String year = scanner.next();
        String month = scanner.next();
        String day = scanner.next();
        Date date = new Date();
        String dateString = year+"-"+month+"-"+day;
        System.out.println(dateString);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        date = dateFormat.parse(dateString);

        String[] weeks = {"Sunday"."Monday"."Tuesday"."Wednesday"."Thursday"."Friday"."Saturday"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(week_index<0){
            week_index = 0;
        }
        System.out.println("This day is."+weeks[week_index]);

        int yearInt = Integer.parseInt(year);
        int monthInt = Integer.parseInt(month);
        int dayInt = Integer.parseInt(day);

        LocalDateTime time = LocalDateTime.of(yearInt,monthInt, dayInt, 0 ,0 ,0);
        int dayOfYear = time.getDayOfYear();
        System.out.printf("This is the day % D of % D year",yearInt,dayOfYear); }}Copy the code

4. Reference code

import java.util.Scanner;

/* * Determines whether an integer is prime */
public classExperiment 2_4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        if(prime(num)){
            System.out.println(num+"Is prime.");
        }else {
            System.out.println(num+"Not prime."); }}static boolean prime(int num){
        if(num<=1) return false;
        for(int i=2; i<=Math.sqrt(num); i++){if(num%i==0) {return false; }}return true; }}Copy the code

5. Reference code

/* * Enter a number, find all its factors * a number, if there are divisible numbers that do not include itself, then the books are all factors of that number. * specifies that 0 has no factor, the factor of 1 is 1, and other factors such as 4 have "1", "2" */
import java.util.Scanner;

public classExperiment 2_5 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        System.out.print(1+"");
        for(int i = 2; i<=Math.sqrt(num); i++){if(num%i==0){
                System.out.println(i+"");
                System.out.println(num/i+""); }}}}Copy the code

6. Reference code

import java.util.Scanner;

public classExperiment 2_6_1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        System.out.println(num+"The binary is"+Integer.toBinaryString(num));
        System.out.println(num+"Octal is"+Integer.toOctalString(num));
        System.out.println(num+"The hexadecimal is"+Integer.toHexString(num)); }}/* * Switch from base 16 to base 10 */
import java.util.Scanner;

public classExperiment 2_6_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String Hexnum = scanner.next();
        int num = Integer.parseInt(Hexnum,16); System.out.println(num); }}Copy the code

Number seven reference code

import java.util.Scanner;

public classExperiment 2_7 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();

        int[] cnt = new int[54];

        for(int i=0; i<str.length(); i++){char ch = str.charAt(i);
            if((ch>=65&&ch<=90 )||( ch>=97&&ch<=122)) {int index = ch-65;
                cnt[index]+=1; }}for(int j=0; j<cnt.length; j++){if(cnt[j]! =0){
                System.out.println((char)(j+65) +"A"+cnt[j]+"Time."); }}}}Copy the code