最近从头开始复习JavaSE,发现还是有很多不足之处,于是把复习中做过的比较经典的例题整理到这个笔记中,希望
自己能够完全掌握这些经典基础题。

一. 经典基础题

1.1 打印等腰三角形

在控制台输出用*打印的等腰三角形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.lousen.test;
import java.util.Scanner;
public class DengYao
{
public static void main (String[] args)
{
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
System.out.println("请输入一个整型数:");
int a = in.nextInt();
for(int i=1; i<=a; i++)
{
for(int j = a-i ; j>0; j--)
{
System.out.print(" ");
}
for(int p = 2*i-1 ; p>0; p--)
{
System.out.print("*");
}
System.out.println();
}
}
}

1.2 打印近似圆

在控制台用*打印近似圆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.lousen.test;
import java.util.Scanner;
public class Circle {
/**
* 定义画圆函数
* 设圆的半径为r
* 设我们要打的点的坐标为(x,y)
* 设圆心为(r,r)*/
public static void DrawCircle(int r)
{
for(int y = 0; y <= 2*r; y = y+2) //纵轴y,取步长为2
{
long x = Math.round(Math.sqrt(2*r*y-y*y)+r); //根据已知的r和y取得最近接x的值
for(int t = 0; t <= 2*r; t++) //横轴t,取步长为1
{
if(t == x || t == (2*r - x)) //横轴上对应两个x值
{
System.out.print("*"); //输出点
}
else
{
System.out.print(" "); //否则为空
}
}
System.out.println(); //每打下一条y轴换行
}
}
/*获取一个半径,使用DrawCircle函数画圆*/
public static void main(String[] args)
{
System.out.println("请输入半径:");
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
int r = in.nextInt();
DrawCircle(r);
}
}

1.3 按字节截取字符串

注:一个英文字符相当于一个字节,一个中文字符相当于两个字节
例:输入字符串:”我们asd”,再输入(1,3)将返回”们as”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.lousen.test;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
public class StringCut_2 {
public static String String_Cut(String abc,int i,int j)
{
byte[] a = null;
try {
a = abc.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(j<0){
System.out.println("请输入正整数");
}
else if(j>=a.length-i+1)
{
return abc;
}
else if(j>0&&(j-i+1)%2==1)
{
if((byte)a[j-i]<0)
{
j--;
}
}
byte[] b = new byte[j];
System.arraycopy(a, i, b, 0, j);
String str = new String(b);
return str;
}
public static void main(String[] args)
{
System.out.println("请输入字符串:");
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
String str = in.next();
System.out.print("i=");
int i = in.nextInt();
System.out.print("j=");
int j = in.nextInt();
String s = String_Cut(str,i,j);
System.out.println(s);
}
}

1.4 计算平均值、最小值和最大值

定义一个长度为10的整数数组,用于保存输入的10个整数,并计算它们的平均值、最大值和最小值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.lousen.test_6;
import java.util.Scanner;
public class ZhengShuJS {
public static void main(String[] args){
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
int[] array = new int[10]; //数组动态初始化
int sum = 0;
for(int i = 0;i < array.length;i++){ //数组赋值并取和
array[i] = in.nextInt();
sum = sum + array[i];
}
int max = array[0];
int min = array[0];
for(int i = 0;i < array.length;i++){
if(max == 0){ //max默认为0,故需要对max为0时的情况做判断
max = array[0];
}
max = Math.max(array[i], max); //最大值
if(min == 0){ //min默认为0,故需要对min为0时的情况做判断
min = array[0];
}
min = Math.min(array[i], min); //最小值
}
double avg = sum/10.0;
System.out.println("平均数:"+avg);
System.out.println("最大值:"+max);
System.out.println("最小值:"+min);
}
}

二. 使用正则表达式

将A1B2C3D4E5F6拆分开来,并分别存入int[]和String[]数组。得到结果[A,B,C,D,E,F]和[1,2,3,4,5,6]。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.lousen.test_6;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ChaiFen {
public static void main(String[] args){
String str = "A1B2C3D4E5F6";
int[] array = new int[10];
int i = 0; //数组下标索引
int j = 0; //数组下标索引
String[] str1 = new String[10];
Pattern p1 = Pattern.compile("\\d"); //匹配数字
Pattern p2 = Pattern.compile("\\D"); //匹配非数字
Matcher m1 = p1.matcher(str);
Matcher m2 = p2.matcher(str);
while(m1.find()){ //查找符合条件
array[i++] = Integer.parseInt(m1.group()); //存入数组
}
while(m2.find()){
str1[j++] = m2.group();
}
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(str1));
}
}

