public class TestStudy {
/**
* 打印九九乘法表
* @param args
*/
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i+"x"+j + "=" + i*j +" ");
}
System.out.println();
}
}
}
/*
编程实现所有三位数水仙花数的打印
*/
public class TestWaterFlower{
public static void main(String[] args){
//1.打印所有的三位数
for(int i = 100; i <= 999; i++){
//2.拆分该三位数中的每个数字 123
int ia = i / 100; //获取百位数
int ib = i % 100 / 10; //获取十位数
int ic = i % 10; //获取个位数
//3.判断i代表的数值是否为水仙花数,若是则打印
if((ia*ia*ia + ib*ib*ib + ic*ic*ic) == i){
System.out.println(i);
}
}
}
}
数组增删不便,牵一发而动全身
查找方便,支持下标访问
Arrays.sort(arr);调用方法进行数组排序,底层实现方式是:快速排序
package cn.xdl.test;
import java.util.Arrays;
import java.util.Random;
public class TestStudy {
public static void main(String[] args) {
int[] arr = new int[6];
//调用随机函数
Random random = new Random();
for (int i = 0; i < 6; i++) {
//调用随机函数生成1~33之间的随机数并对数组进行赋值
arr[i] = random.nextInt(33)+1;
//针对每次生成的随机号码进行去重操作
for (int j = i-1 ; j >=0 ;j--) {
//如果有相等,i--重新执行生成操作
if(arr[i] == arr[j]){
i--;
break;
}
}
}
Arrays.sort(arr);//排序
int red = random.nextInt(16)+1;
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]+" ");
}
System.out.println(red);
}
}
package cn.yimu.goband;
/*
编程实现控制台版的五子棋游戏,支持两人对战
*/
import java.util.Scanner;
public class Goband {
//自定义二维数组来描述棋盘,默认初始值为0
int[][] chessBoard = new int[16][16];
//自定义成员方法来绘制棋盘
void paint(){
//1.先绘制棋盘中第一行的坐标信息,也就是列坐标信息
for(int i = 0; i < 17; i++){
if(i == 0){
System.out.print(" ");
}
else{
//按照十六进制的格式打印i-1的数值
System.out.printf("%x ", i-1);
}
}
System.out.println();
//2.绘制棋盘中除了第一行之外的其他部分以及行坐标信息
for(int i = 0; i < 16; i++){
//用于打印行坐标信息
System.out.printf("%x ", i);
for(int j = 0; j < 16; j++){
//刚开始棋盘中的所有内容都是+,因此直接打印
if(chessBoard[i][j] == 0){
System.out.print("+ ");
}
else if(chessBoard[i][j] == 1){
System.out.print("● ");
}
else{
System.out.print("○ ");
}
}
//打印完毕一行的所有内容之后进行换行
System.out.println();
}
}
//自定义成员方法来提示黑方和白方分别下棋
void play(){
//定义标志位来进行黑方和白方的切换,true代表黑方,false代表白方
boolean flag = true;
//不断地分别提示黑方和白方下棋
while(true){
System.out.println("请" + (flag ? "黑方": "白方") + "输入落子坐标(x y):");
Scanner sc = new Scanner(System.in);
int ix = sc.nextInt();
int iy = sc.nextInt();
//根据用户输入的坐标来调整棋盘中的图案,策略为改变数组的元素值
if(flag){
//当黑方落子时就将数组中对应元素值改为1
chessBoard[ix][iy] = 1;
}else{
//当白方落子时就将数组中对应元素改为2
chessBoard[ix][iy] = 2;
}
//重新绘制图案
paint();
//判断当前方是否胜利,若胜利就立刻结束游戏
if(judge(ix, iy)){
System.out.println("恭喜" + (flag ? "黑方": "白方") + "胜利了!");
break;
}
//此时切换下棋方
flag = !flag;
}
}
//自定义成员方法来判断用户是否获胜,获胜的规则是:任意相同颜色的5个棋子连成一线
boolean judge(int ix, int iy){
//1.判断竖向是否连成一线,则需要以该点为中心向上四个点向下四个点
//声明变量来统计竖向相同颜色棋子的个数,先统计向上同色棋子的个数
int count = 1;
for(int i = ix-1; i >= 0 && i >= ix-4; i--){
//若当前点代表的棋子与上述某个点代表的棋子不一样,则向上统计结束
if(chessBoard[ix][iy] != chessBoard[i][iy]){
break;
}
count++;
}
System.out.println("count1 = " + count);
//再统计向下颜色相同的个数
for(int i = ix+1; i <= 15 && i <= ix+4; i++){
if(chessBoard[ix][iy] != chessBoard[i][iy]){
break;
}
count++;
}
System.out.println("count2 = " + count);
//... ...
return count >= 5;
//当所有可能胜利的情况都排除了,那么肯定是失败了
}
}
启动类:
public class TestGoband {
public static void main(String[] args) {
//1.声明一个GoBand类型的引用指向该类的对象
Goband gb = new Goband();
//2.调用成员方法来绘制棋盘
gb.paint();
//3.调用成员方法来进行下棋
gb.play();
}
}
所谓递归就是自己调用自己的方法:
递归有可能会大幅简化代码的编写。递归要考虑性能问题,有些时候可以使用循环而不是递归。
递归的使用原则:
1 必须有退出条件。
2 必须使问题变简单,而不是复杂化。
public class TestDG {
/**
* 自定义算法实现阶乘
* 阶乘:1*2*3*4...*n
* @param n
* @return
*/
int factorial(int n){
if(n == 1){
return 1;
}
return n*factorial(n-1);
}
public static void main(String[] args) {
TestDG dg = new TestDG();
System.out.println(dg.factorial(4));
}
}
public class TestDG {
/**
* 自定义递归实现费式数列
* 费式数列:也叫斐波那契数列:即从1开始,后一个数等于前两个数之后1、1、2、3、5、8、13、21...
* @return
*/
int sequence(int n){
if(n == 1 || n == 2){
return 1;
}
return sequence(n-1)+sequence(n-2);
}
public static void main(String[] args) {
TestDG dg = new TestDG();
System.out.println(dg.sequence(45));
}
}
饿汉式:
public class Singleton(){
//1 私有化构造方法
private Singleton(){}
//2 提供一个本类类型的引用指向本类的对象
private static Singleton sin = new Singleton();
//3 提供一个公用的方法将sin的数值返回出去
public static Singleton getInstance(){
return sin;
}
}
懒汉式:
public calss Singleton(){
private Singleton(){};
private static Singleton sin = null;
public static Singleton getInstance(){
if(sin == null){
sin = new Singleton();
}
return sin;
}
}
Thread th = new Thread(){
编写代码;
};
自定义异常类:继承自Exception
public class AgeException extends Exception{
编写代码;
}
import java.io.File;
public class PrintList {
/**
* 自定义方法打印参数指定目录及其子目录中的文件
* @param file
*/
public static void print(File file){
// 判断是否是普通文件
if(file.isFile()){
System.out.println(file.getName());
}else if(file.isDirectory()){
//获取目录中的所用内容
File[] fArr = file.listFiles();
//使用递归,打印所有
for (File f: fArr) {
print(f);
}
}
}
public static void main(String[] args) {
File file = new File("目录或者文件名");
PrintList.print(file);
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileCopy {
public static void main(String[] args) {
try {
//文件流对象,关联需要拷贝文件
FileInputStream fis = new FileInputStream("输出文件名");
FileOutputStream fos = new FileOutputStream("输入文件名");
//准备一个合理的缓存区,每次只需要将缓存区写满再写入
byte[] brr = new byte[1024*8];
int res =0;
while((res = fis.read(brr)) != -1){
fos.write(brr,0,res);
}
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
方式一:继承Thread类
/**
* 1 继承Thread类
* 2 重写run方法
* 3 调用start()开启线程
* @author liangmu
*/
public class WayThread extends Thread{
@Override
public void run(){
for (int i = 0; i < 26; i++) {
System.out.println(“线程一:” + i);
}
}
public static void main(String[] args) {
Thread th = new WayThread();
th.start();//启动一个线程的方法
for (int i = 0; i < 21; i++) {
System.out.println(“main线程:”+ i);
}
}
}
方式二:实现Runnable接口
/**
* 方式二
* 1 实现Runnable接口
* 2 重写run方法
* 3 开启线程
* @author liangmu
*
*/
public class WayThread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("子线程:" + i);
}
}
public static void main(String[] args) {
Runnable ra = new WayThread();
Thread th = new Thread(ra);
th.start();
for (int i = 0; i < 100; i++) {
System.out.println("主线程方法:" + i);
}
}
}
方式三:匿名内部类
/**
* 方式三:
* 使用匿名内部类的方式调用
* @author liangmu
*
*/
public class WayThread{
public static void main(String[] args) {
Thread th = new Thread(){
public void run(){
for (int i = 0; i < 30; i++) {
System.out.println("线程一" + i);
}
}
};
th.start();
new Thread(){
public void run(){
for (int i = 0; i < 30; i++) {
System.out.println("线程二" + i);
}
}
}.start();
}
}
客户端:
public class TestClient {
public static void main(String[] args) {
try{
//1.创建Socket类型的对象,并提供服务器的IP地址和端口号
Socket s = new Socket("localhost", 8888);
//2.使用输入输出流进行通信
PrintStream ps = new PrintStream(s.getOutputStream());
BufferedReader br = new BufferedReader(
new InputStreamReader(s.getInputStream()));
Scanner sc = new Scanner(System.in);
while(true){
//String msg = "在吗?";
System.out.println("请输入要发送的内容:");
String msg = sc.nextLine();
ps.println(msg);
System.out.println("发送数据成功!");
//判断客户端给服务器发送的内容是否为"bye"
if("bye".equalsIgnoreCase(msg)){
System.out.println("再见吧!");
break;
}
//接收服务器回发的消息
String str = br.readLine();
System.out.println("服务器发来的数据是:" + str);
}
//3.关闭Socket并释放有关的资源
br.close();
ps.close();
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
服务端:
import java.net.ServerSocket;
import java.net.Socket;
public class TestServer {
public static void main(String[] args) {
try {
//创建ServerSocket类型的对象,并提供端口号
ServerSocket ss = new ServerSocket(8888);
while(true){
System.out.println("等待客户端连接请求...");
Socket s = ss.accept();
System.out.println(s.getInetAddress()+"连接成功");
ServerThread thread = new ServerThread(s);
Thread th = new Thread(thread);
th.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
线程:
public class ServerThread implements Runnable{
private Socket s;
public ServerThread(Socket s){
this.s = s;
}
@Override
public void run(){
try{
//3.使用输入输出流进行通信
BufferedReader br = new BufferedReader(
new InputStreamReader(s.getInputStream()));
PrintStream ps = new PrintStream(s.getOutputStream());
while(true){
String str = br.readLine();
System.out.println("客户端" + s.getInetAddress()
+ "发来的数据是:" + str);
//判断客户端发来的数据是否为"bye",若是则结束通信
if("bye".equalsIgnoreCase(str)){
System.out.println("话不投机半句多!客户端"
+ s.getInetAddress() + "已下线!");
break;
}
//实现服务器向客户端回发消息
ps.println("I received!");
System.out.println("服务器发送数据成功!");
}
//4.关闭Socket并释放有关的资源
ps.close();
br.close();
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public class TestSort(){
public static void bubble(int[] arr){
//1.使用外层for循环来控制比较的轮数
for(int i=1;iarr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = false;
}
}
if(flag) break;
}
}
public static void main(String[] args){
int[] arr = {12,32,25,2,6,86,63,21,15,36};
TestSort.bubble(arr);
for(int i=0;i
设计模式: 设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。设计模式本质上就是一种固定的套路,使用在固定的场合中。
创建型模式 - 工厂方法模式、抽象工厂模式、单例设计模式。
结构型模式 - 装饰器模式、代理模式。
行为型模式 - 模板设计模式、观察者模式。
普通工厂模式:(邮件和短信的例子)
接口:
public interface Sender{
public void send();
}
接口实现类:
public class MailSender implements Sender {
@Override
public void send() {
System.out.println("邮件发送");
}
}
public class SmsSender implements Sender {
@Override
public void send() {
System.out.println("信息发送");
}
}
工厂类:
public class SendFactory {
//工厂方法
public Sender produce(String type){
if ("mail".equals(type)) {
return new MailSender();
} else if ("sms".equals(type)) {
return new SmsSender();
} else {
System.out.println("请输入正确的类型!");
return null;
}
}
}
工厂测试类:
public class TestFactory {
public static void main(String[] args) {
SendFactory sendFactory = new SendFactory();
Sender sms = sendFactory.produce("sms");
sms.send();
}
}
多个工厂方法模式:
即在工厂类中添加多个工厂方式
静态工厂方法模式:
即将工厂类中的方法设置为静态的加static,不需要创建实例,可直接调用
工厂方法模式有一个问题就是,类的创建依赖工厂类,也就是说,如果想要拓展程序,必须对工厂类进行修改,这违背了闭包原则,所以,从设计角度考虑,有一定的问题,如何解决?就用到抽象工厂模式,创建多个工厂类,这样一旦需要增加新的功能,直接增加新的工厂类就可以了,不需要修改之前的代码

接口:
实现类:
工厂类实现接口方法:
测试类:
c/s架构编写,SE阶段最具代表性的项目