Data Types,Math,Client Input,Debugging
一. Data types
数据类型是一组具有相同性质的值的集合,同时定义在这些值上的一组操作。它决定了变量能存储什么样的数据,以及这些数据能进行什么样的操作。
1. Integer, Floating Point, Boolean, Char(字符), String
int a = 25;中
25是iterals字面量/字面值
2. Arithmetic(+, -), Logical(与&&或||非!), Comparison (<,>,==,!=)Operators(运算符)
Intergers
An integer is a whole number between -231 (-2,147,483,648) and 231 − 1
(2,147,483,647)
● Operations with arithmetic operators:
- 加法(addition) +
- 减法(subtraction) -
- 乘法(multiplication) *
- 除法(division) /
- 取模(modulo)% (余数)
- 编译错误(compile error):语法错误、类型不匹配、缺少必要的库引用或者其他不符合编译器规则
- 运行时错误(runtime error):数组越界、空指针异常、除数为零等。这些问题只有在实际运行时才会暴露出来,因为它们依赖于特定的输入或环境条件。通常是因为程序试图执行某些不可能的操作,比如除以零。
- Infinity:表示结果是无限大。浮点数除以0.0
- NaN:代表无法表示为数字的结果,例如0除以0/0.0除以0.0If int is not large enough, you can use long data type
Decimal Number
- double类型:在编程中,double类型是一种数据类型,用于存储大范围的浮点数,其精度高于float类型,但占用的内存空间也更大。
- 浮点数:浮点数是一种数学上的表示方式,用于表示实数,可以包含小数部分。在计算机中,浮点数通常用来表示非整数的数,比如分数、小数等。
浮点数除以0不会报错,而是会返回无穷大(Infinity)或负无穷大(-Infinity),具体取决于被除数的符号。
整数除以0会导致runtime error。
Comparison Oprators
比较运算符,Comparison operators that take operands运算数 of one type (e.g., int or double) and produce a result of type boolean
do not confuse equality operator == with assignment operator =
Precedence优先级
Operator运算符precedence / order of execution:
Char
A char is an alphanumeric(字母数字) character or symbol, enclosed in single quote ’ ’
一个字符(Char)是一个Unicode字符
○ 它实际上是一个整数,范围在0到65,535之间(包含)
○ 它可以像整数一样进行比较,使用 == != < <= >= >
char firstLetter = 'A';
firstLetter == 65;
String
字符串是由字符(chars)组成的序列,比如单词 “Halloween”,或者句子 “A corpse is talking.”
● 在Java中,通过在(double quotes)双引号 " " 之间写入字符来创建一个字符串。
Concatenation连接
使用加号Plus(+)操作符将不同类型的数据转换成字符串(String)格式。这种转换通常是指将数字、布尔值、对象等非字符串类型的数据与一个字符串相加,结果是一个新的字符串。
String bangbang = bang + 2;
Convert Strings to Numbers(parse前缀)
● Convert a String to an integer with Integer.parseInt()
String s = "5";
int i = Integer.parseInt(s);
i++;
System.out.println(i)
同理
Convert a String to a double with Double.parseDouble()
Boolean.parseBoolean()
如果字符串不是有效的double或整数表示,它会抛出NumberFormatException
。
to前缀将其他数据类型转换为字符串
Integer.toString()
Double.toString()
Boolean.toString()
二. Math Library库, Random Numbers, Casting(类型转换)
Math Library
Rounding and Casting
我们需要先将长输出转换为整数,通过强制类型转换(casting)
int i = (int) Math.round(3.14159);
Math.round:用于将一个浮点数四舍五入到最接近的整数。
○ 强制类型转换double并不会进行四舍五入(round),相反,小数部分(fractional part)会被忽略。
Problem Solving:Random Integer
int min = 1;
int max = 6;
int randomInt = min + (int) (Math.random() * (max - min + 1));
三. Program Input输入 and Interactive交互式 Programs by converting between types
四. Using Debugger调试 and Visualizer可视化
Buggy Code 指的是有错误的或者有缺陷的计算机程序代码。
icon图标
import java.util.Scanner;
public class DateToDay {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int y = Integer.parseInt(input);
input = scanner.nextLine();
int m = Integer.parseInt(input);
input = scanner.nextLine();
int d = Integer.parseInt(input);
int a = y – (14 – m) / 12;
int b = a + a/4 – a/100 + a/400;
int c = m + 12 * ((14 – m) / 12) - 2;
int day = (d + b + 31 * c / 12) % 7;
System.out.println("It's day " + day + " !");
}
}
import java.util.Scanner;
public class DateToDay {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int y = scanner.nextInt();
int m = scanner.nextInt();
int d = scanner.nextInt();
int a = y - (14 - m) / 12;
int b = a + a / 4 - a / 100 + a / 400;
int c = m + 12 * ((14 - m) / 12) - 2;
int day = (d + b + 31 * c / 12) % 7;
System.out.println("It's day " + day + " !");
}
}