1. Variable operator expression input and output
A simple Java program
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
The operators +, -, *, /, %, ++, --, +=, -=, *=, /=, and %= are similar to C++
Variables must be defined before they can be used. Cannot have the same name.
How variables are defined:
public class Main {
public static void main(String[] args) {
int a = 5;
int b, c = a, d = 10 / 2;
}
}
Constants are modified with final. References declared as final are like variables declared as const in C++ and cannot be changed.
final int N = 110;
Built-in data types
byte, short, int, long, double, char, and boolean are all similar to C++. boolean occupies 1 byte and the expression form is true or false.
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();//整型
double b = sc.nextDouble();//浮点数
String op = sc.next();//字符的定义
boolean flag = true;
}
}
When comparing characters
if("M".equals(op))
2. Judgment statements
In Java, you can also use the ternary operator directly when outputting
For example, in the example, if Z is greater than or equal to 5.0, then output Aluno aprovado.
, otherwise output Aluno reprovado.
System.out.println(z >= 5.0 ? "Aluno aprovado." : "Aluno reprovado.");
When judging whether it is a perfect number (an integer, if the sum of all other divisors except itself is equal to the number, then we call the integer a perfect number), the advantage is that you can directly use "+" to make statements of splicing.
System.out.println(sum == a ? a + " is perfect" : a + " is not perfect");
Equivalent to
if (sum == x)
System.out.printf("%d is perfect\n", x);
else
System.out.printf("%d is not perfect\n", x);
3. Loop statements
Three types of loops:
while loop, do while loop, for loop (same as C++)
Jump statement:
break, continue (same as C++)
Classic example:
Fibonacci Sequence
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 1, b = 1;
for (int i = 1; i < n; i ++ ) {
int c = a + b;
a = b;
b = c;
}
System.out.println(a);
}
}
4. Array
Define one-dimensional array
public class Main {
public static void main(String[] args) {
int[] a = new int[10], b;
float[] f = new float[33];
double[] d = new double[123];
char[] c = new char[21];
}
}
Initialization of array
public class Main {
public static void main(String[] args) {
int[] a = {0, 1, 2}; // 含有3个元素的数组,元素分别是0, 1, 2
int[] b = new int[3]; // 含有3个元素的数组,元素的值均为0
char[] d = {'a', 'b', 'c'}; // 字符数组的初始化
}
}
Define multidimensional array
public class Main {
public static void main(String[] args) {
int[][] a = new int[3][4]; // 大小为3的数组,每个元素是含有4个整数的数组。
int[][][] b = new int[10][20][30]; // 将所有元素的初值为0
// 大小为10的数组,它的每个元素是含有20个数组的数组
// 这些数组的元素是含有30个整数的数组
}
}
Array traversal:
for(int[] row: a)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[][] a = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11},
};
for (int[] row: a) { // 范围遍历
for (int x: row) // 范围遍历
System.out.printf("%d ", x);
System.out.println();
}
}
}
Classic example:
Array to find the Nth term of Fibonacci sequence
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] f = new int[n + 1];
f[0] = 0;
f[1] = 1;
for (int i = 2; i <= n; i ++ )
f[i] = f[i - 1] + f[i - 2];
System.out.println(f[n]);
}
}
Commonly used APIs
5. String
initialization
String a = "Hello World";
String b = "My name is ";
String x = b; // 存储到了相同地址
String c = b + "abc"; // String可以通过加号拼接
String d = "My age is " + 18; // int会被隐式转化成字符串"18"
String str = String.format("My age is %d", 18); // 格式化字符串,类似于C++中的sprintf
String money_str = "123.45";
double money = Double.parseDouble(money_str); // String转double
Commonly used APIs
input Output
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.next(); // 输入字符串,遇到空格、回车等空白字符时停止输入
String str2 = sc.nextLine(); // 输入一整行字符串,遇到空格不会停止输入,遇到回车才会停止
System.out.println(str1); // 可以直接输出
System.out.printf("%s\n", str2); // 也可以格式化输出,用 %s 表示字符串
}
}