一·数学函数
1.三角函数方法
sin(double a)
cos(double a)
tan(double a)
acos(double a)
asin(double a)
atan(double a)
Examples:
Math.sin(0) returns 0.0
Math.sin(Math.PI / 6) returns 0.5
Math.sin(Math.PI / 2) returns 1.0
Math.cos(0) returns 1.0
Math.cos(Math.PI / 6) returns 0.866
Math.cos(Math.PI / 2) returns 0
2.指数函数方法
exp(double a)
Returns e raised to the power of a.
log(double a)
Returns the natural logarithm of a.
log10(double a)
Returns the 10-based logarithm of a.
pow(double a, double b)
Returns a raised to the power of b. (a的b次方)
sqrt(double a)
Returns the square root of a. (根号a)
Examples:
Math.exp(1) returns 2.71
Math.log(2.71) returns 1.0
Math.pow(2, 3) returns 8.0
Math.pow(3, 2) returns 9.0
Math.pow(3.5, 2.5) returns 22.91765
Math.sqrt(4) returns 2.0
Math.sqrt(10.5) returns 3.24
3.取整方法
x取向上离它最接近的整数 ,这个整数将以一个 double类型的值返回。
x取向下离它最接近的整数, 这个整数将以一个double 类型的值返回。
x取离它最接近的整数。如果 x 距离两个整数同样接近,就返回成偶数的double类型值。
返回 (int)Math.floor(x+0.5)。
返回 (long)Math.floor(x+0.5)。
max(a, b) and min(a, b)
Returns the maximum or minimum of two parameters. (返回最大最小值)
abs(a)
Returns the absolute value of the parameter.(返回绝对值)
random()
Returns a random double value in the range [0.0, 1.0). (随机生存一个0.0--1.0的小数字)
Examples:
Math.max(2, 3) returns 3
Math.max(2.5, 3) returns 3.0
Math.min(2.5, 3.6) returns 2.5
Math.abs(-2) returns 2
Math.abs(-2.1) returns 2.1
(int)(Math.random()*10) 随机生成0-9的数字
二·字符
char zi='a';
三.字符串
1.概念
String (字符串)的数据类型。例如:
String message = "Welcome to Java";
String message = new String("Welcome to Java");
String 与 System 类和 Scanner 类一样,都是 Java 库中一个预定义的类。String类型不是基本类型,而是引用类型(reference type)。使用引用类型声明的变量称为引用变量,它引用一个对象。这里,message 是一个引用变量,它引用一个内容为 Welcome to Java 的字符串对象。
Scanner input = new Scanner(System.in);
System.out.print("Enter three words separated by spaces: ");
2.字符连接
String s3 = s1.concat(s2); 或者String s3 = s1 + s2;
String message = "Welcome " + "to " + "Java";
String s = "Chapter" + 2=Chapter2;
String s1 = "Supplement" + 'B'=SupplementB;
3.输入
string s=input.next();
string s=input.nextLine();
注意next()接收字符串输入,遇到空格就结束,要接收多个空白符隔开的字符串,就需要多个next()方法;
而nextLine()可以获取完整一行的字符串,遇到换行符结束;nextLine() 不要紧接其他输入方法使用,例如nextInt()只读取一个整数,会留下一个换行符,而nextLine()从上次信息开始读,读完换行符就以为按下空格了过早输入结束。
4.输出
System.out.printf(format, items);
列如:System.out.printf("Random value %f\n", Math.random());
%b a boolean value
%c 字符'a'
%d 整数 200
%f 浮点数 45.460000
%e 科学计数法 4.556000e+01
%s 字符串 "Java is cool"
默认情况下, 输出是右对齐的。 可以在格式标识符中放一个负号(- ), 表明该条目在特定区域中的输出是左对齐的。
比如%-10d
%10.2f
输出的浮点条目宽度至少为 10, 包括小数点和小数点后两位数字(按照四舍五入规则)。 这样, 给小数点前分配了 7 位数字。如果该条目小数点前的位数小于 7, 就在数字前面加空格加宽度 。
如果一个条目需要比指定宽度更多的空间, 宽度自动增加。