1. 主体:
public class Main {
public static void main(String[] args) {
//这里的都会被调用call
}
}
2. 输入
//Scanner
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
String str = sc.next(); // 读入下一个字符串
int x = sc.nextInt(); // 读入下一个整数
float y = sc.nextFloat(); // 读入下一个单精度浮点数
double z = sc.nextDouble(); // 读入下一个双精度浮点数
String line = sc.nextLine(); // 读入下一行
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
//默认readLine都是String,要手动转一下类型
int number = Integer.parseInt(input);
System.out.println(str);
}
}
3. 输出
public class Main {
public static void main(String[] args) throws Exception {
System.out.println(123); // 输出整数 + 换行
System.out.println("Hello World"); // 输出字符串 + 换行
System.out.print(123); // 输出整数
System.out.print("yxc\n"); // 输出字符串
System.out.printf("%04d %.2f\n", 4, 123.456D); // 格式化输出,float与double都用%f输出
}
}
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write("Hello World\n");
bw.flush(); // 需要手动刷新缓冲区
}
}
4. try-catch异常处理
调一个可能出错的方法比如输入时,java里包在try,catch里,和python类似,可以抛出异常,好习惯方便后续处理
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
int[] numbers = new int[4]; // Array to store the four numbers. numbers数组的实例名可以用a来简写
//System.out.println("Please enter 4 numbers, each followed by pressing Enter:");
// Loop to read four numbers
for (int i = 0; i < 4; i++) {
try {
// Read a line of input and try to parse it as an integer
numbers[i] = Integer.parseInt(br.readLine());
} catch (NumberFormatException e) {
// If input is not a valid integer, print an error message and decrement i to retry
System.out.println("Invalid input. Please enter an integer.");
i--;
}
}
// Perform the calculation: x1*x2 - x3*x4
int result = numbers[0] * numbers[1] - numbers[2] * numbers[3];
// Print the result
System.out.println("DIFERENCA = " + result);
} catch (IOException e) {
System.out.println("An error occurred while trying to read input.");
e.printStackTrace();
}
5. if判断,
和cpp基本一致,没啥可写的,如果下面只有一行{}可省略。可嵌套if-else.
条件表达式:&&以及(和), || 或者, ==, ! 不等于
if(!a) thensomething 如果!a为true也就是a为false,则执行。
boolean a = false;
if (!a) //注意java省略{}时不能把下面那行放上来。还有a=false不能和a=0混用。a=0时无法用if(a或!a)
System.out.println("执行了");
//输出结果为 执行了
6. switch 语句
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int day = sc.nextInt();
String name;
switch(day) {
case 1:
name = "Monday";
break; //Java的switch不加break则按顺序所有的case都会进行判断
case 2:
name = "Tuesday";
break;
case 3:
name = "Wednesday";
break;
case 4:
name = "Thursday";
break;
case 5:
name = "Friday";
break;
case 6:
name = "Saturday";
break;
case 7:
name = "Sunday";
break;
default:
name = "not valid";
}
System.out.println(name);
}
}
- 数组array
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] x = new int[10]; //创建数组。
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
int a = sc.nextInt();
if(a<=0)
x[i] = 1;
else
x[i] = a;
System.out.printf("X[%d] = %d\n",i, x[i]);
}
}
}
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];
}
}
1.2 数组的初始化
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'}; // 字符数组的初始化
}
}
多维数组:
f[n][n] 这种
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个整数的数组
}
}
int[][] a = { // 三个元素,每个元素都是大小为4的数组
{0, 1, 2, 3}, // 第1行的初始值
{4, 5, 6, 7}, // 第2行的初始值
{8, 9, 10, 11} // 第3行的初始值
};
for (int i = 0; i < 4; i ++ ) // 将第一行全部变成0
a[0][i] = 0;
数组的遍历
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();
}
}
属性length:返回数组长度,注意不加小括号
Arrays.sort():数组排序
Arrays.fill(int[] a, int val):填充数组
Arrays.toString():将数组转化为字符串
Arrays.deepToString():将多维数组转化为字符串
数组不可变长
使用Arrays需要import java.util.Arrays