常用的数学函数
String类
public class StringDemo {
public static void main(String[] args) {
String value = "456";
value += "123"; //相当于调用了 value.concat()方法,会直接返回一个新的字符串空间
String value2 = "456123";//将字符串中的地址赋值给value2
System.out.println(value == value2);
}
}
== 如果地址不同的话就输出flase
判断字符串相等的时候用 字符串1.equals(字符串2)判断相等
Character.isLetter(ch)//判断是否为数字
Character.isLetterOrDigit(ch)// 判断是否为字母或数字
Character.isJavaIdentifierPart(ch)//判断用来定义java变量的单词中,除首字符外,其他字符是否是合法字符
Character.isJavaIdentifierStart(ch)//判断java变量中,第一个字符是否是合法字符
Character.isUpperCase(ch)//判断是否是大写字母
public class StringDemo {
public static void main(String[] args) {
//演示常用的几种String对象的创建方式
String str1 = "你好,世界!";
String str2 = new String();//使用默认构造
String str3 = new String(str1);
char[] chArray = {'a','b','c'};
//将字符数组转换为String对象
String str4 = new String(chArray);
//将字符串转换为字符数组
chArray = str4.toCharArray();
//字节数组与字符串转换
byte[] byteArray = {104,101,108,108,111};
String str5 = new String(byteArray);
System.out.println(str5);
}
}
public class StringDemo {
public static void main(String[] args) {
//trim方法:去掉字符串左右两侧的空格
//建议:在用户输入用户名后使用
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名:");
String uName = input.nextLine().trim();
System.out.println("---" + uName + "---" );
}
}
public class StringDemo {
public static void main(String[] args) {
//indexOf方法
String password = "ab3456abc";
System.out.println(password.indexOf("ab",2));
}
}
2指的是从字符串中下标2以后开始查
判断字符串中的数
public class StringDemo {
public static void main(String[] args) {
//问题:判断字符串中只能有一个小数点,小数点不能在第一位,也不能在最后一位!
StringDemo stringDemo = new StringDemo();
stringDemo.isDecimal("0");
}
//判断传入的字符串是否是一个正确格式的小数
//小数点不能在第一位,也不能在最后一位!
public static boolean isDecimal(String str){
boolean isDecimal = true;
for (int i = 0; i < str.length(); i++) {
if (!(Character.isDefined(str.charAt(i)))){
if(str.charAt(i) == '.'){
if(i == 0 || i == str.length() - 1){//如果小数点在第一位或最后一位,仍然是非法的,返回flase
isDecimal = false;
break;
}
}else {//如果当前数字不是数字并且也不是小数点,那么证明是非法字符,直接返回false
isDecimal = false;
break;
}
}
}
//判断字符串中只有一个小数点
if(!(str.contains(".") && str.indexOf(".") == str.lastIndexOf("."))){
return false;
}
return isDecimal;
}
}
substring
public class StringDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String password = "abc123456";
//[beginIndex,endIndex)左闭右开
System.out.println(password.substring(1,3));
//从下标3开始一直到最后
System.out.println(password.substring(3));
}
}
制作一个日历表输入年份月份显示
public class Demo09 {
//用户输入的年份
public static int year = Integer.MIN_VALUE;
//用户输入的月份
public static int month = Integer.MIN_VALUE;
//对应每个月份的天数
private static int[] dayOfMoth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static void main(String[] args) {
PirntCalendar();
}
public static void PirntCalendar() {
//1.让用户输入年份和月份
InputYearAndMomth();
//2.1900-1-1到用户输入年份的总天数
//2.1.计算各年的总天数
//2.2.计算各月的总天数之和
int sum = getSunDayOfYears();
sum += getSumDayOfMoth();
sum++;
//3.打印年份和月份(英语)
printMothTitle();
//4.打印月份的标题(星期一到星期日)
//5.根据某月1日是星期几,打印月历内容
PrintCalendarContent(sum % 7 );
}
/**
*打印月历的内容
* @param dayOfweek 当前月一号是星期几
*/
private static void PrintCalendarContent(int dayOfweek){
int sepCount = 0;// \t的数量
if(dayOfweek == 0){
sepCount = 6;
}else {
sepCount = dayOfweek - 1;
}
for (int i = 0; i < sepCount; i++) {
System.out.print("\t\t");
}
for (int i = 0; i < dayOfMoth[month-1]; i++) {
System.out.print(i + 1);
if((dayOfweek + i) % 7 == 0){
//周日
System.out.println();
}else{
System.out.print("\t\t");
}
}
}
private static void printMothTitle(){
String[] monthNames = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
System.out.println("\t" + "\t" +year + "\t" + monthNames[month-1]);
String[] weekdays = {"月曜日","火曜日","水耀日","木曜日","金曜日","土曜日","日耀日"};
for (int i = 0; i < weekdays.length; i++) {
System.out.print(weekdays[i] + "\t");
}
System.out.println();
}
/**
*获得1900到year的总天数
* @return
*/
public static int getSunDayOfYears(){
if(year == Integer.MIN_VALUE){
System.out.println("年份错误,请重新输入年份和月份");
InputYearAndMomth();
}
int sum = 0;
for (int i = 0; i < year-1900; i++) {
sum +=365;
if(isLeaYear(i)){
sum++;
}
}
return sum;
}
/**
* 得到year年一月一号到year年month-1月最后一天的总天数
* @return
*/
private static int getSumDayOfMoth(){
int sum = 0;
for (int i = 0; i < month - 1; i++) {
sum +=dayOfMoth[i];
}
if(isLeaYear(year) && month >= 3){
sum++;
}
return sum;
}
/**
* 用来判断传入的year是不是闰年
* @param year 要判断的年份
* @return 是闰年返回true
*/
private static boolean isLeaYear(int year){
return year % 400 == 0 ||year % 4 == 0 && year%100 !=0;
}
/**
* 接收用户输入的年份和月份
*/
private static void InputYearAndMomth(){
Scanner input = new Scanner(System.in);
System.out.print("请输入年份:");
year = input.nextInt();
System.out.print("请输入月份:");
month = input.nextInt();
input.close();
input = null;
}
}
密码验证
package com.zhang.uererdemo;
import java.util.Scanner;
public class UserMain {
public static void main(String[] args) {
//实现用户注册
Scanner input = new Scanner(System.in);
// System.out.print("请输入用户名:");
// String userName = input.next();
System.out.print("请输入密码:");
String password = input.next();
System.out.println(StringUtil.validatePassWore(password));
//
}
}
public class User {
private String userName;
private String password;
private String email;
public User() {
super();
}
public User(String userName , String password , String email){
setUserName(userName);
setPassword(password);
setEmail(email);
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
//字符串工具类
public class StringUtil {
public static boolean isCorrectUserName(String userName){
boolean isCorrect = true;
//判断用户输入的用户名是否是合法的用户名
if(userName == null) return false;
if(userName.length() > 25 )return false;
char[] unValidChar = {' ', '\'' , '"' , '?' ,'<' , '>' , ','};
for (char ch : unValidChar){
if(userName.contains(Character.toString(ch))){
isCorrect = false;
break;
}
}
return isCorrect;
}
public static String validatePassWore(String passWord){
String power = "";//密码强度,如果返回值为"",那么密码验证失败,验证成功则返回星号表示密码强度
if(passWord == null) return power;
if(passWord.length() < 6 || passWord.length() >18) return power;
//String[] correctPart = {"_" , "@" , "#" , "$" , "!" };//合法的组成部分
String part = "_@#$!";
for (int i = 0; i < passWord.length(); i++) {
if(!Character.isLetterOrDigit(passWord.charAt(i))){
//如果不是字母或数字,并且也不是合法的特殊符号,就证明是非法格式
if(!part.contains(Character.toString(passWord.charAt(i)))){
return power;
}
}
}
//判断密码强度
if(isDigit(passWord) || isLetter(passWord)){
power = "◆◆◇◇◇◇";
}else if(isDigitAndLetter(passWord)){
power = "◆◆◆◆◇◇";
}else if(isContainsSymble(passWord)){
power = "◆◆◆◆◆◆";
}
return power;
}
// private static boolean isDigitAndLetterAndSymble(String value){
// //是否包含数字字母特殊符号
// for (int i = 0; i < value.length(); i++) {
// if(!(Character.isDigit(value.charAt(i)) ||
// Character.isLetter(value.charAt(i)) ||
// "_@#$!".contains(Character.toString(value.charAt(i))))){
// return false;
// }
//
// }
// return true;
// }
//字符串中包含和合法的字母和数字
private static boolean isDigitAndLetter(String value){
for (int i = 0; i < value.length(); i++) {
if(!Character.isLetterOrDigit(value.charAt(i)) ){
return false;
}
}
return true;
}
//判断一个数字是不是纯数字
private static boolean isDigit(String value){
for (int i = 0; i < value.length(); i++) {
if(!Character.isDigit(value.charAt(i))){
return false;
}
}
return true;
}
private static boolean isLetter(String value){
for (int i = 0; i < value.length(); i++) {
if(!Character.isLetter(value.charAt(i))){
return false;
}
}
return true;
}
//判断字符串中是否包含特殊符号
private static boolean isContainsSymble(String value){
String part = "_@#$!";
for (int i = 0; i < value.length(); i++) {
if(!part.contains(Character.toString(value.charAt(i)))){
return true;
}
}
return false;
}
}
StringBuffer /StringBuilder
带缓存的字符串
public class StringBuff {
public static void main(String[] args) {
//加强版字符串
String str1 = "a";
String str2 = "b";
String str3 = str1 + str2;
//底层实现
String str4 = new StringBuffer(String.valueOf(str1)).append(str2).toString();
}
}
与String类不同的是,StringBuffer和StringBuilder类的对象能够被多次修改,并且不产生新的未使用的对象。
public class StringBuff {
public static void main(String[] args) {
final int N = 500_000;//十万
long starTime = System.currentTimeMillis();
String str = "*";
for (int i = 0; i < N; i++) {
str += "*";
}
long endTime = System.currentTimeMillis();
System.out.println("+=用时:" + (endTime - starTime) + "毫秒");
//使用StringBuff进行拼接
starTime = System.currentTimeMillis();
StringBuffer str1 = new StringBuffer("*");
for (int i = 0; i < N; i++) {
str1.append("*");
}
endTime = System.currentTimeMillis();
System.out.println("StringBuff的append用时:" + (endTime - starTime) + "毫秒");
//StringBuilder
starTime = System.currentTimeMillis();
StringBuilder str2 = new StringBuilder("*");
for (int i = 0; i < N; i++) {
str2.append("*");
}
endTime = System.currentTimeMillis();
System.out.println("StringBuilder的append用时:" + (endTime - starTime) + "毫秒");
}
}
public class StringBuff {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("你好,世界");
StringBuffer stringBuffer1 = new StringBuffer("你好,世界");
StringBuffer stringBuffer2 = new StringBuffer("你好,世界");
StringBuffer stringBuffer3 = new StringBuffer("你好,世界");
System.out.println(stringBuffer.reverse());//反转字符串
//插入 在规定下标处插入元素
stringBuffer1.insert(3,"abc");
System.out.println(stringBuffer1);
stringBuffer2.deleteCharAt(3);
System.out.println(stringBuffer2);
stringBuffer3.delete(2,4); //范围 [2,4) 之间的元素
System.out.println(stringBuffer3);
stringBuffer1.replace(2,4,"北京");
System.out.println(stringBuffer1);
System.out.println("缓存大小:" + stringBuffer.capacity());
stringBuffer.setLength(0);//相当于清空了字符串对象
//
stringBuffer.trimToSize();
}
}
商品管理
public class ProdectBiz {
public static void main(String[] args) {
//用来保存每种商品的总量
int[] counts = new int[3];
double total = 0; //最终要支付的总金额
Preduct[] preducts = new Preduct[3];
for (int i = 0; i < counts.length; i++) {
preducts[i] = new Preduct();
String name = JOptionPane.showInputDialog("请输入商品名称:");
preducts[i] .setName(name);
//接收用户输入的商品单价,要注意:返回值是字符串类型!
String strPrice = JOptionPane.showInputDialog("请输入商品的单价:");
//需要将字符串类型转换为double类型,在进行赋值
preducts[i] .setPrice(Double.parseDouble(strPrice));
String strCount = JOptionPane.showInputDialog("请输入购买的数量");
counts[i] = Integer.parseInt(strCount);
//累加总金额
total += preducts[i] .getPrice() * counts[i];
}
System.out.println("购物结算:");
for (int i = 0; i < preducts.length; i++) {
System.out.println(preducts[i].getName() + "\t" + preducts[i].getPrice());
}
System.out.println("商品的总金额" + total);
}
}
public class Preduct {
private String name;
private double price;
//商品的描述
private String description;
//商品的服务
private String[] services;
public Preduct(){
}
public Preduct(String name , String description){
setName(name);
setDescription(description);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String[] getServices() {
return services;
}
public void setServices(String[] services) {
this.services = services;
}
}