加载中...
avatar

Java学习讲义

# Java 语法学习汇总

# 1、最经典开局之 hello world

1
2
3
4
5
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

# 2、语法基础汇总

2.1 变量:变量必须先定义,才可以使用。不能重名

1
2
3
4
5
6
public class Main {
public static void main(String[] args) {
int a = 5;
int b, c = a, d = 10 / 2;
}
}

内置的数据类型总览

类型 字节数 举例
byte 1 123
short 2 12345
int 4 123456789
long 8 1234567891011L
float 4 1.2F
double 8 1.2, 1.2D
boolean 1 true, false
char 2 ‘A’

ps: 基本上 float 有 5-6 位是精确的,但是 double 有 15-16 位是精确的。


2.2 常量:使用 final 修饰

1
final int N = 110;

2.3 类型转换

  • 显示转化: int x = (int)'A';
  • 隐式转化: double x = 12, y = 4 * 3.3;

2.4 运算符

例子前提:A = 10, B = 20

运算符 描述 实例
+ 把两个数相加 A + B 将得到 30
- 从第一个数中减去第二个数 A - B 将得到 -10
* 把两个数相乘 A * B 将得到 200
/ 分子除以分母 B / A 将得到 2
% 取模运算符,向零整除后的余数,注意余数可能为负数 B % A 将得到 0
++ 自增运算符 A++ :先取值后加 1; ++A :先加 1 后取值
-- 自减运算符 A-- :先取值后减 1; --A :先减 1 后取值
+= 第一个数加上第二个数 A = A + B 可以简写为 A += B
-= 第一个数减去第二个数 A = A - B 可以简写为 A -= B
*= 第一个数乘以第二个数 A = A * B 可以简写为 A *= B
/= 第一个数除以第二个数 A = A / B 可以简写为 A /= B
%= 第一个对第二个数取余数 A = A % B 可以简写为 A %= B

2.5 表达式的使用

整数的加减乘除四则运算:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Main {
public static void main(String[] args) {
int a = 6 + 3 * 4 / 2 - 2;

System.out.println(a);

int b = a * 10 + 5 / 2;

System.out.println(b);

System.out.println(23 * 56 - 78 / 3);
}
}

浮点数(小数)的运算:

1
2
3
4
5
6
7
8
9
10
public class Main {
public static void main(String[] args) {
double x = 1.5, y = 3.2;

System.out.println(x * y);
System.out.println(x + y);
System.out.println(x - y);
System.out.println(x / y);
}
}

整型变量的自增、自减:

1
2
3
4
5
6
7
8
9
10
public class Main {
public static void main(String[] args) {
int a = 1;
int b = a ++ ;
System.out.println(a + " " + b);

int c = ++ a;
System.out.println(a + " " + c);
}
}

2.6 输入方式

方式 1,效率较低,输入规模较小时使用。

1
2
3
4
5
6
7
8
9
10
11
12
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(); // 读入下一行
}
}

方式 2,效率较高,输入规模较大时使用。注意需要抛异常

1
2
3
4
5
6
7
8
9
10
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();
System.out.println(str);
}
}

2.7 输出方式

方式 1,效率较低,输出规模较小时使用。

1
2
3
4
5
6
7
8
9
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输出
}
}

注意点

System.out.printf() 中不同类型变量的输出格式:

  1. int:%d
  2. float: %f , 默认保留 6 位小数
  3. double: %f , 默认保留 6 位小数
  4. char: %c , 回车也是一个字符,用 '\n' 表示
  5. String: %s

方式 2,效率较高,输出规模较大时使用。注意需要抛异常

1
2
3
4
5
6
7
8
9
10
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(); // 需要手动刷新缓冲区
}
}

# 3、判断语句

3.1 if 语句

基本的 if-else 语句:

当条件成立时,执行某些语句;否则执行另一些语句。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();

if (a > 5) {
System.out.printf("%d is big!\n", a);
System.out.printf("%d + 1 = %d\n", a, a + 1);
} else {
System.out.printf("%d is small!\n", a);
System.out.printf("%d - 1 = %d\n", a, a - 1);
}
}
}

注意else 语句可以省略:

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();

if (a > 5) {
System.out.printf("%d is big!\n", a);
System.out.printf("%d + 1 = %d\n", a, a + 1);
}
}
}

注意:当只有一条语句时,大括号可以省略:

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();

if (a > 5)
System.out.printf("%d is big!\n", a);
else
System.out.printf("%d is small!\n", a);
}
}

练习 1:输入一个整数,输出这个数的绝对值。

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();

if (x > 0)
System.out.println(x);
else
System.out.println(-x);
}
}

练习 2:输入两个整数,输出两个数中较大的那个。

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt();

if (a > b)
System.out.println(a);
else
System.out.println(b);
}
}

注意if-else 语句内部也可以是 if-else 语句。

练习 3:输入三个整数,输出三个数中最大的那个。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();

if (a > b) {
if (a > c)
System.out.println(a);
else
System.out.println(c);
} else {
if (b > c)
System.out.println(b);
else
System.out.println(c);
}
}
}

3.2 常见比较运算符

(1) 大于 >
(2) 小于 <
(3) 大于等于 >=
(4) 小于等于 <=
(5) 等于 ==
(6) 不等于 !=

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt();

if (a > b) System.out.printf("%d > %d\n", a, b);
if (a >= b) System.out.printf("%d >= %d\n", a, b);
if (a < b) System.out.printf("%d < %d\n", a, b);
if (a <= b) System.out.printf("%d <= %d\n", a, b);
if (a == b) System.out.printf("%d == %d\n", a, b);
if (a != b) System.out.printf("%d != %d\n", a, b);
}
}
文章作者: Ex-monster
文章链接: http://example.com/2023/07/15/Java%20%E8%AF%AD%E6%B3%95%E5%AD%A6%E4%B9%A0%E6%B1%87%E6%80%BB/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 monster's blog
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论