标签:目录 substring pre 产生 生成 err substr fas 用户
package work1;
public class Work1 {
public static void main(String[] args) {
StringBuffer s = new StringBuffer();
s= s.append("a");
s= s.append("b");
s= s.append("c");
s= s.append("d");
s= s.append("e");
s= s.append("f");
s= s.append("g");
s= s.append("h");
System.out.println(s);
}
}
Java中随机数的生成:
java.util.Random r = new java.util.Random();
int a = r.nextInt(100): a 0-99的随机数
package work2;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Work2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.println("请输入生成字符串的长度");
int length = sc.nextInt();
String s = "";
for(int i = 0;i<length;i++){
int random = r.nextInt(2);
if(random==1){
int num = r.nextInt(10);//随机产生数字
s=s+s.valueOf(num);
}
else {
int randomChar = r.nextInt(2);
if(randomChar==1){//随机产生大写字母
char str =(char)(r.nextInt(26)+‘A‘);
s=s+s.valueOf(str);
}
else{//随机产生小写字母
char str =(char)(r.nextInt(26)+‘a‘);
s=s+s.valueOf(str);
}
}
}
System.out.println(s);
}
}
package work2;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Work2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random r = new Random();
System.out.println("请输入生成字符串的长度");
int length = sc.nextInt();
//创建一个集合存储大写小写字母以及数字
List<Character> list = new ArrayList<Character>();
for(char c =‘A‘;c<=‘Z‘;c++)
list.add(c);
for(char c =‘a‘;c<=‘z‘;c++)
list.add(c);
for(char c =‘0‘;c<=‘9‘;c++)
list.add(c);
String s = "";//保存结果
for(int i = 0;i<length;i++){
//随机获取字符下标
int a = r.nextInt(list.size());
s = s+list.get(a);
}
//打印
System.out.println(s);
}
}
如:"abcd23abc34bcd"中,"bc"子串的出现位置为: 1,7,11.字符串和子串均由用户输入
package work3;
import java.util.Scanner;
public class Work3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串");
String s1 = sc.next();
System.out.println("请输入子串");
String s2 = sc.next();
int index =0;
while((index=s1.indexOf(s2, index))!=-1){
System.out.print(index+" ");
index = index +s2.length();
}
}
}
package work4;
import java.util.Scanner;
public class Work4 {
public static void main(String[] args) {
String s = "1591231231231326560686464506565791310665979803";
int[] count = new int[10];
for(int i=0;i<10;i++){
String s1 = s;
int index=0;
while(true){
index = s1.indexOf(String.valueOf(i));
if(index!= -1){
s1=s1.substring(index+1);//截取字符串
count[i]++;
}
else break;
}
}
for (int i=0;i<count.length;i++) {
System.out.println(i+" 出现的次数 "+count[i]);
}
}
}
package work4;
import java.util.Scanner;
public class Work4 {
public static void main(String[] args) {
String s = "1591231231231326560686464506565791310665979803";
int[] arr = new int[10];//创建数组记录次数
for(int i =0;i<s.length();i++){
arr[s.charAt(i)-‘0‘]++;
}
//遍历
for (int i=0;i<arr.length;i++) {
System.out.println(i+"的次数是"+arr[i]);
}
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Work5 {
public static void main(String[] args) {
String s ="s13fs54fas87f8sf40";
Map<Character, Integer> map = new HashMap<Character, Integer>();//一个key值对应一个value
for(int i=0;i<s.length();i++){
Character c =s.charAt(i);
if(map.get(c)==null)//第一次加入到map集合
map.put(c, 1);
else
map.put(c, map.get(c)+1);
}
Set<Character> keySet = map.keySet();
for (Character character : keySet) {
System.out.println(character+"----"+map.get(character));
}
}
}
public class Work6 {
public static void main(String[] args) {
String str = "aaaaaaabbbbbcerrrrggggggggsssssspoqqqqq";
StringBuilder sb = new StringBuilder("");
char current = str.charAt(0);
int count = 1;
for(int i = 1 ; i < str.length() ; i++){
char c = str.charAt(i);
if (c == current){
count++;
}
else{
if (count == 1) sb.append(current);
else sb.append(current).append(count);
current = c;
count = 1;
}
}
if (count == 1) sb.append(current);
else sb.append(current).append(count);
System.out.println(sb.toString());
}
}
标签:目录 substring pre 产生 生成 err substr fas 用户
原文地址:https://www.cnblogs.com/shellxx/p/12578522.html