编写一个校验用户名的程序,检测键盘录入的用户名是否合法
要求:用户名的长度必须是6-10位之间(包括6位和10位),可以是字母,数字。但是不 能以数字开头
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入用户名");
String s;
s = sc.nextLine();
while (true) {
if (s.length() >= 6 && s.length() <= 10) {
//matches() 方法用于检测字符串是否匹配给定的正则表达式。
if (s.matches("[a-zA-Z](.)+")) {
System.out.println(s + "是正确的用户名");
break;
} else {
System.out.println("用户名不能数字开头");
}
} else {
System.out.println("请输入长度为6-10之间的用户名:");
}
}
}
接收用户输入的字符将字符串中所有的数字替换为“*”号输出在控制台。
public class dome6 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("输入字符串");
String s = sc.nextLine();
// replaceAll() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串,该函数会替换所有匹配到的子字符串。
// 把字符串中的数字/非数字都去掉
// a.replaceAll("\D+", “”) —— 这么写是把非数字字符去掉
// 比如:str=123mefrewe345contrewwebute 把非数字去掉 str=123345
//
// a.replaceAll("\d+","") —— 这么写就是把数字字符都去掉
// 比如:str=123ameeewf345conutedew 把数字去掉 str=ameeewfconutedew
//————————————————
System.out.println(s.replaceAll("\\d", "*"));
}
}
3.题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字,由键盘输入。例如5+55+555+5555+55555(此时共有5个数相加), a的值为几就有几个数相加。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a;
System.out.println("请输入纯数字:");
while (true) {
try {
a = sc.nextInt();
break;
} catch (Exception e) {
System.out.println("请输入纯数字:");
}
}
int s = 0;
for (int i = 1; i <= a; i++) {
int unm = 0;
int k = 1;
for (int j = 0; j < i; j++) {
unm += a * k;
k *= 10;
}
s += unm;
}
System.out.println(s);
}
}
接收用户输入的一句英文,将其中的单词以反序输出。
/* 接收用户输入的一句英文,将其中的单词以反序输出。例如:
“I love you” →"I evol uoy"
思路:
1、获取用户输入,调用String的split()方法用空格将其分割成
字符串数组
2、遍历这个字符串数组,通过StringBuffer构造方法,将
字符串数组转换成StringBuffer对象,并调用reverse() 方法,
将字符串倒序
3、通过String的构造方法再将StringBuffer对象转换成String
并输出该新字符串
*/
public class dome10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入要反序输出的一句英文:");
String line = sc.nextLine();
String[] lines = line.split(" ");
for (int i = 0; i < lines.length; i++) {
//将数组中的字符串转换成StringBuffer对象
StringBuffer sb = new StringBuffer(lines[i]);
//通过StringBuffer对象的reverse()方法得到反序单词
String newStr = new String(sb.reverse());
System.out.print(newStr + "");
}
System.out.println();
}
}
有类似这样的字符串:"1.2,3.4,5.6,7.8,5.56,44.55"请按照要求,依次完成以下试题。
/*
* 1、有类似这样的字符串:"1.2,3.4,5.6,7.8,5.56,44.55"请按照要求,依次完成以下试题。
* (1)以逗号作为分隔符,把已知的字符串分成一个String类型的数组,数组中的每一个元素类似于"1.2","3.4"这样的字符串
* (2)把数组中的每一个元素以"."作为分隔符,把"."左边的元素作为key,右边的元素作为value,封装到Map中,Map中的key和value都是Object类型。
* (3)把map中的key封装的Set中,并把Set中的元素输出。
* (4)把map中的value封装到Collection中,把Collection中的元素输出。
*/
public class dome1 {
public static void main(String[] args) {
String string = "1.2,3.4,5.6,7.8,5.56,44.55";
//1.将字符串以","分割,用一个字符串str数组接收
String[] str = string.split(",");
//2.定义Map数组
Map<Object, Object> map = new HashMap<Object, Object>();
// 3.遍历str数组并将数组中的元素用"."分割(使用了正则表达式内容),用str2接收。
for (int i = 0; i < str.length; i++) {
String[] str2 = str[i].split("\\.");
map.put(str2[0], str2[1]);
}
// 创建Collection集合col
Collection<Object> col = new ArrayList();
// 创建Set集合set,接受map中的key
Set<Object> set = map.keySet();
// 打印set
System.out.println(set);
// 遍历set集合,通过get方法获取map中的value,并添加进col集合中
for (Object o : set) {
col.add(map.get(o));
}
// 打印col
System.out.println(col);
}
}
编写程序,循环接收用户从键盘输入多个字符串,直到输入“end”时循环结束,并将所有已输入的字符串按字典顺序倒序打印。
public static void main(String[] args) {
//创建TreeSet集合 ts 重写comparator带参排序
TreeSet<String> ts = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return -(o1.compareTo(o2));
}
});
//使用while循坏包裹输出接收条件
//迭代器循坏输出
//再添加到 ts 集合
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入end");
String s = sc.nextLine();
if (s.equals("end")) {
Iterator<String> it = ts.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
break;
}
ts.add(s);
}
}
编写程序,循环接收用户从键盘输入多个字符串,直到输入“end”时循环结束,并将所有已输入的字符串按字典顺序倒序打印。(使用到:IO操作——BufferedReader(缓冲区读取内容,避免中文乱码))
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/*
题目:编写程序,循环接收用户从键盘输入多个字符串,直到输入“end”时循环结束,并将所有已输入的字符串按字典顺序倒序打印。
分析:
1.字符串本身提供的比较性为字典顺序,可以使用工具类Collections.reverse()方法将原来的比较性反序。
但也可以自定一个比较器,让集合自身必备比较性;
2.键盘录入的是字节流,操作的是字符流,可以使用转换流,并加入缓冲区技术,提高效率;
3.录入的字符串存储到ArrayList集合中;
4.使用Collections工具类给ArrayList中元素排序
5.打印ArrayList集合中的元素
步骤:
1.List<String> list = new ArrayList<String>();
2.BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
3.list.add(line);
4.Collections.sort(list, Collections.reverseOrder());
5.Iterator<String> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
*/
public class dome7 {
public static void main(String[] args) {
// 1、定义一个ArrayList集合
List<String> list = new ArrayList<String>();
// 键盘录入字符串,转换流,缓冲区
System.out.println("请输入一些字符或者字符串, end 结束!");
// IO操作——BufferedReader(缓冲区读取内容,避免中文乱码)
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
try {
while ((line = bufr.readLine()) != null) {
if ("end".equals(line))
break;
// 往ArrayList集合中添加元素
list.add(line);
}
} catch (IOException e) {
throw new RuntimeException("IO异常");
}
// 给ArrayList排序,字典倒序
Collections.sort(list, Collections.reverseOrder());
// 打印集合
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
编写一个程序,把这个目录下的所有的带.java文件都拷贝到另一个目录中,拷贝成功后,把后缀名是.java的改成.txt,如果没有这个文件就创建。
//4、编写一个程序,把这个目录下的所有的带.java文件都拷贝到另一个目录中,拷贝成功后,把后缀名是.java的改成.txt
/*
* 思路
* 1、创建输入流对象,遍历目录,通过匿名内部类拿出。符合规则的文件
* 2、通过高效字符流复制文件
* 3、将.java改成.txt
*/
public class dome3 {
public static void main(String[] args) throws IOException {
// 封装d:\\java这个目录
File start = new File("d:\\java");
// 获取该目录下的所有满足条件的数组
File[] files = start.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isFile() && name.endsWith(".java");
}
});
// 判断目的地是否存在,不存在就建立
File endFile = new File("d:\\jar");
if (!endFile.exists()) {
endFile.mkdirs();
}
// 遍历文件数组
for (File f : files) {
String name = f.getName();//得到文件名
File newFile = new File(endFile, name);// 将路径和文件名拼接
BufferedReader br = new BufferedReader(new FileReader(f));
BufferedWriter bw = new BufferedWriter(new FileWriter(newFile));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
// 改名
File[] endFileArray = endFile.listFiles();
for (File file : endFileArray) {
String name = file.getName();
String newname = name.replace("java", ".java");
File newFile = new File(endFile, newname);
file.renameTo(newFile);
}
}
}
键盘录入5个数据,存储到一个数组中,取最大值和最小值
public static void main(String[] args) {
Scanner Sc = new Scanner(System.in);
int[] arr = new int[5];
int count = 0;
for (int i = 0; i < 5; i++) {
System.out.println("请输入第" + (count + 1) + "个数据:");
int a = Sc.nextInt();
arr[count] = a;
count++;
}
get(arr);
}
public static void get(int[] arr) {
//排序
Arrays.sort(arr);
//排序后lenth-1位置就是最大的
System.out.println("最大值:" + arr[arr.length - 1]);
//排序后arr【0】位置就是最小的
System.out.println("最小值:" + arr[0]);
}
}
字符串出现次数,取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)...
/* 7、 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)...
*/
public class dome5 {
public static void main(String[] args) {
//1.接收字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串");
String line = sc.nextLine();
//2.转换成字符数组用于判断
char[] str = line.toCharArray();
// 3.创建一个TreeMap
TreeMap<Character, Integer> map = new TreeMap<>();
// 4.遍历字符数组
for (char c : str) {
Character key = c;
Integer value = map.get(c);
// 判断,如果值为空,则向集合中添加(key,1),如果不为空则添加(key,value+1)
if (value == null) {
map.put(key, 1);
} else {
map.put(key, value + 1);
}
}
//遍历集合
Set<Map.Entry<Character, Integer>> set = map.entrySet();
Iterator<Map.Entry<Character, Integer>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<Character, Integer> me = it.next();
Character key = me.getKey();
Integer vakue = me.getValue();
System.out.println(key + "(" + vakue + ")");
}
}
}
求三位数的质数,只能被 本身和1整除,倒序输出。
import java.util.ArrayList;
import java.util.Collections;
//求三位数的质数,只能被 本身和1整除,倒序输出
public class dome10 {
public static void main(String[] args) {
//创建list集合
ArrayList<Integer> a1 = new ArrayList<>();
//创建布尔对象用来判断
boolean flag = true;
//循坏得到3位
for (int a = 100; a < 1000; a++) {
//flag=true进行下列判断
flag = true;
//判断能被2整除的数
for (int b = 2; b < a; b++) {
if (a % b == 0) {
flag = false;
count++;
break;
}
}
if (flag) {
a1.add(a);
}
}
Collections.reverse(a1);
System.out.println(a1);
}
}
九九乘法表
//九九乘法表
public class dome11 {
public static void main(String[] args) {
for (int x = 1; x <= 9; x++) {
for (int y = 1; y <= x; y++) {
System.out.print(y + "*" + x + "=" + x * y + "\t");
}
System.out.println();
}
}
}
冒泡排序
//冒泡排序
public class dome12 {
public static void main(String[] args) {
int[] array = {12, 25, 234, 364, 4, 345, 346, 47};
for (int a = 0; a < array.length - 1; a++) {
for (int b = 0; b < array.length - 1 - a; b++) {
if (array[b] > array[b + 1]) {
int temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
}
}
for (int i : array) {
System.out.println(i);
}
}
}
1-100 中个位和十位不含 7 的数
//1-100 中个位和十位不含 7 的数
public class dome13 {
public static void main(String[] args) {
for (int a = 0; a < 100; a++) {
int ge = a % 10;
int shi = a / 10;
if (!(ge == 7 || shi == 7)) {
System.out.println(a);
}
}
}
}
又逢年底,某个有100个员工的公司准备组织年会。会场有两个(前门和后门)入
* 口,入场时每位员工都能获得阳光普照奖 -- 双色球彩票一注。请同学们通过多线程的技
* 术模拟员工入场,以及获得彩票的过程。并且分别统计每个入口入场的人数,以及每个
* 员工拿到的彩票的号码。
/**
* 第二题
* 又逢年底,某个有100个员工的公司准备组织年会。会场有两个(前门和后门)入
* 口,入场时每位员工都能获得阳光普照奖 -- 双色球彩票一注。请同学们通过多线程的技
* 术模拟员工入场,以及获得彩票的过程。并且分别统计每个入口入场的人数,以及每个
* 员工拿到的彩票的号码。
* 程序运行后打印格式如下:
* 编号为: 1 的员工从 后门 入场! 拿到的双色球彩票号码是: [17, 24,29, 30, 31, 32, 7]
* 编号为: 2 的员工从 后门 入场! 拿到的双色球彩票号码是: [6, 11, 14,22, 29, 32, 15]
* 编号为: 3 的员工从 前门 入场! 拿到的双色球彩票号码是: [6, 11, 15,22, 27, 32, 15]
* //.....
* 从后门入场的员工总共: xx 位员工
* 从前门入场的员工总共: XX 位员工
* 双色球组成介绍:
* 红球: 6个 范围:1~33 不可以重复
* 蓝球: 1个 范围:1~16 蓝球可以和红球重复
* 双色球是可以同时开出多注特等奖
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class dome2 {
public static void main(String[] args) {
//开启线程
lottery lottery=new lottery();
Thread d1=new Thread(lottery,"前门");
d1.start();
Thread d2=new Thread(lottery,"后门");
d2.start();
}
}
class lottery extends Thread{
//前门人数
private int frontNum=0;
//后门人数
private int backNum=0;
//总人数
private int person=100;
@Override
public void run() {
while (true){
synchronized (this){
try {
//让线程每隔10秒运行
Thread.sleep(10);
//获取线程名称currentThread():获取当前正在运行的线程
String threndName=Thread.currentThread().getName();
//1-1判断人数大于0就退出
if (person<0){
return;
}
//1-2:总人数>0
if (person>0) {
//统计前门人数
if ("前门".equals(threndName)) {
System.out.println("编号为:" + (100 - person + 1) + "的员工从前门入场!" +
"拿到的双色球号码是:" + this.show());
frontNum++;
}
//统计后门人数
if ("后门".equals(threndName)) {
System.out.println("编号为:" + (100 - person + 1) + "的员工从后门入场!" +
"拿到的双色球号码是:" + this.show());
backNum++;
}
person--;
}
//1-3:总人数==0,显示前门人数及后门人数
if (person==0){
System.out.println("前门人数"+frontNum);
System.out.println("后门人数"+backNum);
person--;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//显示双色球结果
public List<Integer> show() {
//创建集合
List<Integer>list=new ArrayList<Integer>();
//生成6个随机数
for (int i=1;i<6;i++){
Random r=new Random();
int unm=r.nextInt(33)+1;
list.add(unm);
}
return list;
}
}