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)