邮件地址的合法格式检验

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.lousen.test_4;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestZhengZe_2 {
public static void main(String[] args){
String[] mails = {
"1135497143@qq.com",
"lousenjay@gmail.com",
"18579116628@163.com",
"aaaaaaaa"
};
Pattern p = Pattern.compile("\\w{3,15}@\\w+\\.(com|cn|org|net)");
Matcher m = null;
for(int i = 0; i < mails.length ; i++){
if(m==null){
m = p.matcher(mails[i]);
}else{
m.reset(mails[i]);
}
String result = mails[i] + (m.matches() ? "是":"不是" + "一个有效邮件地址!");
System.out.println(result);
}
}
}

三. 国际化

应用程序运行时,可根据客户端请求来自的国家/地区和语言显示不同的界面

3.1 生成properties文件

在命令窗口:native2ascii name.properties name_zh_CN.properties //适用于非西欧字符
在IDE中: 利用插件直接新建一个文件夹改为properties格式,输入对应的name和value即可
例:
mess_en_US
hello=Welcom you \!

mess_zh_CN
hello=你好 !

3.2 使用国际化

java.util.ResourceBundle:用于加载国家、语言资源包
import java.util.Locale:用于封装特定的国家/区域、语言环境
例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.lousen.test_5;
import java.util.Locale;
import java.util.ResourceBundle;
public class Hello {
public static void main(String[] args){
//取得系统默认的国家和语言环境
Locale myLocale = Locale.getDefault(Locale.Category.FORMAT);
//Locale myLocale = Locale.CHINA; //指定国家和语言环境
//根据指定的国家/语言环境加载资源文件
//给出完全路径!!!!!!!!
ResourceBundle bundle = ResourceBundle.getBundle("com.lousen.test_5//mess",myLocale);
//打印从资源文件所取得的的信息
System.out.println(bundle.getString("hello"));
}
}

四. 格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.lousen.test_5;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
public class myData {
private static int SHORT;
private static int LONG;
public static void main(String[] args)
{
Date dt =new Date();
double db = 12456789;
// 定义一个Locale变量
Locale currentLocale = null;
currentLocale = Locale.getDefault(Locale.Category.FORMAT);
// 根据Locale加载语言资源
ResourceBundle bundle = ResourceBundle
.getBundle("com.lousen.test_5//myMess" , currentLocale);
// 取得已加载的语言资源文件中msg对应消息
String msg = bundle.getString("msg");
// 使用MessageFormat为带占位符的字符串传入参数
System.out.println(MessageFormat.format(msg, "lousenjay" , new Date()));
/*使用NumberFormat格式化数字*/
//格式化货币
System.out.println(NumberFormat.getCurrencyInstance(currentLocale).format(db));
//格式化整数
System.out.println(NumberFormat.getIntegerInstance(currentLocale).format(db));
//格式化通用数值
System.out.println(NumberFormat.getNumberInstance(currentLocale).format(db));
//格式化百分数
System.out.println(NumberFormat.getPercentInstance(currentLocale).format(db));
/*使用DateFormat格式化日期
* 可通过FULL、LONG、SHORT、MEDUIM四个DateFormat的静态常量来控制样式*/
//格式化日期
System.out.println(DateFormat.getDateInstance(SHORT, currentLocale).format(dt));
//格式化时间
System.out.println(DateFormat.getTimeInstance(LONG, currentLocale).format(dt));
//格式化日期、时间
System.out.println(DateFormat.getDateTimeInstance(LONG,LONG, currentLocale).format(dt));
/*使用DateTimeFormatter格式化日期*/
//直接使用常量创建DateTimeFormatter个格式器
DateTimeFormatter formatter1 = DateTimeFormatter.ISO_LOCAL_DATE;
DateTimeFormatter formatter2 = DateTimeFormatter.ISO_LOCAL_TIME;
DateTimeFormatter formatter3 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//使用本地化风格来创建DateTimeFormatter格式器
DateTimeFormatter formatter4 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
DateTimeFormatter formatter5 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG,FormatStyle.FULL);
//根据模式字符串来创建DateTimeFormatter格式器
DateTimeFormatter formatter6 = DateTimeFormatter.ofPattern("Gyyyy%%MMM%%dd HH:MM:ss");
LocalDateTime date = LocalDateTime.now();
System.out.println(formatter1.format(date));
}
}

最后更新: 2020年07月27日 03:40

原始链接: https://www.lousenjay.top/2017/10/29/Java复习笔记/