package apiDemo;
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个小数");
double v = sc.nextDouble();
System.out.println(v);
}
}
package stringDemo;
public class StringDemo1 {
public static void main(String[] args) {
//1、直接赋值
String s1 = "abc";
System.out.println(s1);
//2、使用new的方式创建
//空参构造:一个空白的字符串对象
String s2 = new String();
System.out.println("q" + s2 + "r");
//传递一个字符数组,根据字符数组的内容再创建一个新的字符串对象
//需求:我要修改字符串的内容 abc - Qbc
//就可以通过创建一个字符数组,通过修改字符数组里面的元素,进而修改字符串
char[] chs = {'a', 'b', 'c', 'd'};
String s3 = new String(chs);
System.out.println(s3);
//传递一个字节数组,根据字节数组的内容在创建一个新的字符串对象
//应用场景:以后在网络当中传输的数据其实都是字节信息
//我们一般要把字节信息进行转换,此时就需要用到这个构造了
byte[] bytes = {97, 98, 99, 100};
String s4 = new String(bytes);
System.out.println(s4);
}
}
package stringDemo;
import java.util.Scanner;
public class StingDemo2 {
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
String s3 = "Abc";
System.out.println(s1.equals(s3));
System.out.println(s1.equalsIgnoreCase(s3));
//键盘录入的字符串和代码中定义的字符串比较
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String str = sc.next();
System.out.println(str == s2);
//输出false,next方法底层也是new出来的
//以后只要想比较字符串的内容,就必须要用String里面的方法
}
}
package stringDemo;
import java.util.Scanner;
public class StringDemo3 {
public static void main(String[] args) {
//读题拆解法
//1.定义两个变量记录用户名和密码
String name = "lisi";
String password = "123456";
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
//2、键盘录入用户名和密码
System.out.println("请输入用户名");
String userName = sc.next();
System.out.println("请输入用户密码");
String userPassword = sc.next();
//3、比较
if(name.equals(userName) && password.equals(userPassword)){
System.out.println("登录成功");
break;
}else {
if(i == 2){
System.out.println("最后一次机会用完,账户被锁定");
}else{
System.out.printf("登录失败,用户名或密码错误,您还剩下%d次机会", (2-i));
}
}
}
}
}
package stringDemo;
import java.util.Scanner;
public class StringDemo4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//1.键盘输入一个字符串
System.out.println("请输入一个字符串");
String str = sc.next();
//2.遍历
for (int i = 0; i < str.length(); i++) {
//i 依次表示字符串的每一个索引
char c = str.charAt(i);
System.out.println(c);
}
}
}
package stringDemo;
import java.util.Scanner;
public class StringDemo5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String str = sc.next();
int bigCount = 0;
int smallCount = 0;
int numCount = 0;
int otherCount = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= 'a' && c <= 'z'){
smallCount ++;
}else if(c >= 'A' && c <= 'Z'){
bigCount ++;
}else if(c >= '0' && c <= '9'){
numCount ++;
}else{
otherCount ++;
}
}
System.out.println("大写字母个数" + bigCount);
System.out.println("小写字母个数" + smallCount);
System.out.println("数字个数" + numCount);
System.out.println("其他字符个数" + otherCount);
}
}
package stringDemo;
public class StringDemo6 {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
String str = arrToString(arr);
System.out.println(str);
}
//1.我要干嘛 ---- 遍历数组并把数组拼接成一个字符串
//2、我干这件事情需要什么才能完成 ----- 数组
//3、我干完了是否要把结果返回给调用处 ---- 返回字符串
public static String arrToString(int[] arr){
if(arr == null){
return "";
}
if(arr.length == 0){
return "[]";
}
String result = "[";
for (int i = 0; i < arr.length; i++) {
if(i == arr.length - 1){
result += arr[i];
}else {
result += (arr[i] + ",");
}
}
result += "]";
return result;
}
}
package stringDemo;
import java.util.Scanner;
public class StringDemo7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String str = sc.next();
String s = reverse(str);
System.out.println("反转后的字符串是" + s);
}
public static String reverse(String str){
// str.length()forr 回车直接生成,选中一个变量 shift+f6 批量修改
String str1 = "";
for (int i = str.length() - 1; i >= 0; i--) {
char c = str.charAt(i);
str1 += c;
}
return str1;
}
}
package stringDemo;
import java.util.Scanner;
public class StringDemo8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int money;
while (true){
System.out.println("请输入一个合法金额");
money = sc.nextInt();
if(money >= 0 && money <= 9999999){
break;
}else {
System.out.println("请重新输入");
}
}
String moneyStr = "";
//得到money中的每一位数字
while (true){
int ge = money % 10;
String capitalNumber = getCapitalNumber(ge);
moneyStr = capitalNumber + moneyStr;
money /= 10;
if(money == 0){
break;
}
}
//在前面补0,补齐7位
int count = 7 - moneyStr.length();
for (int i = 0; i < count; i++) {
moneyStr = "零" + moneyStr;
}
String result = "";
//加上金额的单位
String[] arr = {"佰", "拾", "万", "仟", "佰", "拾", "元"};
for (int i = 0; i < moneyStr.length(); i++) {
char c = moneyStr.charAt(i);
result = result + c + arr[i];
}
System.out.println("转换后的金额:" + result);
}
//1.定义一个方法 1 --- 壹
public static String getCapitalNumber(int num){
String[] arr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
return arr[num];
}
}
package stringDemo;
public class StringDemo9 {
public static void main(String[] args) {
String phoneNumber = "13112349468";
String start = phoneNumber.substring(0, 3);
String end = phoneNumber.substring(7);
String result = start + "****" + end;
System.out.println(result);
}
}
package stringDemo;
public class StringDemo10 {
public static void main(String[] args) {
//获取说的话
String talk = "你玩的真好,下次不要玩了,TMD,SB";
//定义一个敏感词库
String[] arr = {"TMD", "CNM", "SB", "MLGB"};
for (int i = 0; i < arr.length; i++) {
talk = talk.replace(arr[i], "***");
}
System.out.println(talk);
}
}
package stringBuilderDemo;
public class StringBuilderDemo1 {
public static void main(String[] args) {
//1.创建对象
StringBuilder sb = new StringBuilder("abc");
//2.添加元素
// sb.append(123);
// sb.append("xyz");
//输出 abc123xyz
sb.reverse(); //cba
//StringBuilder是java已经写好的类,java在底层对他做了一些特殊处理
//打印对象不是地址值而是属性值
System.out.println(sb);
sb.append("sdf");
// toString方法将StringBuilder对象转换成字符串,StringBuilder只是一个字符串操作工具,并不是字符串
String str = sb.toString();
System.out.println(str);//cbasdf
}
}
package stringBuilderDemo;
import java.util.Scanner;
public class StringBuilderDemo2 {
public static void main(String[] args) {
//1.键盘输入字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String str = sc.next();
//2.反转键盘输入的字符
// StringBuilder sb = new StringBuilder();
// sb.append(str);
// sb.reverse();
// System.out.println(sb);
StringBuilder sb = new StringBuilder().append(str).reverse(); //链式编程
if (str.equals(sb.toString())){
System.out.println("是对称字符串");
}else {
System.out.println("不是对称字符串");
}
}
}
package stringBuilderDemo;
public class StringBuilderDemo3 {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
System.out.println(arrToString(arr));
}
public static String arrToString(int[] arr){
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if(i == arr.length - 1){
sb.append(arr[i]);
}else {
sb.append(arr[i]).append(",");
}
}
sb.append("]");
return sb.toString();
}
}
package stringBuilderDemo;
import java.util.StringJoiner;
public class Demo4 {
public static void main(String[] args) {
//1、创建对象
StringJoiner sj = new StringJoiner(",", "[", "]");
//2、添加元素
sj.add("1").add("2").add("3");
//3、打印
System.out.println(sj);
System.out.println(sj.length());
System.out.println(sj.toString());
}
}
package text;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
//1.键盘输入一个字符串
Scanner sc = new Scanner(System.in);
String str;
//2.检验字符串是否满足规则
while (true) {
System.out.println("请输入一个字符串");
str = sc.next();
boolean flag = checkStr(str);
if(flag){
break;
}else{
System.out.println("当前字符串不符合规则,请重新输入");
continue;
}
}
//3.字符转换
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
// System.out.println(c);
// System.out.print(changeLouMa(c - '0'));
result.append(changeLouMa(c - '0'));
}
//4、输出结果
System.out.println(result);
}
//将数字转换成罗马数字
public static String changeLouMa(int number){
String[] arr = {"","Ⅰ","Ⅱ","Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ","Ⅸ"};
return arr[number];
}
public static boolean checkStr(String str){
if (str.length() > 9){
return false;
}
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c < '0' || c > '9' ){
return false;
}
}
return true;
}
}
package text;
public class Test2 {
public static void main(String[] args) {
//1、定义两个字符串
String strA = "abcde";
String strB = "cdeab";
//2、旋转字符串并比较
boolean result = check(strA, strB);
System.out.println(result);
}
//比较
public static boolean check(String strA, String strB){
for (int i = 0; i < strA.length(); i++) {
strA = rotate(strA);
if (strA.equals(strB)){
return true;
}
}
return false;
}
//旋转字符串
public static String rotate(String str){
char first = str.charAt(0);
String end = str.substring(1);
return end + first;
}
}
方法二,使用字符数组
package text;
public class Test3 {
public static void main(String[] args) {
//1、定义两个字符串
String strA = "abcde";
String strB = "cdeab";
//2、旋转字符串并比较
boolean result = check(strA, strB);
System.out.println(result);
}
//比较
public static boolean check(String strA, String strB){
for (int i = 0; i < strA.length(); i++) {
strA = rotate(strA);
if (strA.equals(strB)){
return true;
}
}
return false;
}
//旋转字符串
public static String rotate(String str){
//可以把字符串先变成一个字符数组,然后调整字符数组里面的数组,最后再把字符数组变成字符串
char[] arr = str.toCharArray();
char first = arr[0];
for (int i = 0; i < arr.length-1; i++) {
arr[i] = arr[i+1];
}
arr[arr.length -1] = first;
//使用字符数组创建一个字符串对象
String result = new String(arr);
return result;
}
}