Java实现才彩票近几年数字出现的最多的数字
package com.example.demo.controller;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("/")
public class LotteryController {
public static String API_URL = "http://www.huiniao.top/interface/home/lotteryHistory";
/**
* 返回出现大乐透/双色球次数最多的号码
* @param type 1大乐透 2双色球 默认大乐透
* @param year 年份
* @return @link String
*/
@GetMapping("/lottery")
public String getMostFigureLottery(@RequestParam("type") Integer type, @RequestParam("year") Integer year) {
Map<String, Object> params = new HashMap<>(16);
//组合参数 dlt大乐透 ssq双色球
if(2 == type){
params.put("type", "ssq");
}else{
params.put("type", "dlt");
}
params.put("page", 1);
// 从20240320到20230320共151期 2023-03-20 23030 2023-12-30 23150 2024-03-20 24031
// System.out.println("大乐透从上一年20230320到20240320共经历了"+(23150-23030+31)+"期");
params.put("limit", 151*year);
String queryParams = urlencode(params);
String response = doGet(API_URL, queryParams);
List<Integer> oneList = new ArrayList<>();
List<Integer> twoList = new ArrayList<>();
List<Integer> threeList = new ArrayList<>();
List<Integer> fourList = new ArrayList<>();
List<Integer> fiveList = new ArrayList<>();
List<Integer> sixList = new ArrayList<>();
List<Integer> sevenList = new ArrayList<>();
String message = "";
try {
JSONObject jsonObject = JSONObject.fromObject(response);
int error_code = jsonObject.getInt("code");
if (error_code == 1) {
JSONObject result = jsonObject.getJSONObject("data");
JSONObject data = result.getJSONObject("data");
Object list = data.get("list");
if (list instanceof JSONArray) {
JSONArray listArray = (JSONArray) list;
// 遍历数组,处理每个元素
for (int i = 0; i < listArray.size(); i++) {
JSONObject item = listArray.getJSONObject(i);
// 处理item
oneList.add(item.getInt("one"));
twoList.add(item.getInt("two"));
threeList.add(item.getInt("three"));
fourList.add(item.getInt("four"));
fiveList.add(item.getInt("five"));
sixList.add(item.getInt("six"));
sevenList.add(item.getInt("seven"));
}
}
Integer one = findMostFrequent(oneList);
Integer two = findMostFrequent(twoList);
Integer three = findMostFrequent(threeList);
Integer four = findMostFrequent(fourList);
Integer five = findMostFrequent(fiveList);
Integer six = findMostFrequent(sixList);
Integer seven = findMostFrequent(sevenList);
message = "近"+year+"年数字出现最多的是:"+one+","+two+","+three+","+four+","+five+","+six+","+seven;
System.out.println(message);
} else {
System.out.println("调用接口失败:" + jsonObject.getString("code"));
}
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
/**
* get方式的http请求
*
* @param httpUrl 请求地址
* @return 返回结果
*/
public static String doGet(String httpUrl, String queryParams) {
HttpURLConnection connection = null;
InputStream inputStream = null;
BufferedReader bufferedReader = null;
String result = null;// 返回结果字符串
try {
// 创建远程url连接对象
URL url = new URL(new StringBuffer(httpUrl).append("?").append(queryParams).toString());
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
// 设置连接方式:get
connection.setRequestMethod("GET");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(5000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(6000);
// 发送请求
connection.connect();
// 通过connection连接,获取输入流
if (connection.getResponseCode() == 200) {
inputStream = connection.getInputStream();
// 封装输入流,并指定字符集
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
// 存放数据
StringBuilder sbf = new StringBuilder();
String temp;
while ((temp = bufferedReader.readLine()) != null) {
sbf.append(temp);
sbf.append(System.getProperty("line.separator"));
}
result = sbf.toString();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();// 关闭远程连接
}
}
return result;
}
/**
* 将map型转为请求参数型
*
* @param data
* @return
*/
public static String urlencode(Map<String, ?> data) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, ?> i : data.entrySet()) {
try {
sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
String result = sb.toString();
result = result.substring(0, result.lastIndexOf("&"));
return result;
}
/**
* 取出list重复最多的数字
* @param list
* @return
*/
public static Integer findMostFrequent(List<Integer> list) {
Map<Integer, Integer> frequencyMap = new HashMap<>();
for (Integer num : list) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
}
int maxFrequency = 0;
Integer mostFrequentNumber = null;
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
if (entry.getValue() > maxFrequency) {
maxFrequency = entry.getValue();
mostFrequentNumber = entry.getKey();
}
}
return mostFrequentNumber;
}
}