基本程序设计
编写程序涉及到如何解决问题的策略,以及如何应用编程语言实现这个策略
- 问题分析 2. 本质抽取 3. 程序设计 4. 语言实现
标识符遵循规则
- 标识符是由【字母、数字、下划线、美元符号$】组成的字符序列
- 标识符不能以【数字】为开头
- 标识符不能是保留字
- 标识符不能是【true、false、null】
- 标识符可以是任意长度
变量 - 程序可操作存储区的名称
每个变量都有自己的特殊类型,所谓的变量声明就是告知编译器申请一块指定类型的内存来存储数据。
变量的名称就是内存地址字面化的表达。
任何时候,都要尽可能一步完成变量的声明和赋初值。这会使得程序易读,同时避免程序设计错误。
变量在程序执行过程之中可以转变
常量 - 数值固定不变的量
final datatype CONSTANTNAME = value;
常量必须在同一条语句中声明和赋值
大写常量中的所有字母,两个单词之间使用下划线
_连接
private static final double TIME_CONVERSION_UNIT = 60;
数值数据类型和操作
byte(8bit) => short(16bit) => int(32bit) => long(64bit) => float(32bit) => double(64bit)
数值操作符
| 运算符 | 实例 |
|---|---|
+ |
1+2=3 |
- |
1-2=-1 |
* |
1*2=2 |
/ |
1 / 2 = 0 |
/ |
1 / 2.0 = 0.5 |
% |
10 / 3 = 1 |
/即整除,除法结果只保留整数部分,小数部分丢去。
int a = 5 / 2; // a = 2 不是 2.5
为了实现浮点数的除法,要保证除数或被除数至少有一个是浮点数
int a = 5.0 / 2; // a = 2.5
int b = 5 / 2.0; // b = 2.5
int c = 5.0 / 2.0; // c = 2.5
⚠️ 取模/求余操作要警惕
int numA = numB % numC;
- numB: 被除数
- numC: 除数
只有被除数是负数其结果才能使负数
-7 % 3 = -1;
-12 % 4 = 0;
20 % -13 = 7;
10 % 3 = 1;
-10 % 3 = -1;
10 % -3 = 1;
10 % 3 = 1;
// 再次验证只有被除数是负数其取模结果才能是负数 ⭐️
时间转换
public class Demo04 {
private static final int TIME_CONVERSION_UNIT = 60;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please input the total seconds: ");
int totalSeconds = scanner.nextInt();
System.out.println("====================================");
System.out.println("minutes: " + (totalSeconds / TIME_CONVERSION_UNIT));
System.out.println("remaining seconds: " + (totalSeconds % TIME_CONVERSION_UNIT));
}
}
输出:
Please input the total seconds:
320
====================================
minutes: 5
remaining seconds: 20
幂运算
Math.pow(a, b); ===> a^b
System.out.println(Math.pow(4, 2)); // 16.0
System.out.println(Math.pow(100, 0.5)); // 10.0
System.out.println(Math.pow(2.5, 2)); // 6.25
System.out.println(Math.pow(2.5, -2)); // 0.16
System.out.println(1 / Math.pow(2.5, 2)); // 0.16
// Math.pow(2.5, -2) = (1.0 / Math.pow(2.5, 2))
课后习题
int numA = 5 % 1; // numA = 0
int numB = 1 % 5; // numB = 1 ⭐️
System.out.println("100 + 20" + 20 + 20 ); // 100 + 202020
System.out.println("100 + 20" + 20 / 20); // 100 + 201
System.out.println(20 + 20 + "100 + 20"); // 40100 + 20
System.out.println("25 / 4 is " + 25 / 4); // 25 / 4 is 6
System.out.println("25 / 4.0 is " + 25 / 4.0); // 25 / 4.0 is 6.25 ⭐️
System.out.println("3 * 2 / 4 is " + 3 * 2 / 4); // 3 * 2 / 4 is 1
System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4); // 3.0 * 2 / 4 is 1.5
直接量
一个直接量(literal)就是程序中直接出现的常量值
public static void main(String[] args) {
/*
* 默认情况下,整型直接量是一个十进制整数
* 二进制整数直接量,使用【0b(B)】开头
* 八进制整数直接量,使用【0】开头
* 十六进制整数直接量,使用【0X】开头
* */
System.out.println(0B1111); // displays 15
System.out.println(07777); // displays 4095
System.out.println(0XFFFF); // displays 65535
System.out.println(125L); // type is long
System.out.println(125l); // type is long
System.out.println(123.23F); // type is float
System.out.println(123.23f); // type is float
/*
* 整型直接量默认为:int
* 浮点型直接量默认为:float
* */
}
为了提高可读性(readability),Java允许在数值直接量的两个数字之间添加下划线
int numA = 521_521_521; // ✔️
long numB = 232_231_812L; // ✔️
int numC = _45; // ❌
int numD = 45_; // ❌ 下划线必须在两个数字之间
int numF = 4_5; // ✅
double numE = 231_123_34.231_123_34; // ✔️
课后习题:将华氏温度转换为摄氏温度
public class Demo04 {
private static final double COEFFICIENT01 = 0.5555556;
private static final int COEFFICIENT02 = 32;
public static double fahrenheitToCelsius(double degreeInFahrenheit){
return COEFFICIENT01 * (degreeInFahrenheit - COEFFICIENT02);
}
public static double getEstimatedNum(double num, int scale){
return Math.round(num * Math.pow(10, 2)) / Math.pow(10, 2);
}
// the privatization of constructor(which represent all of methods in the class is prefixed with "public static"
private Demo04(){}
public static void main(String[] args) {
System.out.println(getEstimatedNum(fahrenheitToCelsius(100), 2)); // 37.78
}
}
System.currentTimeMills()获得一个long类型的以毫秒为单位的当前时间值。
i = i ++is equlas tobullshit
数值类型转换
- widening a type is auto operation
- narrowing a type is compelled operation
System.out.println((int)1.7); // 1
System.out.println((double)1/2); // 0.5
软件开发过程
4个阶段:
系统规划:系统是什么和做什么
软件要处理的问题和软件系统需要做的详细记录到文档中。(涉及到用户和开发者之间的紧密的接触)。
系统分析:系统具体做什么
系统分析主要是分析数据流并确定系统的输入和输出。
系统设计:系统怎么做
系统设计是设计一个从输入获得输出的过程。此阶段涉及到多层抽象,将问题分解为可管理的组成部分,并且设计执行每个组成部分的策略。
系统分析和设计的本质: 输入、处理和输出(IPO)
系统实施&维护
将系统设计翻译成程序。为每个组成部分编写独立的程序进行测试(单元测试、系统测试、集成测试、验收测试)=》打包=》部署
典例:找零计算《Java基础程序设计》(P55)
/**
* ClassName: Demo05
* Function:
* Date: 2021/2/9 9:13 上午
* author Wangzhuang2
* version V1.0
* TODO:
*/
public class Demo05 {
private static final int MONEY_CONVERSION_UNIT = 10;
private Demo05(){} // the privatization of constructor method that represent all of the methods in the class have to be prefixed by "public static"
public static void main(String[] args) {
client();
}
public static void client(){
Scanner scanner = new Scanner(System.in);
System.out.println("please input the amount of change: ");
double amount = scanner.nextDouble();
System.out.println(processMoney(amount));
}
/**
* process the representation of money
*
* @param amount the total amount of change
* */
public static String processMoney(double amount){
int temp = (int)(amount * Math.pow(10, 2));
int yuan = temp / 100;
int jiao = temp % 100 / 10 ;
int fen = temp % 10;
return "找零:" + yuan + "元 " + jiao + "角 " + fen + "分 ";
}
}
/**
* 1156 % 10 = 6 保尾
* 1156 / 10 = 115 报头
*
* 1156 % 100 = 56 保尾
* 1156 / 100 = 11 保头
* */
常见的错误和陷阱
- 为声明、未初始化的变量参与计算
double interestRate = 0.05;
double totalAmount = interstrate * 45; // 此时 interestrate变为事先声明和初始化 interstrate 和 interestRate 完全是两个不同的变量 (Java是大小写敏感的)
- 不要出现任何冗余变量(即重复定义初始化、从未使用都应该从程序中去除)
public static void printItem(double money){
double amount = 100; // 从未使用的变量
double temp = money; // 冗余的临时变量
double result = temp * 100;
}
- 整数溢出问题
【MIN_VALUE = MAX_VALUE + 1】 【MAX_VALUE = MIN_VALUE - 1】
// 整数 int
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648
System.out.println(Integer.MAX_VALUE + 1); // -2147483648
System.out.println(Integer.MIN_VALUE - 1); // 2147483647
// 浮点数double
System.out.println(Double.MAX_VALUE); // 1.7976931348623157E308
System.out.println(Double.MAX_VALUE + 1); // 1.7976931348623157E308
System.out.println(Double.MAX_VALUE + 100); // 1.7976931348623157E308
System.out.println(Double.MIN_VALUE); // 4.9E-324
System.out.println(Double.MIN_VALUE - 1); // -1.0
浮点数很小会引起向下溢出。Java会将其近似为0,一般不用考虑向下溢出问题。
浮点数最大值达到最大值时超出的部分将不在纳入其中直接以最大值进行显示!
- 取整错误
凡是涉及到浮点数之间的计算都是近似的,因为这些数没有以准确的精度来存储!
System.out.println(1.0 - 0.9); // 0.09999999999999998
- 在Java中除法分为 【整除】和【浮点除】
-
整除:保证参与计算的被除数和除数之间都是整数
-
浮点除:保证参与计算的被除数和除数之间至少存在一个浮点数
// 整除
System.out.println(5 / 2); // 2
// 浮点除
System.out.println(5.0 / 2); // 2.5
System.out.println(5 / 2.0); // 2.5
System.out.println(5.0 / 2.0); // 2.5
关键术语
算法: algorithm 向下转型:narrowing of type 赋值操作符:assignment operator 操作数: operands 操作数:operator
上溢:overflow 下溢:underflow 类型转换:casting 常量:constant 后自减:postdecrement 后自增:postincrement
前自减:predecrement 前自增:preincrement 数据类型: data type 声明变量:declare variables 基本数据类型:primitive data type
表达式:expression 需求规范:requirement specification 变量范围:scope of variables 明确导入:specific import
系统分析:system analysis 系统设计: system design 标识符:identifier 输入-处理-输出(Input-Process-Output)
直接量:literal 通配符导入:wildcard import

浙公网安备 33010602011771号