Java为开发者提供了基本的数学工具类——Math类。Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。本实验我们将利用这些方法,对数值进行复杂的运算。
Java数学工具包java.lang.Math类
Java SE官方API文档 - Java.lang.Math
Java SE官方API文档 - Java.lang.Random
1. Math类中的常量
Math类自带了两个常量,分别是:
比任何其他值都更接近 e(即自然对数的底数)的 double 值。
比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。
public class ShowConstant {
public static void main(String[] args) {
System.out.println("The natural logarithm is " + Math.E);
System.out.println("The circumference ratio is " + Math.PI);
}
}
2. 如何利用数学工具
2.1 三角函数计算
public class TriFunction {
public static void main(String[] args)
{
System.out.println("sin30 = "+Math.sin(Math.PI/6));
System.out.println("cos30 = " + Math.cos(Math.PI / 6));
System.out.println("tan30 = " + Math.tan(Math.PI / 6));
}
}
sin30 = 0.49999999999999994
cos30 = 0.8660254037844387
tan30 = 0.5773502691896257
2.2 生成彩票号码
目前市面上彩票的种类很多,我们就以大乐透为例吧。大乐透的规则是:从1~35中随机选取5个不重复的数字,从1~12中随机选取2个不重复的数字,这些数字组成了一个七位数。如果你买的号码与摇出来的号码相同,那么你一夜之间可能就不会当程序员了。
对于该项目所需要用到的API,你需要引入下面一些包:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
由于彩票号码的前半段和后半段的规则和个数略有不同,所以我们分别创建生成它们的首先来创建生成前段号码的方法getHeadNumber(),该方法需要从1到36中随机选取一个数作为号码(即摇号的过程)。对于个位数,为了显示上更整齐,所以我们在其十位再放一个“0”。再者,为了结果更加易读,我们还需要调用Collections.sort()方法对结果进行排序。
代码片段如下:
0) {
list.add("0" + i + " ");
// 在集合中添加0~9的号码,因为是个位数,为了显示上更加人性化,所以需要在十位的地方添加“0”
} else {
list.add("" + i + " ");
// 向集合添加大于9的号码,即添加双位数
}
}
int roundIndex = 0;
// 创建并初始化随机数
List<String> lotteryList = new ArrayList<String>();
// 保存前段号码的List集合
for (int j = 0; j < 5; j++) {
int amount = list.size(); // 获取前半段号码的个数
Random r = new Random(); // 创建并实例化Random的对象
roundIndex = r.nextInt(amount); // 获取一个0到amount-1的随机数
lotteryNumber = list.get(roundIndex); // 获取彩票数字,即摇号的号码
lotteryList.add(lotteryNumber); // 将彩票号码添加lotteryList中
list.remove(roundIndex); // 移除刚刚产生的号码
}
Collections.sort(lotteryList);
// 对前半段号码进行排序,排序的目的是为了让结果更具可读性
return lotteryList;
//返回前半段号码
}
此处用到了Random()方法,在创建一个Random对象r后,你可以调用该对象的nextInt()方法得到一个随机数。r.nextInt(amount);中的amount是随机数的上限,产生的随机数为0到100(不含100)的整数。
和前半段号码类似,我们接着来写生成后半段号码的方法getRearNum(),代码片段如下:
public List<String> getRearNumber() {
List<String> list = new ArrayList<String>();
// 创建后半段号码集合,也就是最后两个球的数字
String lotteryNumber = "";
for (int i = 1; i < 13; i++) {
// 初始化后半段号码集合
if (i < 10) {
list.add("0" + i + " ");
// 添加0~9的号码,原理同前半段
} else {
list.add("" + i + " ");
// 添加大于9的号码
}
}
int roundIndex = 0;
//创建并初始化随机数
List<String> lotteryList = new ArrayList<String>();
// 保存后半段号码的List集合
for (int j = 0; j < 2; j++) {
int amount = list.size(); // 获取后半段号码的个数
Random r = new Random(); // 创建并实例化Random的对象
roundIndex = r.nextInt(amount); // 获取一个0到amount-1的随机数
lotteryNumber = list.get(roundIndex); // 摇号
lotteryList.add(lotteryNumber); // 将彩票号码添加lotteryList中
list.remove(roundIndex); // 移除刚刚产生的号码
}
Collections.sort(lotteryList);
// 对后半段号码进行排序
return lotteryList;
}
现在,根据规则我们已经能够产生对应的数字了。再接再厉,我们再做一个生成最终结果的方法,把这两个号段组装成整体的号码,并且按照用户的要求产生多少组号码,在控制台输出它们。
下面给出了这个方法的代码片段:
public void generateLottery(String groupNum) {
int groupNumber = 0;
//为了避免不必要的错误,一般在创建变量时都要为其赋初始值
groupNumber = Integer.parseInt(groupNum);
StringBuilder sbuiler = new StringBuilder();
// 创建字符串生成器对象,使用字符串生成器能够较为方便地在字符串中追加内容
for (int i = 0; i < groupNumber; i++) {
List<String> startList = getHeadNumber();
// 获得彩票前半段号码的集合
List<String> endList = getRearNumber();
// 获得彩票后半段号码的集合
for (int m = 0; m < startList.size(); m++) {
sbuiler.append(startList.get(m));
// append()即为追加方法,用于添加彩票的前半段号码到字符串生成器中
}
sbuiler.append(" ");
for (int n = 0; n < endList.size(); n++) {
sbuiler.append(endList.get(n));
// 添加彩票的后半段号码到字符串生成器中
}
sbuiler.append("\n");
}
System.out.println(sbuiler.toString());
//将每组生成好的彩票号码即时输出
}
为了验证我们的实现无误,在同一个包中创建一个名为LotteryTest的测试类,编写其main()方法。在其中创建一个彩票对象并调用其产生号码的方法。
LotteryTest.java中的内容为:
import java.util.Scanner;
public class LotteryTest {
public static void main(String[] args) {
Lottery l = new Lottery();
Scanner scan = new Scanner(System.in);// 创建扫描器
System.out.println("Please input the number of lottery group(s) that you want to generate : ");
// 获得用户输入的需要生成的中奖号码个数
String groupNum = scan.nextLine();
l.generateLottery(groupNum);
}
}
检查一下代码,点击编译并运行,在控制台输入你要生成多少组的彩票号码,按下回车键你就能看到生成的结果。