java">package text7;public class ZiFanz {public static void main(String[] args) {//1.定义两个字符串String strA = "abcde";String strB = "deabc";//2.abcde->bcdea->cdeab->deabc旋转字符串//旋转并比较boolean result = cheak(strA, strB);System.out.println(result);}public static boolean cheak(String strA , String strB){for (int i = 0; i < strA.length(); i++) {strA = rotate(strA);if (strA.equals(strB)){return true;}}return false;}//1.作用:旋转字符串,把左侧的字符移到右侧上//2.形参:旋转前的字符串//3.返回值:旋转后的字符串public static String rotate(String str){//套路//如果我们看到要修改字符串的内容//可以有两个办法//1.用subString进行截取,把左边的字符截取出来拼接到右侧去//2.可以把字符串先变成一个字符数组,然后调整字符数组里面的数据,然后再把字符数组变成字符串//截取思路//获取最左侧那个字符char first = str.charAt(0);//获取剩余字符String end = str.substring(1);return end+first;//第二种写法char[] arr = str.toCharArray();char first = arr[0];for (int i = 1; i < arr.length; i++) {arr[i-1] = arr[i];}arr[arr.length - 1] = first;String result = new String(arr);return result;}
}
自主小练习
键盘输入字符串,然后进行随机打乱
java">package text7;import java.util.Random;
import java.util.Scanner;public class SuiJi {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串");String str = sc.next();//2.打乱里面的内容String daluan = Daluan(str);System.out.println(daluan);}public static String Daluan(String str){char[] arr = str.toCharArray();Random rd = new Random();for (int i = 0; i < arr.length; i++) {int cishu = rd.nextInt(arr.length);int cishu2 = rd.nextInt(arr.length);if(cishu == cishu2){cishu2 = rd.nextInt(arr.length);}char Tihuan = arr[cishu];arr[cishu] = arr[cishu2];arr[cishu2] = Tihuan;}return new String(arr);}
}
在大写的A~Z和小写的a~z中随机抽取四个字母,然后在中间随机插入一个数字
java">package text7;import java.util.Random;public class Yanzheng {public static void main(String[] args) {char[] allChars = generateAllChars();char[] result = suiji(allChars);Random str = new Random();int i = str.nextInt(10);//生成十个随机数字int w = str.nextInt(5);//四个字母五个断点,随机插入一个数字String resulti = new String(result);String str1 = resulti.substring(0,w) + i + resulti.substring(w);System.out.println(str1);}// 随机获取四个字母public static char[] suiji(char[] arr) {Random random = new Random();char[] result = new char[4];for (int i = 0; i < 4; i++) {int randomIndex = random.nextInt(arr.length);result[i] = arr[randomIndex];}return result;}public static char[] generateAllChars() {char[] allChars = new char[52];int index = 0;for (char c = 'a'; c <= 'z'; c++) {allChars[index++] = c;}for (char c = 'A'; c <= 'Z'; c++) {allChars[index++] = c;}return allChars;}
}