展开全部
/**
* 第一题
*
* @author 我为足球32313133353236313431303231363533e58685e5aeb931333337383865狂
*/
import java.util.Random;
import java.text.DecimalFormat;
/**
* 课程类 课程编号设定为7位随机任意字符串编码
*/
class KeCheng {
public String KeChengID;
public String KeChengName;
/**
* 以下为私有变量及其公开方法
*/
private double FenShu; // 分数应设置为私有,保证安全
public void setFenShu(double fenshu) {
this.FenShu = fenshu;
}
public double getFenShu() {
return this.FenShu;
}
/**
* 辅助指定输出格式
*/
public void print() {
if (this.getFenShu()
DecimalFormat df = new DecimalFormat("#0.00");
System.out.println("| 编号:" + this.KeChengID + " | 课名:"
+ this.KeChengName + " | 分数:" + df.format(this.getFenShu())
+ " | \n");
} else {
DecimalFormat df = new DecimalFormat("#00.00");
System.out.println("| 编号:" + this.KeChengID + " | 课名:"
+ this.KeChengName + " | 分数:" + df.format(this.getFenShu())
+ " | \n");
}
}
/**
* 以下为构造方法
*/
KeCheng() {
String kcID = (new GenerateSuiJiBianHao()).randomString(7);// 此处7指课程编号长度,可自由修改
this.KeChengID = kcID;
this.KeChengName = "未指定";
this.setFenShu(-99.99);
print();
}
KeCheng(String kechengName, double fenshu) {
String kcID = (new GenerateSuiJiBianHao()).randomString(7);
this.KeChengID = kcID;
this.KeChengName = kechengName;
this.setFenShu(fenshu);
print();
}
}
/**
* 以下产生随机课程编号的类
*/
class GenerateSuiJiBianHao {
private static Random randGen = null;
private static char[] numbersAndLetters = null;
public static final String randomString(int length) {
if (length
return null;
}
if (randGen == null) {
randGen = new Random();
numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz"
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
}
char[] randBuffer = new char[length];
for (int i = 0; i
randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
}
return new String(randBuffer);
}
}
/**
* 以下为主类
*/
public class Course {
public static void main(String[] args) {
// 演示示例
System.out.print("\n");
System.out.println("——————————————————————————————————————\n");
KeCheng kc1 = new KeCheng();
KeCheng kc2 = new KeCheng("自然地理学", 99.5);
// 10个随机指定课程名(四位字符串)和分数的示例
System.out.println("—————————随机指定课程名(四位字符串)和分数示例——————————\n");
for (int i = 1; i
KeCheng kcGroup = new KeCheng(
(new GenerateSuiJiBianHao()).randomString(4),
(new Random().nextDouble()) * 100);
}
}
}
/*
等我下午踢完球晚上回来继续答
*/