Java Getting Started Diary 1 | Summary of the first week

An unrefined beginning:

       To be precise, today is my first day of learning Java. In the class, the teacher instructed us how to download - how to install - how to configure, etc., but the Java grammar is a big move that is airtight and afraid of leaking. In this case, we still need to do questions. The implication is that we learn to go to the Internet to seek help. I must admit that the process of finding codes is not easy. Fortunately, the five questions given in the first week are not difficult. After all, it was solved relatively easily, so naturally we have to clean up quickly.


While you're reading this article, you might want to quickly know what it can tell you, so here's what I'm about to do:

1. How to create a program in IDEA

2. What does a complete program look like?

3. Calling of packages and classes

4. What is a static method?

5. Simple output System.out.print

6. Command line parameter setting

7. Simple input Scanner

8. Convert string to integer

9. String to character array

10. Integers are forced to convert to decimals

11. End the program


1. How to create a program in IDEA?

First we need to create a new project: Open IDEA > File > New > Project in the upper left corner

Then create a new program in the project: left Project > src > right click src > New > Java Class > enter a name

2. What does a complete program look like?

Take the first question of this week as an example. The question requires input of an integer n, and output the calculation result of 2 to the nth power.

It can be seen that it is still very similar to C.

3. Calling of packages and classes

The package is equivalent to the thing behind include in C language, but import is used here

import java.util.Scanner;

The above statement represents the package that introduces Scanner, and Scanner is a package used to read in.

There are two main packages in Java. The core package starts with "java" and is directly provided by JDK. For example, Scanner is the core package; the extension package starts with "javax".

There are classes in the package, which are equivalent to the functions called after include in C.

4. What is a static method?

Methods that can be used without creating an object.

5. Simple output

System.out.println("Hello World!");
System.out.print("Hello World!");

The difference between having ln and not having ln is that if there is ln output, a carriage return will be automatically added after the output, and without ln there will be no carriage return.

System.out.printf("%.3lf\n",ans);

This is similar to the output method of C language.

6. Command line parameter setting

The small green arrow on the left of the program > Modefy Run Configuration > Program Arguments

7. Simple input

Method 1: Type in Scanner

The package Scanner must be imported first, and then it can be read.

Scanner s = new Scanner(System.in);
//创建一个读入器s
String a = s.next();
//每次用s读入一个字符串(遇到空格或回车则停止读入),并且存入字符串a
s.close();
//关闭读入器,也就是停止读入

 Let’s look at a troublesome example of reading three numbers in a row. Of course, you can choose a for loop to solve it. I don’t, hum

Scanner s = new Scanner(System.in);
int a,b,c;
String num = s.next();
a = Integer.parseInt(num);
num = s.next();
b = Integer.parseInt(num);
num = s.next();
c = Integer.parseInt(num);
s.close();

 Method 2: read in the command line parameters, this method is also similar to the C language

for (String x : args) {
    System.out.println(x);
}

8. Forcibly convert integer to Integer.parseInt

String num = s.next();
a = Integer.parseInt(num);

9. String to character array

String str = "Hello";
//str是一个字符串
char[] a = str.toCharArray();
//这样操作以后,a就是一个字符数组,且为"Hello"

Computing the length of strings and character arrays is the same.

int len_str = str.length();
int len_a = str.a();
//len_str和len_a都等于5

But the calling method of elements of string and character array is different.

The character array is the same as the C language, a[0], a[1] are legal, but the string does not allow this. If you want to call the elements on the string, the correct way is str.charAt()

System.out.print( str.charAt(4) );
//输出结果为o

10. Integers are forced to convert to decimals

Same as C language

int a1 = 2, a2 = 4;

int ans_i;
ans_i = a1 / a2;    //ans_i等于0

double ans_d;
ans_d = (double)a1 / a2;    //ans_d等于0.5

11. End the program

System.exit(0);
//0表示顺利结束,非0表示有错误发生。

        After writing the notes here, put the complete codes of the two questions, everyone can try to see if they can understand, and then you can start writing spicy!

Question 2. Description of the topic

Given a word, if the word ends with `er`, `ly` or `ing` suffix, remove the suffix (the title guarantees that the length of the word after removing the suffix is ​​not 0), otherwise do nothing.

input format

Enter a line containing one word (there is no space between words, and the maximum length of each word is 32).

output format

Output the words processed according to the requirements of the topic.

import java.util.Scanner;

public class W1T2 {
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        String a = s.next();
        s.close();

        char[] str = a.toCharArray();

        int len = str.length,flag = 3;
        if ( (str[len-2]=='e' && str[len-1]=='r') || (str[len-2]=='l' && str[len-1]=='y') )
            flag = 1;
        else if (str[len-3]=='i' && str[len-2]=='n' && str[len-1]=='g')
            flag = 0;

        for (int i=0;i<len-3+flag;i++) {
            System.out.print(str[i]);
        }
        System.out.println();

    }
}

Question 5. Description of the topic

Implement a solution in Java. For two input strings, determine whether the second string is the end of the first string.

input format

The input is only one line, containing two strings str and ending, which are separated by spaces.

output format

true or false

import java.util.Scanner;

public class W1T5 {
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        String str_a = s.next();
        String str_b = s.next();
        s.close();

        int flag = 1;
        if (str_a.length() < str_b.length()) {
            flag = 0;
        }
        if ( flag==1 ) {
            int k = str_a.length()-1;
            for (int i=str_b.length()-1;i>=0;i--) {
                if (str_a.charAt(k)!=str_b.charAt(i)) {
                    flag = 0; break;
                }
                k--;
            }
        }
        if (flag==1) System.out.println("true");
        else System.out.println("false");
    }
}

See you next week~

I am a novice in learning Java and Python~ If there are mistakes, please correct me!

Guess you like

Origin blog.csdn.net/m0_70241024/article/details/126751797