java程序设计基础篇 复习笔记 第二单元
1
原始数据类型(primitive data type) == 基本类型 (fundamental type)
byte short int long float double char boolean
引用类型 reference type
2
System.in System.out
java.util.Scanner
Scanner input = new Scanner (System.in);
nextByte()
nextShort()
nextInt()
nextLong()
nextFloat()
nextDouble()
next()
nextLine()
3
标识符
标识符必须是字母数字下划线和$组成的字符序列,习惯上,$只用在机器自动产生的源代码中
不能以数字开头,
不能是保留字,
不可以是true,false,null,
可以为任意长度
区分大小写
4
定名常量 named constant
final datatype CONSTANTNAME = value;
必须在同一条语句中声明和赋值
5
overflow: int value = 2147483647 + 1; int value2 = -2147483648 - 1;
java不会报关于上溢的警告或错误
underflow: java近似认为是0
6
%: 只有当被除数是负数时,余数是负数
7
数值直接量 literal
整型直接量
long 加L或l
浮点型直接量
double 加D或d
float F或f
科学计数法 e或E 12.3e+2 == 12.3e2
8
long System.currentTimeMillis() 返回格林尼治时间GMT 1,1,1970 0:00(UNIX时间戳)到当前时间的毫秒数
9
简介赋值运算符
10
数值类型转换
拓宽类型 widening a type
缩窄类型 narrowing a type
11
Math.pow(base,times)
12
encoding
encoding scheme
char 16
整型转换成char型发生截断
byte需显式转换
Unicode
Unicode Consortium
supplementary character
\u0000-\uFFFF,必须提供4个16位进制数位
如果没有安装中文字体看不到中文字符
13
Escape character 转义字符
14
不要在nextByte(),nextint()等后面使用nextLine()
15
java文档注释
/**开始 */结尾
二元运算符两边应各加一个空格
16
块的风格:
次行 next-line
行尾 end-of-line
17
java 不能从编译错误检测到除0
调试工具
18
javax.swing.JOptionPane.showInputDialog(null,title,message,ircon);
19
int intValue=Integer.parseInt(intString);
输入的如果不是数值或者点击了Cancel都会出现runtime error
Integer类和Double类都包含在java.lang包中
20
'a'-'c'=-2
显示int值
21
java -cp exercise8e.zip Exercise2_1
22
java.util.Scanner
Keyword:
algorithm
assignment operator
assignment statement
backslash(\)
byte type
casting
char type
constant
data type
debugger
debugging
declaration
decrement operator
double type
encoding
final
float type
floating-point number
expression
identifier
increment operator
incremental developing and testing
indentation
int type
literal
logic error
long type
narrowing
operator
overflow
pseudocode
primitive data type
runtime error
syntax error
supplementary Unicode
short type
underflow
Unicode
UNIX epoch
variable
widening
whitespace
习题
2.1
合法:applet,Applet,$4,apps,x,y,radius
关键字:class,public,int
2.2
double miles=100;
final double KILOMETERS_PER_MILE=1.09
double kilometers=miles*KILOMETERS_PER_MILE;
System.out.println(kilometers);
109
2.3
增加可读性,防止改变常量值,易于维护
final int SIZE=20;
2.4
5
15
50
1
6.5
-4.5
2.5
2
-2
-1
4
0
1
2.6
thursday
2.7
byte -128 - 127 8
short -32768 - 32767 16
int -2147483648 - 2147483647 32
long -9,223,372,036,854,775,808 - 92233720369547758807
float +-(1.4E-45 - 3.4028235E+38) 32
double +-(4.9E-324 - 1.797 693 134 862 315 7E+308)
2.8
6
25.0/4
2.9
从计算机的角度都正确
2.10
4/3.0/(r+34)-9*(a+bc)+(double)(3+d*(2+a))/(a+b*d)
2.11
(double)(m*r*r)
m*Math.pow(r,2);
2.12
(2)(3)
2.13
12.3,12.3e+2,23.4e-2,-334.4,20,39F,40D
2.14
2:public static void
4:int k = 100;
7:and 改为 +
2.15
int currentMinute = (int)(System.currentMillis()%36000000/60000*60)
2.16
可以发生自动转换时可以,但是float和int,long和double运算时则需要显式转换
2.17
发生截断 truncation
改变
2.18
12.5
12
2.19
49
65
66
97
98
(
;
O
U
Z
@
Z
q
r
z
2.20
'1','\u3fFa','\b'
2.21
\\
\"
2.23
char c = 'A';
i = (int)c;//i=67
float f = 1000.34f;
int i = (int)f;//变量重复声明,1000
double d = 1000.34;
int i = (int)d;//1000
int i = 97;
char c = (char)i;//'c'
2.24
'b'
'c'
-2
2.25
11
11
111
12
111
2.26
1Welcome11
1Welcome2
1Welcome2
1Welcomea1
2.27
常量: MAX_VALUE
类或接口: Test
常量或方法名: read,readInt
2.28
.............
2.29
语法错误: 语法不合规范,编译器报错
运行错误: 运行过程中报出的错误,影响程序正常运行
逻辑错误: 编程内容出现错误,指令不能达到应有效果
2.30
JOptionPane 是 javax.swing包里的,不是默认导入的
Math类则是java.lang包内的,默认导入
2.31
int i = Integer.parseInt(javax.swing.JOptionPane.showInputDialog(null,"请输入一个整数"));
编程练习:
2.9
public class Exercise2-9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String money = javax.swing.JOptionPane.showInputDialog(null,"请输入钱数");
int ind=money.indexOf('.');
String penny = money.substring(ind+1);
money=money.substring(0, ind);
int i=Integer.parseUnsignedInt(money,10)*100+Integer.parseUnsignedInt(penny);
javax.swing.JOptionPane.showMessageDialog(null,"answer"+i+"penny","result",JOptionPane.INFORMATION_MESSAGE);
}
}
2.18
public class Exercise {
public static void print(String a){
System.out.printf("%-10s",a);
}
public static void main(String[] args){
print("a");print("b");print("pow(a,b)");System.out.println();
for(int i=1;i<6;i++){
String s = ""+i;
print(s);
s = String.valueOf(i+1);
print(s);
s = ""+(int)Math.pow(i, i+1);
print(s);System.out.println();
}
}
}
2.25
import java.util.Scanner;
public class Exercise {
public static void main(String[] args){
System.out.print("Enter the time zone offset to GMT:");
Scanner scanner = new Scanner(System.in);
int i=scanner.nextInt();
long second=System.currentTimeMillis()/1000-3600*i;
int sec = (int)(second%60);
second/=60;
int min = (int)second%60;
second/=60;
int hour = (int)second%24;
System.out.print("The current time is "+hour+":"+min+":"+sec);
}
}
java程序设计基础篇 复习笔记 第二单元的更多相关文章
- java程序设计基础篇 复习笔记 第一单元
java语言程序设计基础篇笔记1. 几种有名的语言COBOL:商业应用FORTRAN:数学运算BASIC:易学易用Visual Basic,Delphi:图形用户界面C:汇编语言的强大功能和易学性,可 ...
- java程序设计基础篇 复习笔记 第七单元&&第八单元
7.1 int[][] triArray{ {1}, {1,2}, {1,2,3}, }; 7.2 array[2].length 8.1 Unified Modeling Language:UML ...
- java程序设计基础篇 复习笔记 第六单元
第六章 一维数组 1 数组初始化语法 array initializer 2 for each loop 3 off-by-one error 通常是在循环中该使用<的地方使用了<= 4 ...
- java程序设计基础篇 复习笔记 第五单元
1. method header: modifier, return value type, method signature(method name, parameter) method body ...
- java程序设计基础篇 复习笔记 第三单元
1 单向if语句 双向if语句 dangling else switch:char,byte,short,int 2 javax.swing.JOptionPane.showConfirmDialog ...
- java程序设计基础篇 复习笔记 第四单元
1 think before coding code incrementally 2 sentinel value sentinel-controlled loop 3 输入输出重定向 > &l ...
- Java程序设计基础作业目录(作业笔记)
持续更新中............. Java程序设计基础笔记 • [目录] 我的大学笔记>>> 第1章 初识Java>>> 1.1.4 学生成绩等级流程图练习 1 ...
- Java程序设计基础笔记 • 【目录】
持续更新中- 我的大学笔记>>> 章节 内容 实践练习 Java程序设计基础作业目录(作业笔记) 第1章 Java程序设计基础笔记 • [第1章 初识Java] 第2章 Java程序 ...
- 20145322第九周JAVA程序设计基础学习总结
20145322第九周JAVA程序设计基础学习总结 JDBC简介 JDBC全名Java DataBase Connectivity,是java联机数据库的标准规范.它定义一组标准类与接口,应用程序需要 ...
随机推荐
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League
地址:http://codeforces.com/contest/782/problem/D 题目: D. Innokenty and a Football League time limit per ...
- bash 获取时间段内的日志内容
需求,获取时段内的/var/log/messages文件内出现错误的消息,支持多行的消息,支持天,小时分钟,秒级的区间,可以修改监控的日志对象 #!/bin/bash if [ $# != 1 ] ; ...
- Maven的plugins、pluginManagement和dependencies、dependencyManagement
plugins和dependencies下边配的都是真实使用的. pluginManagement和dependencyManagement下边配的都只是做声明的,一般配置在顶级pom中. 参考链接: ...
- python3_unittest单元测试框架
看见英文懵逼,强迫学习英语 The Unittest suppots test automation,sharing of setup and shutdown code of tests, aggr ...
- mongodb研究(mongodb 内存数据库)
本日志大部分都不是原创的转载复制的会带链接保持版权 工作中使用mongodb已经好久了,讽刺的是到了最后快离职的时候才有时间好好研究下源码. 印象:mongodb是一个内存数据库,数据都是放到内存 ...
- 20145314郑凯杰 《Java程序设计》第2周学习总结 代码开始!
---恢复内容开始--- 20145314郑凯杰 <Java程序设计>第2周学习总结 代码开始! 教材学习内容总结 跟着教材的顺序开始总结我学过的内容: 1编辑.编译.运行教材上代码 这部 ...
- Linux命令awk
1.简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大. 简单来说awk就是把文件逐行读入,默认以空格为分隔符将每行切片,切开的部 ...
- [BZOJ2091]The Minima Game
Description 给出N个正整数,AB两个人轮流取数,A先取.每次可以取任意多个数,直到N个数都被取走.每次获得的得分为取的数中的最小值,A和B的策略都是尽可能使得自己的得分减去对手的得分更大. ...
- unix_timestamp() 和 from_unixtime()
unix_timestamp() 将时间转换为时间戳.(date 类型数据转换成 timestamp 形式整数) select unix_timestamp('2016-03-23 11:10:10' ...
- mathematical method
mathematical method 曲线拟合 指数 \(lnY = lna + bX\) 对数 \(Y = blnX + a\) 幂函数 \(lgY=lga+blgX\) 多元线性回归模型 回归分 ...