1、统计我们一个字符串里面出现的字母、数字、空格、其他符号各有多少个?
import java.util.Scanner;
public class Test05 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一行字符串:");
String str = sc.nextLine();
number(str);
}
public static void number(String str) {
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.isLetter(str.charAt(i))) {
num1 += 1;
} else if (Character.isDigit(str.charAt(i))) {
num2 += 1;
} else if (Character.isSpaceChar(str.charAt(i))) {
num3 += 1;
} else {
num4 += 1;
}
}
System.out.println("字母有:" + num1);
System.out.println("数字有:" + num2);
System.out.println("空格有:" + num3);
System.out.println("其他符号有" + num4);
}
}
2、请查找a字符串在b字符串里面出现过多少次?
public class Test05 {
public static void main(String[] args) {
String a = "asdasdas";
String b = "as";
times(a, b);
}
public static void times(String a, String b) {
int count = 0;
while (a.indexOf(b) != -1) {
count++;
a = a.substring(a.indexOf(b) + b.length());
}
System.out.println("出现过:" + count + "次");
}
}
3、随机产生4个字符(大小写字母、数字组合)作为验证码,控制台输入并提示是否输入正确。
import java.util.Random;
import java.util.Scanner;
public class Test05 {
public static void main(String[] args) {
String code = "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ0123456789";
char[] cha = code.toCharArray();
Random random = new Random();
char[] code1 = new char[4];
for (int i = 0; i < 4; i++) {
int index = random.nextInt(code.length() - 1);
code1[i] += cha[index];
}
String a = "";
for (char c : code1) {
a += c;
}
System.out.println("验证码为:" + a);
Scanner sc = new Scanner(System.in);
System.out.println("请输入验证码:");
String s = sc.nextLine();
if (s.equals(a)) {
System.out.println("验证码正确");
} else {
System.out.println("验证码错误");
}
}
}
4、写一个方法判断一个字符串是否对称
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = sc.nextLine();
same(str);
}
public static void same(String str) {
boolean result = true;
int count = (str.length() - 1) / 2;
for (int i = 0; i <= count; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
result = false;
break;
}
}
if (!result) System.out.println("该字符串是不对称的");
else{
System.out.println("该字符串是对称的");
}
}
5、输入一个电话号码,并把电话号码格式化成xxx-xxxx-xxxx形式。例如输入18620011234,格式化后186-2001-9538
import java.util.Scanner;
public class Test05 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入手机号码:");
String num = sc.next();
StringBuilder sb = new StringBuilder();
sb.append(num);
String regex = "1[3|4|5|7|8][0-9]{9}";
if (num.matches(regex)) {
sb.insert(3, '-');
sb.insert(8, '-');
System.out.println(sb);
} else {
System.out.println("手机号码输入错误!");
}
}
}
6、去掉字符串中的重复字符
public class Test04 {
public static void main(String[] args) {
String str = "abbbcdcddd";
str = str.replaceAll("(?s)(.)(?=.*\\1)","");
System.out.println(str);
}
}
7、声明一个Point类表示屏幕上的点,其中含有float类型的x和y字段,请设计一个工具方法计算两点之间的距离。(提示:两点之间的距离公式)
public class Point {
private float x;
private float y;
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public Point() {
}
public Point(float x, float y) {
this.x = x;
this.y = y;
}
}
public class PointUtils {
private static PointUtils pointUtils = new PointUtils();
private PointUtils() {
}
public static PointUtils getInstance() {
return pointUtils;
}
public static float distancePoint(Point p1, Point p2) {
float a = p1.getX() - p2.getX();
float b = p1.getY() - p2.getY();
return (float) Math.sqrt(a * a + b * b);
}
}
public class Test01 {
public static void main(String[] args) {
Point point1 = new Point(3, 4);
Point point2 = new Point(4, 5);
float f = PointUtils.getInstance().distancePoint(point1, point2);
System.out.println(f);
}
}
8、建立银行账户类(Account),类中有变量double balance表示存款,Account类的构造方法能初始化账户余额,Account类中有取款的方法withDrawal(double dAmount),当取款的数额大于存款时,抛出自定义的InsufficientFundsException,当取款数额为负数,抛出自定义的NagativeFundsException。如new Account(100),表示当前余额100元,当调用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。
public class Account {
private double balance;
public Account(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Account() {
}
public void withDrawal(double dAmount) throws Exception{
if (dAmount>balance){
throw new Exception("InsufficientFundsException");
}else if(dAmount<0){
throw new Exception("NagativeFundsException");
}else{
balance -=dAmount;
}
}
@Override
public String toString() {
return "Account{" +
"balance=" + balance +
'}';
}
}
package cn.homework02;
public class Test02 {
public static void main(String[] args) {
try{
Account account = new Account();
account.setBalance(100);
account.withDrawal(150);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("程序正常结束");
}
}
9、请编写程序利用线程输出从A到Z的字母,每隔一秒钟输出一个数字,请按顺序输出。
public class AThread implements Runnable {
@Override
public void run() {
synchronized (this) {
try {
for (char i ='A';i<='Z';i++){
Thread.sleep(500);
System.out.print(i+" ");
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) {
AThread aThread= new AThread() ;
Thread thread = new Thread(aThread);
thread.start();
}
}
10、现有一百个快递待派发,定义快递类Expresses 作为公共资源类,定义快递员线程类Mailman ,请开启三个线程派发此100个快递,并打印哪个快递员派发了哪一个快递。创建100个Expresses对象表示100个快递,放到数组中 思路如下
class Expresses{
private String name;
....
}
class Mailman extends Thread{
//初始化100个快递
private static Expresses[] es = new Expresses[100];
static{
for(){
es[i] = new Expresses("地址"+i);
}
}
private int index;
private int number;
run(){
for(100){
if(index < 100){
Expresses e = es[index++];
sout(getName() + "发了"+ e.getName()+"快递");
number++;
//如果是实现方式,三个变量记录各自的数量
if("小王".equals(Thread.currentThread().getName())){
}
}
}
Sout(getName() + "发了" + number+"份快递");
}
}
11、请根据以下描述完成操作。
1)请用IO的知识点,获取文件的内容。文件内容如下:
2)创建一个Map集合将统计文件D:/calcCharNum.txt中各个字母出现次数 。
3)遍历Map集合并按照以下格式打印,格式如下:“A(2),B(5),C(4),D(6),E(3),F(7)”,括号内代表字符出现次数。
import java.io.*;
import java.util.*;
public class map_io {
public static void main(String[] args) throws IOException {
File file = new File("D:\\calcCharNum.txt");
Reader reader = new FileReader(file);
char[] chars = new char[1024];
int len;
String s = "";
while ((len = reader.read(chars)) != -1) {
s = new String(chars,0,len);
System.out.println(s);
}
reader.close();
char[] chars1 = s.toCharArray();
Map<Character, Integer> map = new TreeMap<>();
for(char c : chars1){
if (map.containsKey(c)){
Integer count = map.get(c);
map.put(c,count+1);
}else {
map.put(c,1);
}
}
System.out.println(map);
Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
for (Map.Entry<Character,Integer> entry : entrys){
Character key = entry.getKey();
Integer value = entry.getValue();
if (key.equals('F')){
System.out.print(key+"("+value+")");
}
else {
System.out.print(key+"("+value+"),");
}
}
}
}