1. 判断邮箱
输入一个电子邮箱,判断是否是正确电子邮箱地址。 正确的邮箱地址:
- 必须包含 @ 字符,不能是开头或结尾
- 必须以 .com结尾
- @和.com之间必须有其他字符
import java.util.Scanner;
//输入一个电子邮箱,判断是否是正确电子邮箱地址。 正确的邮箱地址:
//
//必须包含 @ 字符,不能是开头或结尾
//必须以 .com结尾
//@和.com之间必须有其他字符
public class Email {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入邮箱号");
String email = sc.nextLine();
Email e = new Email();
Boolean result = e.email(email);
if(result){
System.out.println("邮箱地址有效");
}
else {
System.out.println("邮箱地址无效");
}
}
public Boolean email(String email){
int result = email.indexOf("@");
if(result == -1 ){
return false;
}
if(!email.endsWith(".com") && !email.startsWith("@")){
return false;
}
if(email.indexOf("@") >= email.indexOf(".com")-1){
return false;
}
return true;
}
}
2. 生成密码
随机生成一个 6 位的密码,要求:
-
不包含重复字符
-
要有字母和数字
-
不能以数字开头
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Password {
public static void main(String[] args) {
String result = password();
System.out.println(result);
}
public static String password(){
StringBuilder str = new StringBuilder();
Random random = new Random();
char c = (char)random.nextInt(65,91);
str.append(c);
for (int i = 1; i < 6; i++) {
int a = random.nextInt(10);
String s = String.valueOf(a);
if (str.indexOf(s) > 0) {
i--;
continue;
}
str.append(a);
}
String s = new String(str);
return s;
}
}
3. 生成幸运数字
随机生成 20 个 1 ~ 40 之间的不重复幸运数字存储到数组中。 要求:
- 数组中的数字乱序
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class RandomNumber {
static Random random = new Random();
public static int[] randomNum() {
int[] arr = new int[20];
for (int i = 0; i < arr.length; i++) {
Arrays.sort(arr);
int j = random.nextInt(40) + 1;
if (Arrays.binarySearch(arr, arr.length - i, arr.length, j) < 0) {
arr[0] = j;
} else {
i --;
}
}
// 打乱顺序
List<Integer> list = Arrays.asList(Arrays.stream(arr).boxed().toArray(Integer[]::new));
Collections.shuffle(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
return arr;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(randomNum()));
}
}
4.去除重复字符
编写一个程序,去除字符串)中的重复字符。
public class RemoveDuplicates {
public static void main(String[] args) {
String str = "ssttddffgg";
String result = fun(str);
System.out.println(result);
}
public static String fun(String str){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char currentChar = str.charAt(i);
if (sb.indexOf(String.valueOf(currentChar)) == -1) {
sb.append(currentChar);
}
}
return sb.toString();
}
}
5. 最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
public class Longest {
public static void main(String[] args) {
String str = "abcaefgh";
String result = p(str);
System.out.println(result.length());
}
public static String p(String str) {
//最长子串
String max = "";
//子串的起始下标
int start = 0;
for (int end = start + 1; end < str.length(); end++) {
//子串的范围
String sub = str.substring(start, end);
//当前字符的位置
String current = str.substring(end, end + 1);
//判断当前位置的字符是否在子串中存在
if (sub.indexOf(current) != -1) {
if (sub.length() > max.length()) {
max = sub;
}
// 找下一个子串的开始坐标
// str.indexOf(current); sub.indexOf()
start = str.indexOf(current, start) + 1;
// 最长子串比剩下的长度还长
if (max.length() > str.length() - start - 1) {
break;
} else if (end == str.length() - 1) { // 不存在并且是最后一个
sub = str.substring(start); // 一直到最后一个都没有重复的
// 当前子串和目前最大子串谁大
if (sub.length() > max.length()) {
max = sub;
}
}
}
}
return max;
}
}