十年网站开发经验+多家企业客户+靠谱的建站团队
量身定制+运营维护+专业推广+无忧售后,网站问题一站解决
上面wuzhikun12同学写的不错,但我想还不能运行,并且还不太完善。我给个能运行的:(注意:文件名为:Test.java)
//要实现对象间的比较,就必须实现Comparable接口,它里面有个compareTo方法
//Comparable最好使用泛型,这样,无论是速度还是代码量都会减少
@SuppressWarnings("unchecked")
classStudentimplementsComparableStudent{
privateStringstudentNo;//学号
privateStringstudentName;//姓名
privatedoubleenglishScore;//英语成绩
privatedoublecomputerScore;//计算机成绩
privatedoublemathScore;//数学成绩
privatedoubletotalScore;//总成绩
//空构造函数
publicStudent(){}
//构造函数
publicStudent(StringstudentNo,StringstudentName,doubleenglishSocre,doublecomputerScore,doublemathScore){
this.studentNo=studentNo;
this.studentName=studentName;
this.englishScore=englishSocre;
this.computerScore=computerScore;
this.mathScore=mathScore;
}
//计算总成绩
publicdoublesum(){
this.totalScore=englishScore+computerScore+mathScore;
returntotalScore;
//计算评测成绩
publicdoubletestScore(){
returnsum()/3;
//实现compareTO方法
@Override
publicintcompareTo(Studentstudent){
doublestudentTotal=student.getTotalScore();
returntotalScore==studentTotal0:(totalScorestudentTotal1:-1);
//重写toString方法
publicStringtoString(){
return"学号:"+this.getStudentNo()+"姓名:"+this.getStudentName()+"英语成绩:"+this.getEnglishScore()+"数学成绩:"+this.getMathScore()+"计算机成绩:"+this.getComputerScore()+"总成绩:"+this.getTotalScore();
//重写equals方法
publicbooleanequals(Objectobj){
if(obj==null){
returnfalse;
if(!(objinstanceofStudent)){
Studentstudent=(Student)obj;
if(this.studentNo.equals(student.getStudentName())){//照现实来说,比较是不是同一个学生,应该只是看他的学号是不是相同
returntrue;
}else{
/*以下为get和set方法,我个人认为,totalScore的set的方法没必要要,因为它是由其它成绩计算出来的
在set方法中,没设置一次值,调用一次sum方法,即重新计算总成绩
*/
publicStringgetStudentNo(){
returnstudentNo;
publicvoidsetStudentNo(StringstudentNo){
sum();
publicStringgetStudentName(){
returnstudentName;
publicvoidsetStudentName(StringstudentName){
publicdoublegetEnglishScore(){
returnenglishScore;
publicvoidsetEnglishScore(doubleenglishScore){
this.englishScore=englishScore;
publicdoublegetComputerScore(){
returncomputerScore;
publicvoidsetComputerScore(doublecomputerScore){
publicdoublegetMathScore(){
returnmathScore;
publicvoidsetMathScore(doublemathScore){
publicdoublegetTotalScore(){
//Student子类学习委员类的实现
classStudentXWextendsStudent{
//重写父类Student的testScore()方法
returnsum()/3+3;
publicStudentXW(){}
//StudentXW的构造函数
publicStudentXW(StringstudentNo,StringstudentName,doubleenglishSocre,doublecomputerScore,doublemathScore){
super(studentNo,studentName,englishSocre,computerScore,mathScore);
//Student子类班长类的实现
classStudentBZextendsStudent{
returnsum()/3+5;
publicStudentBZ(){}
publicStudentBZ(StringstudentNo,StringstudentName,doubleenglishSocre,doublecomputerScore,doublemathScore){
//测试类
publicclassTest{
publicstaticvoidmain(String[]args){
//生成若干个student类、StudentXW类、StudentBZ类
Studentstudent1=newStudent("s001","张三",70.5,50,88.5);
Studentstudent2=newStudent("s002","李四",88,65,88.5);
Studentstudent3=newStudent("s003","王五",67,77,90);
StudentXWstudent4=newStudentXW("s004","李六",99,88,99.5);
StudentBZstudent5=newStudentBZ("s005","朱漆",56,65.6,43.5);
Student[]students={student1,student2,student3,student4,student5};
for(inti=0;istudents.length;i++){
doubleavgScore=students[i].testScore();
System.out.println(students[i].getStudentName()+"学生的评测成绩为:"+avgScore+"分");
运行结果为:
张三学生的评测成绩为:69.66666666666667分
李四学生的评测成绩为:80.5分
王五学生的评测成绩为:78.0分
李六学生的评测成绩为:98.5分
朱漆学生的评测成绩为:60.03333333333333分
packagecom.regex;
importjava.io.*;
importjava.net.URLDecoder;
importjava.util.regex.*;
publicclassRegex{
privateintREMARK=0;
privateintLOGIC=0;
privateintPHYSIC=0;
booleanstart=false;
/**
*@paramargs
publicstaticvoidmain(String[]args){//测试方法
//TODOAuto-generatedmethodstub
Regexre=newRegex();
re.regCount("Regex.java");
System.out.println("remarkLine:"+re.REMARK);
System.out.println("logicLine:"+re.LOGIC);
System.out.println("physicLine:"+re.PHYSIC);
}/**
*@authorBlueDance
*@params
*@deprecatedcount
publicvoidregCount(Strings){
Stringurl=null;
try{
url=URLDecoder.decode(this.getClass().getResource(s).getPath(),"UTF-8");
}catch(Exceptione){
e.printStackTrace();
//TODO:handleexception
BufferedReaderbr=newBufferedReader(newFileReader(newFile(url)));
Strings1=null;
while((s1=br.readLine())!=null){
PHYSIC++;
if(CheckChar(s1)==1){
REMARK++;
System.out.println("纯注释行:"+s1);
if(CheckChar(s1)==2){
LOGIC++;
System.out.println("非纯注释行:"+s1);
if(CheckChar(s1)==3)
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
}catch(IOExceptione){
*
*@returnint
*@versionchecks
publicintCheckChar(Strings){
if(s!=null)
s1=s.trim();
//System.out.println(regCheck(s1,re));
if(regCheck(s1,"(//.*)"))//判断//开头的为纯注释行
return1;
if(regCheck(s1,"(.*[;{})]*//.*)"))//判断不是//开头的非纯注释行
return2;
if(regCheck(s1,"(//*.*)")){//判断/*开头的纯注释行
start=true;
if(regCheck(s1,"(.*[;{})]//*.*)")){//判断不是/*开头的非纯注释行
if(regCheck(s1,"(.**/*/)")){//判断*/结尾的纯注释行
start=false;
if(regCheck(s1,"(.**/*/.*)")!strCheck(s1)){//判断不是*/结尾的非纯注释行
if(strCheck(s1)){
if(start==true)//状态代码,start即/*开始时start=true*/结束时为false
return3;//ssssllll
}//aeee
*@paramre
*@returnboolean
publicbooleanregCheck(Strings,Stringre){//正则表达试判断方法
returnPattern.matches(re,s);
publicbooleanstrCheck(Strings){//中间有*/的字符判断此方法最关键
if(s.indexOf("*/")0){
intcount=0;
Stringy[]=s.split("/*/");
booleanboo[]=newboolean[y.length];
for(inti=0;iy.length-1;i++){
charc[]=y[i].toCharArray();
for(intj=0;jc.length;j++){
if(c[j]=='\\'c[j+1]=='"'){
count++;
if(count%2==0){
if(countNumber("\"",y[i])%2!=0){
boo[i]=true;
boo[i]=false;
if(countNumber("\"",y[i])%2==0){
for(inti=0;iboo.length;i++){
if(!boo[i])
String[]k=y.split(s);
if(y.lastIndexOf(s)==(y.length()-s.length()))
count=k.length;
else
count=k.length-1;
if(count==0)
System.out.println("字符串\""+s+"\"在字符串\""+y+"\"没有出现过");
returncount;
return-1;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassGoodLuckyextendsJFrameimplementsActionListener{
JTextFieldtf=newJTextField();//实例化一个文本域
//设置两个按钮
JButtonb1=newJButton("开始");
JButtonb2=newJButton("停止");
booleanisGo=false;
publicGoodLucky(){
b1.setActionCommand("start");//在开始按钮上设置一个动作监听start
JPanelp=newJPanel();//实例化一个可视化容器
//将两个按钮添加到可视化容器上面,用add方法
p.add(b1);
p.add(b2);
//在两个按钮上增加监听的属性,自动调用下面的监听处理方法actionPerformed(ActionEvente),如果要代码有更好的可读性,可用内部类实现动作
//监听处理。
b1.addActionListener(this);
b2.addActionListener(this);
//将停止按钮设置为不可编辑(即不可按的状态)
b2.setEnabled(false);
this.getContentPane().add(tf,"North");//将上面的文本域放在面板的北方,也就是上面(上北下南左西右东)
this.getContentPane().add(p,"South");//将可视化容器pannel放在南边,也就是下面
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置用户在此窗体上发起"close"时默认执行的操作,参数EXIT_ON_CLOSE是使用Systemexit方法退出应用程序。仅在应用程序中使用
this.setSize(300,200);//设置面板大小,宽和高
this.setLocation(300,300);//设置面板刚开始的出现的位置
Cursorcu=newCursor(Cursor.HAND_CURSOR);//用指定名称创建一个新的定制光标对象,参数表示手状光标类型
this.setCursor(cu);//为指定的光标设置光标图像,即设置光标图像为上面所创建的手状光标类型
this.setVisible(true);//将面板可视化设置为true,即可视,如果为false,即程序运行时面板会隐藏
tf.setText("welcomeyou!");//设置面板的标题为欢迎
this.go();//调用go方法
publicvoidgo(){
while(true){//这里是死循环,也就是说用户不点击停止按钮的话他一直循环出现随机数,直到用户点击停止按钮循环才能推出,具体流程在actionPerformed方法中控制。
if(isGo==true){//上面所定义的isGo的初始值为false,所以程序第一次到此会跳过
Strings="";//设置空字符串
for(intj=1;j=7;j++){//产生7个随机数
inti=(int)(Math.random()*36)+1;//每个随机数产生方式,这里定义灵活,可以自由定义随机数产生的方式
if(i10){
s=s+"0"+i;//如果产生的随机数小于10的话做处理:这里就牵扯到一个重要的概念,简单叙述一下:
/*
当一个字符串与一个整型数项相加的意思是连接,上面的s=s+"0"+i的意思是字符串s链接0再连接整型i值,而不会导致0和整型的i相加,
产生的效果为s0i,由于s为空字符串(上面定义过的),所以当i小于零时,在个位数前面加上0,比如产生的随机数i为7的话,显示效果为07.
s=s+""+i;//如果产生的随机数比10打的话,那么加上空格显示,即数字和数字之间有个空格
//以上循环循环七次,以保证能出现7个随机数
tf.setText(s);//将产生的随机数全部显示在文本域上,用文本域对象tf调用它的设置文本的方法setText(String)实现。
//以下为线程延迟
Thread.sleep(10);//线程类同步方法sleep,睡眠方法,括号里的单位为ms。
}catch(java.lang.InterruptedExceptione){
e.printStackTrace();//异常捕获,不用多说。
publicvoidactionPerformed(ActionEvente){//传入一个动作事件的参数e
Strings=e.getActionCommand();//设置字符串s来存储获得动作监听,上面的start
以下这个条件语句块的作用为:用户点击开始后(捕获start,用方法getActionCommand()),将命令触发设置为true,从而执行上面的go方法中的循环体(因为循环体中要求isGo参数为true,而初始为false)。
执行循环快产生随机数,并将开始按钮不可编辑化,而用户只可以使用停止按钮去停止。如果用户按下停止时,也就是没有传入参数“start”的时候,
执行else语句块中的语句,isGo设置为false,将不执行上面go中的循环语句块,从而停止产生随机数,并显示,并且把开始按钮设置为可用,而把
停止按钮设置为不可用,等待用户按下开始再去开始新一轮循环产生随机数。
if(s.equals("start")){//如果捕获到start,也就是用户触发了动作监听器,那么下面处理
isGo=true;//设置isGo为true
b1.setEnabled(false);//将开始按钮设置为不可用
b2.setEnabled(true);//将停止按钮设置为可用
isGo=false;//将isGo设置为false,isGo为循环标志位
b2.setEnabled(false);//设置停止按钮为不可用(注意看是b2,b2是停止按钮)
b1.setEnabled(true);//设置开始按钮为可用
newGoodLucky();//产生类的实例,执行方法
//我写了一个程序,你把文字复制到文本框中点转码按钮,就可以了
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JTextArea;
publicclassClass1extendsJFrame{
privatefinalclassATimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
jta.setText(fixString(jta.getText()));
JFramejf=newClass1();
JTextAreajta=newJTextArea();
publicClass1(){
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButtonjb=newJButton("转码");
jb.addActionListener(newAT());
add(jta);
add("North",jb);
setVisible(true);
StringfixString(Strings){
while(true){
intindex=s.indexOf("\\u");
if(index!=-1){
Strings1=s.substring(index,index+6);
if(s1.matches("\\\\u[0-9A-F]{4}")){
charc=(char)Integer.parseInt(s1.replace("\\u",""),16);
s=s.substring(0,index)+c+s.substring(index+6);
break;
returns;
生辰八字的介绍请参考:
具体算法:
1、设置一个下限年,比如1960,1960年以前的就不给查。并且查万年历,该年节后是庚子年。
2、将天干和地支分别装在两个数组里面。
3、以该年为基准线,设定循环,以查询年为上限年,循环计数器每加一年,天干和地支数组标号各右移一位,然后if判断是否数组到底了,到底了则从头开始。
月、时的算法类似。
publicclassTestStar{
Stringstar="*";
for(inti=0;i5;i++){
if(i==0){
System.out.print(""+star);
System.out.println();
if(i==1){
for(intz=0;z4;z++){
if(i==2){
System.out.print("");
for(intx=0;x3;x++){
if(i==3){
for(inty=0;y2;y++){
System.out.print(""+star+"");
是好使的但是我没找到画五角星有什么规律(五角星好象不是正规图形吧?)如果还有什么要求的话补充问题(如果是用*填充所有的东西不包括“”的话我可以重新再给你写一个)