1 本题就是基本函数的用法
1 import java.util.Scanner; 2 3 public class Poone { 4 5 6 public static void main(String[] args) { 7 Scanner input = new Scanner(System.in); 8 System.out.println("请输入一个字符串:"); 9 String string = input.next(); 10 System.out.println("显示这个字符串:"); 11 System.out.println(string); 12 System.out.println("这个字符串长度为:"); 13 System.out.println(string.length()); 14 System.out.println("该字符串第一个字符"); 15 System.out.println(string.charAt(0)); 16 System.out.println("该字符串最后一个字符:"); 17 // System.out.println(string.charAt(2)); 18 System.out.println(string.charAt(string.length()-1)); 19 } 20 21 }
2 String类中有一个方法 public boolean contains(Sting s)就是用来判断当前字符串是否含有参数指定的字符串
1 import java.util.Scanner; 2 3 public class Potwo { 4 public static void main(String[] args) { 5 6 Scanner input = new Scanner(System.in); 7 System.out.println("请你输入第一个字符串:"); 8 String s1 = input.next(); 9 System.out.println("请你输入第二个字符串"); 10 String s2 = input.next(); 11 System.out.print("第二个字符串是否为第一个字符串的子串:"); 12 System.out.println(s1.contains(s2)); 13 14 } 15 16 }
3 判断书写格式是否合法,自行百度正则表达式
1 import java.util.Scanner; 2 3 public class Pothree { 4 public static void main(String[] args) { 5 String regex = "\\d{3}+-\\d+-\\d{3}+-\\d{5}+-\\d"; 6 //String a = "887-7-111-50690-4"; 7 System.out.println("请你输入一个字符串:"); 8 Scanner input = new Scanner(System.in); 9 String a =input.next(); 10 if (a.matches(regex)) 11 System.out.println("该字符串合法"); 12 else 13 System.out.println("该字符串不合法"); 14 } 15 }
4 定义一个在字母范围的条件即可
1 import java.util.Scanner; 2 3 public class Pofour { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 System.out.println("请你输入一串字符串:"); 7 String s = input.next(); 8 System.out.print("字符串中字母的个数为:"+ countLetters(s)); 9 } 10 public static int countLetters(String s){ 11 int count =0; 12 // 字符串转数组 13 char []A = s.toCharArray(); 14 for (int i = 0; i <A.length ; i++) { 15 if (A[i]>='a'&&A[i]<='z'||A[i]>='A'&&A[i]<='Z') 16 count++; 17 } 18 return count; 19 } 20 }
5 本题只要根据题目意思理解就行
1 public class jinzhi { 2 public static String toBinary(int value){ 3 String s =""; 4 while (value!=0){ 5 s=value%2+s; 6 value /= 2; 7 } 8 9 return s; 10 } 11 12 public static void main(String[] args) { 13 System.out.println(toBinary(42)); 14 } 15 16 }
6 把字符串变成数组,然后根据冒泡法进行排序即可
1 public class Posix { 2 public static void main(String[] args) { 3 4 System.out.println(sort("morning")); 5 } 6 public static String sort(String s){ 7 String string=new String(); 8 9 char []a=s.toCharArray(); 10 for (int i = 0; i <a.length-1 ; i++) { 11 for (int j = i+1; j < a.length; j++) { 12 if (a[i]>a[j]){ 13 a[i]^=a[j]; 14 a[j]^=a[i]; 15 a[i]^=a[j]; 16 } 17 } 18 } 19 for (int A:a) 20 System.out.print((char)A+" "); 21 return string; 22 } 23 }
7 只要把对应的ascii值+1即可
1 import java.util.Scanner; 2 3 public class Poseven { 4 public static void main(String[] args) { 5 //System.out.println('); 6 Scanner input = new Scanner(System.in); 7 System.out.print("请输入一串字符串:"); 8 String string = input.next(); 9 char []A = string.toCharArray(); 10 System.out.print("该字符串的密文为:"); 11 for (int i = 0; i < A.length; i++) { 12 if ((A[i]>='a'&&A[i]<='z'||A[i]>='A'&&A[i]<='Z')) 13 System.out.print((char)(A[i]+1)); 14 else 15 System.out.print(A[i]); 16 } 17 } 18 }
8 只要把对应的ascii值-1即可
import java.util.Scanner; public class Poeight { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入一串字符串:"); String string = input.next(); char []A = string.toCharArray(); System.out.print("该字符串的明文为:"); for (int i = 0; i < A.length; i++) { if ((A[i]>='a'&&A[i]<='z'||A[i]>='A'&&A[i]<='Z')) System.out.print((char)(A[i]-1)); else System.out.print(A[i]); } } }
9 根据题目意思,在“ ”,“,”,“.”分割即可
1 public class Ponine { 2 public static void main(String[] args) { 3 String s = "no pains,no gains."; 4 int count=0; 5 // 字符串变数组 6 char[]A =s.toCharArray(); 7 char []B=new char[26]; 8 for (int AA:A){ 9 System.out.print((char)AA); 10 } 11 System.out.println(); 12 // for (int i = 0; i <A.length ; i++) { 13 int k=0; 14 while (A[k]!='.') { 15 if (A[k] == ' ' || A[k] == ',') { 16 k++; 17 System.out.println(); 18 if (k == A.length) 19 k--; 20 } 21 System.out.print(A[k]); 22 k++; 23 } 24 // } 25 System.out.println(); 26 int j=0; 27 for (int i = 0; i <A.length ; i++) 28 if (A[i] != ' ' && A[i] != ',' && A[i] != '.') { 29 B[j] = A[i]; 30 j++; 31 count++; 32 } 33 34 } 35 }
10 熟悉cmd下执行java程序
11 冒泡法进行排序
12、13 自行百度查看