java笔记1-17

  • Math类

Math类是用于数学计算的一个工具类(对于工具类而言,里面的大部分成员都是静态的static)
自然常量
E:自然对数 PI:圆周率
取整方法:
Math.ceil( ) :向上取整
Math.floor( ) :向下取整
Math.round( ):四舍五入
三角函数:
Double sin(double a):正弦函数 a为弧度值
Double cos(double a):余弦函数
Double tan(double a):正切函数
double toDegrees(double a):将弧度转角度
toRadians(double angles):将角度转弧度
double asin(double a):反正弦函数
double acos(double a):反余弦函数
double atan(double a):反正切函数
指数函数:
Pow(double a, double b) :求a的b次幂
double sqrt(double a):求a的平方根
double cbrt(double a):求a的立方根
其他方法:
static double abs(double a):求a的绝对值
static double max(a,b):返回a和b之间的最大值
static double min(a,b):返回a和b之间的最小值
static double random():返回[0,1)之间的随机小数
Double hypot(double a):返回sqrt( x 2 + y 2 ),
案例:

public class Sample {
public static void main(String[] args) {
System.out.println(Math.E);
System.out.println(Math.PI);
System.out.println(Math.ceil(3.1));
System.out.println(Math.ceil(3.9));
System.out.println(Math.ceil(-3.1));
System.out.println(Math.ceil(-3.9));
System.out.println(Math.floor(3.1));
System.out.println(Math.floor(3.9));
System.out.println(Math.floor(-3.1));
System.out.println(Math.floor(-3.9));
System.out.println(Math.round(3.1));
System.out.println(Math.round(3.9));
System.out.println(Math.round(-3.1));
System.out.println(Math.round(-3.9));
System.out.println(Math.sin(Math.PI/6));
System.out.println(Math.cos(Math.PI/3));
System.out.println(Math.tan(Math.PI/4));
System.out.println(Math.toDegrees(Math.PI/2));
System.out.println(Math.toRadians(90));
System.out.println(Math.cbrt(8));
System.out.println(Math.hypot(0 - 1, 0 - 1));
}
}

- Scanner类:

主要用于负责数据输入的类,底层是和io流相关
String next( ):获取直到遇到空格位置的一个字符串
String nextline( ) 获取直到遇到回车为止的一个字符串
int nextInt():获取下一个整数
byte short long double nextDouble( )
boolean nextBoolean( )
float nextFloat( )

import java.util.Scanner;
public class Sample {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("输入一句话:");
String line = input.nextLine();
System.out.println(line);
System.out.print("输入三个单词:");
String word1 = input.next();
String word2 = input.next();
String word3 = input.next();
System.out.println(word1);
System.out.println(word2);
System.out.println(word3);
}
}

- Random类

主要用于产生随机数
boolean nextBoolean():随机产生布尔类型值
double nextDouble():随机生成0.0~1.0之间的小数
double nextInt():随机生成一个整数0~2 32的整数
double nextInt(n):随机生成一个整数[0,n)的整数

import java.util.Random;
public class Sample {
public static void main(String[] args) {
Random random = new Random();
for (int i = 1; i <= 10; i++) {
System.out.print(random.nextBoolean() + " ");
}
System.out.println();
for (int i = 1; i <= 10; i++) {
System.out.print(random.nextInt(2) + " ");
}
System.out.println();
for (int i = 1; i <= 10; i++) {
System.out.print(random.nextInt() + " ");
}
System.out.println();
}
}

- String类

String是一个类,它描述的是字符串。在Java代码当中,所有字符串常量(字符串字面量)都是 String类的一个实例对象。并且,字符串一旦创建,则不可修改! 不可修改其长度,不可修改其内容 。所 以将来对字符串内容的改变,不能在原地改,只能重新创建一个字符串。

获取:
1.char charAt(int index):获取指定角标index处的字符
2.int indexOf(int ch):获取指定字符(编码)在字符串中第一次(从左到右)出现的地方返回的是角标。如果是-1 表示不存在 int lastIndexOf(int ch):获取指定字符(编码)在字符串中第一次(从右到做)出现的地方返回的 是角标
3.int indexOf(String str):获取指定字符串在本字符串中第一次(从左到右)出现的地方返回的是角标
4.int lastIndexOf(String str):获取指定字符串在本字符串中第一次(从右到左)出现的地方返回 的是角标
5.int length():获取字符串的长度(字符的个数)
6.String[ ] split(String regex):将字符串按照regex的定义进行切割(regex指的是正则表达式)
7.String substring(int beginIndex):截取一段子字符串,从beginIndex开始到结尾
8.String substring(int beginIndex, int endIndex):截取一段子字符串,从beginIndex开始到 endIndex(不包含)
判断语句

  1. int compareTo(String anotherString):按照字典顺序比较两个字符串的大小
  2. boolean contains(String another):判断当前字符串中是否包含指定字符串
  3. another boolean equals(String another):比较当前字符串与指定字符串的内容是否相同
  4. boolean isEmpty():判断当前字符串是否为空,
  5. length() == 0 boolean startsWith(String prefix):判断该字符串是否以prefix开头
  6. boolean endsWith(String suix):判断该字符串是否已suix结尾
    修改相关
    1.String toLowerCase():将字符串中所有的英文字母全部变为小写
    2.String toUpperCase():将字符串中所有的英文字母全部变为大写
    3.String trim():将字符串中两端的多余空格进行删除
    4.String replace(char oldCh,char newCh):将字符串中oldCh字符替换成newCh字符
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值