一、Math类有哪些常用方法
Math定义:
public final class Math
Math类包含执行基本数字运算的方法,如基本指数,对数,平方根和三角函数。
Math里面的变量和方法都是静态的,因此是不需要对象访问,通过类型就可以直接调用
static double | E double值比其他任何一个都更接近 e ,自然对数的基数 |
---|---|
static double | PI double值比任何其他的更接近 pi ,圆周长与其直径的比率 |
static long | round(double a) 返回参数中最接近的 long ,其中 long四舍五入为正无穷大。 【注意负数】 |
---|---|
static int | round(float a) 返回参数中最接近的 int ,其中 int四舍五入为正无穷大。【注意负数】 |
static double | random() 返回值为 double值为正号,大于等于 0.0 ,小于 1.0随机数 |
例如:
package com.wangxing.test1;
public class MathTest {
public static void main(String[] args) {
System.out.println("自然常数=="+Math.E);
System.out.println("圆周率=="+Math.PI);
/*
static long round(double a)
返回参数中最接近的 long ,其中 long四舍五入为正无穷大。
static int round(float a)
返回参数中最接近的 int ,其中 int四舍五入为正无穷大。
*/
System.out.println("round(11.5)=="+Math.round(11.5));
System.out.println("round(-11.5)=="+Math.round(-11.8));
//A 12,-12 B 11,-11 C 12,-11 D 11,-12
//static double random() 返回值为 double值为正号,大于等于 0.0 ,小于 1.0随机数 。
System.out.println("random()=="+Math.random());
//得到一个1--10之间的随机数
int num=((int)(Math.random()*10))+1;
System.out.println("num=="+num);
if(num>7) {
System.out.println("中奖");
}else {
System.out.println("没有中奖");
}
}
}
二、Scanner类的用法
Scanner是在在JDK1.5的时候被新增进入的
Scanner从控制台获取输入的数据值。
常用的构造方法:
Scanner(InputStream source)
通过一个字节输入流对象创建Scanner对象
参数–InputStream source【字节输入流对象–读取数据】
用System.in–字节输入流对象【标准”输入流–键盘输入】
创建对象:
Scanner s = new Scanner(System.in);
String | next() 接收String |
---|---|
boolean | nextBoolean()接收boolean[true/false] |
byte | nextByte() 接收byte |
double | nextDouble() 接收 double |
float | nextFloat() 接收float |
int | nextInt() 接收 int |
long | nextLong() 接收 long |
short | nextShort() 接收short |
String | nextLine() 接收一行String |
例如:
package com.wangxing.test1;
import java.util.Scanner;
public class ScannertTest1 {
public static void main(String[] args) {
//创建Scanner类对象
Scanner input=new Scanner(System.in);
System.out.println("请输入学生编号:");
int id=input.nextInt();
System.out.println("请输入学生姓名:");
String name=input.next();
System.out.println("请输入学生身高:");
double height=input.nextDouble();
System.out.println("是否毕业:");
boolean biye=input.nextBoolean();
System.out.println("你录入的学生信息是:");
System.out.println(id+"--"+name+"--"+height+"--"+biye);
}
}
三、什么是正则表达式,如何创建,如何使用
正则表达式,又称规则表达式。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。
匹配正则表达式
(1)1. String类的matchs匹配正则表达式
package com.wangxing.test1;
import java.util.Scanner;
public class TestRegex1 {
public static void main(String[] args) {
//创建一个正则表达式
String regex="^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
Scanner read=new Scanner(System.in);
System.out.println();
String phone=read.next();
boolean flag=phone.matches(regex);
if(flag) {
System.out.print("你输入的手机号码合法!!");
}else {
System.out.print("手机号码不合法!!");
}
}
}
(2)java.util.regex 包主要包括以下两个类:
Pattern 类:【创建】
pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数。
Matcher 类:【匹配】
Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。
例如:
package com.wangxing.test1;
import java.util.Scanner;
import java.util.regex.Pattern;
public class TestRegex2 {
public static void main(String[] args) {
Scanner read=new Scanner(System.in);
System.out.println("请输入手机号码");
String phone=read.next();
//创建一个正则表达式
String pattern ="^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
boolean flag=Pattern.matches(pattern, phone);
if(flag) {
System.out.print("你输入的手机号码合法!!");
}else {
System.out.print("手机号码不合法!!");
}
}
}
package com.wangxing.test1;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegex3 {
public static void main(String[] args) {
// 按指定模式在字符串查找
String line = "This order was placed for QT3000! OK?";
String pattern = "(\\D*)(\\d+)(.*)";
// 创建 Pattern 对象
Pattern r = Pattern.compile(pattern);
// 现在创建 matcher 对象
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
System.out.println("Found value: " + m.group(3) );
} else {
System.out.println("NO MATCH");
}
}
}