求java小游戏源代码

求java小游戏源代码,第1张

import javaappletApplet;

import javaappletAudioClip;

import javaawtDimension;

import javaawtFont;

import javaawtToolkit;

import javaxsoundsampledAudioFileFormat;

import javaxsoundsampledAudioSystem;

import javaxswingJFrame;

import javaxswingJPanel;

import javaawtRectangle;

import javaxswingBorderFactory;

import javaxswingJButton;

import javaxswingJOptionPane;

import javaxswingJSlider;

import javaxswingJLabel;

import javaxswingSwingUtilities;

import javaxswingUIManager;

import javaxswingeventChangeEvent;

import javaxswingeventChangeListener;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaawteventKeyAdapter;

import javaawteventKeyEvent;

import javaioFile;

import javautilVector;

public class Frame extends JFrame implements Runnable {

JPanel contentPane;

JPanel jPanel1 = new JPanel();

JButton jButton1 = new JButton();

JSlider jSlider1 = new JSlider();

JLabel jLabel1 = new JLabel();

JButton jButton2 = new JButton();

JLabel jLabel2 = new JLabel();

int count = 1, rapidity = 80; // count 当前进行的个数, rapidity 游标的位置

int zhengque = 0, cuowu = 0;

int rush[] = { 10 ,20 ,30 }; //游戏每关的个数 可以自由添加列 { 10 ,20 ,30 ,40,50}

int rush_count = 0; //记录关数

char list[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',

'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',

'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; //随机出现的数字 可以自由添加

Vector number = new Vector();

String paiduan = "true";

AudioClip Musci_anjian, Music_shibai, Music_chenggong;

public Frame() {

try {

setDefaultCloseOperation(EXIT_ON_CLOSE);

//-----------------声音文件---------------------

Musci_anjian = AppletnewAudioClip(new File("sounds//anjianwav")

toURL());

Music_shibai = AppletnewAudioClip(new File("sounds//shibaiwav")

toURL());

Music_chenggong = AppletnewAudioClip(new File(

"sounds//chenggongwav")toURL());

//---------------------------------------

jbInit();

} catch (Exception exception) {

exceptionprintStackTrace();

}

}

/

Component initialization

@throws javalangException

/

private void jbInit() throws Exception {

contentPane = (JPanel) getContentPane();

contentPanesetLayout(null);

setSize(new Dimension(588, 530));

setTitle("Frame Title");

jPanel1setBorder(BorderFactorycreateEtchedBorder());

jPanel1setBounds(new Rectangle(4, 4, 573, 419));

jPanel1setLayout(null);

jButton1setBounds(new Rectangle(277, 442, 89, 31));

jButton1setText("开始");

jButton1addActionListener(new Frame1_jButton1_actionAdapter(this));

jSlider1setBounds(new Rectangle(83, 448, 164, 21));

jSlider1setMaximum(100);

jSlider1setMinimum(1);

jSlider1setValue(50);

jLabel1setText("速度");

jLabel1setBounds(new Rectangle(35, 451, 39, 18));

jButton2setBounds(new Rectangle(408, 442, 89, 31));

jButton2setText("结束");

jButton2addActionListener(new Frame1_jButton2_actionAdapter(this));

jLabel2setText("第一关:100个");

jLabel2setBounds(new Rectangle(414, 473, 171, 21));

contentPaneadd(jPanel1);

contentPaneadd(jButton2);

contentPaneadd(jButton1);

contentPaneadd(jSlider1);

contentPaneadd(jLabel1);

contentPaneadd(jLabel2);

thisaddKeyListener(new MyListener());

jButton1addKeyListener(new MyListener());

jSlider1addKeyListener(new MyListener());

jSlider1addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent e) {

rapidity = jSlider1getValue();

}

});

}

public void run() {

numberclear();

zhengque = 0;

cuowu = 0;

paiduan = "true";

while (count <= rush[rush_count]) {

try {

Thread t = new Thread(new Tthread());

tstart();

count += 1;

Threadsleep(1000 + (int) (Mathrandom() 2000)); // 生产下组停顿时间

// 最快1快最慢2秒

} catch (InterruptedException e) {

eprintStackTrace();

}

}

while (true) { // 等待最后一个字符消失

if (numbersize() == 0) {

break;

}

}

if (zhengque == 0) { // 为了以后相除如果全部正确或者错误就会出现错误 所以

zhengque = 1;

}

if (cuowu == 0) {

cuowu = 1;

}

if (paiduanequals("true")) { // 判断是否是自然结束

if (zhengque / cuowu >= 2) {

JOptionPaneshowMessageDialog(null, "恭喜你过关了");

rush_count += 1; // 自动加1关

if (rush_count < rushlength) {

if (rapidity > 10) { // 当速度大于10的时候在-5提加速度怕速度太快

rapidity -= 5; // 速度自动减10毫秒

jSlider1setValue(rapidity); // 选择位置

}

Thread t = new Thread(this);

tstart();

} else {

JOptionPaneshowMessageDialog(null, "牛B你通关了");

rush_count = 0;

count = 0;

}

} else {

JOptionPaneshowMessageDialog(null, "请再接再励");

rush_count = 0;

count = 0;

}

} else {

rush_count = 0;

count = 0;

}

}

public void jButton1_actionPerformed(ActionEvent e) {

Thread t = new Thread(this);

tstart();

}

public void jButton2_actionPerformed(ActionEvent e) {

count = rush[rush_count] + 1;

paiduan = "flase";

}

class Tthread implements Runnable {

public void run() {

boolean fo = true;

int Y = 0, X = 0;

JLabel show = new JLabel();

showsetFont(new javaawtFont("宋体", FontPLAIN, 33));

jPanel1add(show);

X = 10 + (int) (Mathrandom() 400);

String parameter = list[(int) (Mathrandom() listlength)] + "";

Bean bean = new Bean();

beansetParameter(parameter);

beansetShow(show);

numberadd(bean);

showsetText(parameter);

while (fo) {

// ---------------------数字下移--------------------

showsetBounds(new Rectangle(X, Y += 2, 33, 33));

try {

Threadsleep(rapidity);

} catch (InterruptedException e) {

eprintStackTrace();

}

if (Y >= 419) {

fo = false;

for (int i = numbersize() - 1; i >= 0; i--) {

Bean bn = ((Bean) numberget(i));

if (parameterequalsIgnoreCase(bngetParameter())) {

cuowu += 1;

jLabel2setText("正确:" + zhengque + "个,错误:" + cuowu

+ "个");

numberremoveElementAt(i);

Music_shibaiplay();

break;

}

}

}

}

}

}

class MyListener extends KeyAdapter {

public void keyPressed(KeyEvent e) {

String uu = egetKeyChar() + "";

for (int i = 0; i < numbersize(); i++) {

Bean bean = ((Bean) numberget(i));

if (uuequalsIgnoreCase(beangetParameter())) {

zhengque += 1;

numberremoveElementAt(i);

beangetShow()setVisible(false);

jLabel2setText("正确:" + zhengque + "个,错误:" + cuowu + "个");

Music_chenggongplay();

break;

}

}

Musci_anjianplay();

}

}

public static void main(String[] args) {

try {

UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName());

} catch (Exception exception) {

exceptionprintStackTrace();

}

Frame frame = new Frame();

Dimension screenSize = ToolkitgetDefaultToolkit()getScreenSize();

Dimension frameSize = framegetSize();

if (frameSizeheight > screenSizeheight) {

frameSizeheight = screenSizeheight;

}

if (frameSizewidth > screenSizewidth) {

frameSizewidth = screenSizewidth;

}

framesetLocation((screenSizewidth - frameSizewidth) / 2,

(screenSizeheight - frameSizeheight) / 2);

framesetVisible(true);

}

}

class Frame1_jButton2_actionAdapter implements ActionListener {

private Frame adaptee;

Frame1_jButton2_actionAdapter(Frame adaptee) {

thisadaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adapteejButton2_actionPerformed(e);

}

}

class Frame1_jButton1_actionAdapter implements ActionListener {

private Frame adaptee;

Frame1_jButton1_actionAdapter(Frame adaptee) {

thisadaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adapteejButton1_actionPerformed(e);

}

}

class Bean {

String parameter = null;

JLabel show = null;

public JLabel getShow() {

return show;

}

public void setShow(JLabel show) {

thisshow = show;

}

public String getParameter() {

return parameter;

}

public void setParameter(String parameter) {

thisparameter = parameter;

}

}

第一题

import javautilRandom;

import javautilScanner;

public class Guess{

public static void main(String[] args) {

int rightNum = new Random()nextInt(100) + 1;

Scanner scanner = new Scanner(Systemin);

int input = 0;

do{

Systemoutprint("清猜数字(1-100)!");

input = scannernextInt();

if(input > rightNum){

Systemoutprintln("猜大了!");

}

else if(input < rightNum){

Systemoutprintln("猜小了!");

}

}while(input != rightNum);

Systemoutprintln("猜对了" + rightNum);

}

}

第二题

import javautil ;

public class A{

public static void main(String args[]){

int i,j,k,temp;

int a[][]=new int[2][3];

a[0][0]=(int)(100Mathrandom());

a[0][1]=(int)(100Mathrandom());

a[0][2]=(int)(100Mathrandom());

a[1][0]=(int)(100Mathrandom());

a[1][1]=(int)(100Mathrandom());

a[1][2]=(int)(100Mathrandom());

for(j=0;j<3;j++)

Systemoutprintln("a[0]["+j+"]="+a[0][j]);

Systemoutprintln(" ");

for(j=0;j<3;j++)

Systemoutprintln("a[1]["+j+"]="+a[1][j]);

Systemoutprintln(" ");

for(i=0;i<2;i++){

for(j=0;j<2;j++){

for(k=j;k<2;k++){

if(a[i][j]>a[i][k+1]){

temp=a[i][j];

a[i][j]=a[i][k+1];

a[i][k+1]=temp;

}

}

}

}

Systemoutprintln("第一行按从小到大排列:");

for(j=0;j<3;j++){

Systemoutprintln("a[0]["+j+"]="+a[0][j]);

}

Systemoutprintln("第二行按从小到大排列:");

for(j=0;j<3;j++)

Systemoutprintln("a[1]["+j+"]=" +a[1][j]);

}

}

春春??还不快采纳嘛

给你介绍4种排序方法及源码,供参考

1冒泡排序

主要思路: 从前往后依次交换两个相邻的元素,大的交换到后面,这样每次大的数据就到后面,每一次遍历,最大的数据到达最后面,时间复杂度是O(n^2)。

public static void bubbleSort(int[] arr){

for(int i =0; i < arrlength - 1; i++){

for(int j=0; j < arrlength-1; j++){

if(arr[j] > arr[j+1]){

arr[j] = arr[j]^arr[j+1];

arr[j+1] = arr[j]^arr[j+1];

arr[j] = arr[j]^arr[j+1];

}

}

}

}

2选择排序

主要思路:每次遍历序列,从中选取最小的元素放到最前面,n次选择后,前面就都是最小元素的排列了,时间复杂度是O(n^2)。

public static void selectSort(int[] arr){

for(int i = 0; i <arrlength -1; i++){

for(int j = i+1; j < arrlength; j++){

if(arr[j] < arr[i]){

arr[j] = arr[j]^arr[i];

arr[i] = arr[j]^arr[i];

arr[j] = arr[j]^arr[i];

}

}

}

}

3插入排序

主要思路:使用了两层嵌套循环,逐个处理待排序的记录。每个记录与前面已经排好序的记录序列进行比较,并将其插入到合适的位置,时间复杂度是O(n^2)。

public static void insertionSort(int[] arr){

int j;

for(int p = 1; p < arrlength; p++){

int temp = arr[p];   //保存要插入的数据

//将无序中的数和前面有序的数据相比,将比它大的数,向后移动

for(j=p; j>0 && temp <arr[j-1]; j--){

arr[j] = arr[j-1];

}

//正确的位置设置成保存的数据

arr[j] = temp;

}

}

4希尔排序

主要思路:用步长分组,每个分组进行插入排序,再慢慢减小步长,当步长为1的时候完成一次插入排序,  希尔排序的时间复杂度是:O(nlogn)~O(n2),平均时间复杂度大致是O(n^15)

public static void shellSort(int[] arr){

int j ;

for(int gap = arrlength/2; gap > 0 ; gap/=2){

for(int i = gap; i < arrlength; i++){

int temp = arr[i];

for(j = i; j>=gap && temp<arr[j-gap]; j-=gap){

arr[j] = arr[j-gap];

}

arr[j] = temp;

}

}

}

import javautil;

class ThreadTest {

static int type = 4, num = 10; //定义资源数目和线程数目

static int[] resource = new int[type]; //系统资源总数

//static int[] copyResource = new int[type]; //副本

static Random rand = new Random();

static Bank[] bank = new Bank[num]; //线程组

Bank temp = new Bank();

public void init() {

//初始化组中每个线程,随机填充系统资源总数

for(int i = 0; i < type; i++)

resource[i] = randnextInt(10) + 80;

Systemoutprint("Resource:");

for(int i = 0; i < type; i++)

Systemoutprint(" " + resource[i]);

Systemoutprintln("");

for(int i = 0; i < banklength; i++)

bank[i] = new Bank("#" + i);

}

public ThreadTest4() {

init();

}

class Bank extends Thread {

//银行家算法避免死锁

public int[]

max = new int[type], //总共需求量

need = new int[type], //尚需资源量

allocation = new int[type]; //已分配量

private int[]

request = new int[type], //申请资源量

copyResource = new int[type]; //资源副本

private boolean isFinish = false; //线程是否完成

int[][] table = new int[banklength][type4]; //二维资源分配表

private void init() {

// 随机填充总共、尚需、已分配量

synchronized(resource) {

for(int i = 0; i < type; i++) {

max[i] = randnextInt(5) + 10;

need[i] = randnextInt(10);

allocation[i] = max[i] - need[i];

resource[i] -= allocation[i]; //从系统资源中减去已分配的

}

printer();

for(int i = 0; i < type; i++) {

if(resource[i] < 0) {

//若出现已分配量超出系统资源总数的错误则退出

Systemoutprintln("The summation of Threads' allocations is out of range!");

Systemexit(1);

}

}

}

}

public Bank(String s) {

setName(s);

init();

start();

}

public Bank() {

//none

}

public void run() {

try {

sleep(randnextInt(2000));

}

catch(InterruptedException e) {

throw new RuntimeException(e);

}

while(true) {

//程序没有完成时一直不断申请资源

if(askFor() == false) {

try {

sleep(1000);

}

catch(InterruptedException e) {

throw new RuntimeException(e);

}

}

else

tryRequest();

if(noNeed() == true)

break;

}

//休眠一段时间模拟程序运行

try {

sleep(1000);

}

catch(InterruptedException e) {

throw new RuntimeException(e);

}

Systemoutprintln(getName() + " finish!");

synchronized(resource) {

//运行结束释放占有资源

for(int i = 0; i < type; i++) {

resource[i] += allocation[i];

need[i] = allocation[i] = max[i] = 0;

}

}

}

private void printer() {

//打印当前资源信息

Systemoutprint(getName() + " Max:");

for(int i = 0; i < type; i++)

Systemoutprint(" " + max[i]);

Systemoutprint(" Allocation:");

for(int i = 0; i < type; i++)

Systemoutprint(" " + allocation[i]);

Systemoutprint(" Need:");

for(int i = 0; i < type; i++)

Systemoutprint(" " + need[i]);

Systemoutprint(" Available:");

for(int i = 0; i < type; i++)

Systemoutprint(" " + resource[i]);

Systemoutprintln("");

}

private boolean askFor() {

//随机产生申请资源量并检测是否超标

boolean canAsk = false;

for(int i = 0; i < type; i++) {

request[i] = randnextInt(20);

//防止申请量超过所需量

if(request[i] > need[i])

request[i] = need[i];

}

for(int i = 0; i < type; i++) //防止随机申请资源全为0

if(request[i] > 0)

canAsk = true;

synchronized(resource) {

//锁住可供资源检查是否超标

for(int i = 0; i < type; i++) {

if(request[i] > resource[i])

//如果申请资源超过可供资源则等待一段时间后重新申请

return false;

}

}

return canAsk;

}

private void tryRequest() {

//创建副本尝试分配请求

synchronized(resource) {

for(int i = 0; i < type; i++)

//依然要防止请求量超出范围

if(request[i] > resource[i])

return;

for(int i = 0; i < type; i++) {

//复制资源量并减去需求量到一个副本上

copyResource[i] = resource[i];

copyResource[i] -= request[i];

}

Systemoutprint(getName() + " ask for:");

for(int i = 0; i < type; i++)

Systemoutprint(" " + request[i]);

Systemoutprintln("");

if(checkSafe() == true) {

//如果检查安全则将副本值赋给资源量并修改占有量和需求量

for(int i = 0; i < type; i++) {

resource[i] = copyResource[i];

allocation[i] += request[i];

need[i] -= request[i];

}

Systemoutprintln(getName() + " request succeed!");

}

else

Systemoutprintln(getName() + " request fail!");

}

}

private boolean checkSafe() {

//银行家算法检查安全性

synchronized(bank) {

//将线程资源信息放入二维资源分配表检查安全性,0~type可用资源/type~type2所需资源/type2~type3占有资源/type3~-1可用+占用资源

for(int i = 0; i < banklength; i++) {

for(int j = type; j < type2; j++) {

table[i][j] = bank[i]need[j%type];

}

for(int j = type2; j < type3; j++) {

table[i][j] = bank[i]allocation[j%type];

}

}

//冒泡排序按需求资源从小到大排

for(int i = 0; i < banklength; i++) {

for(int j = i; j < banklength-1; j++) {

sort(j, 4);

}

}

//进行此时刻的安全性检查

for(int i = 0; i < type; i++) {

table[0][i] = copyResource[i];

table[0][i+type3] = table[0][i] + table[0][i+type2];

if(table[0][i+type3] < table[1][i+type])

return false;

}

for(int j = 1; j < banklength-1; j++) {

for(int k = 0; k < type; k++) {

table[j][k] = table[j-1][k+type3];

table[j][k+type3] = table[j][k] + table[j][k+type2];

if(table[j][k+type3] < table[j+1][k+type])

return false;

}

}

}

return true;

}

private void sort(int j, int k) {

//递归冒泡排序

int tempNum;

if(table[j][k] > table[j+1][k]) {

for(int i = type; i < type2; i++) {

tempNum = table[j][i];

table[j][i] = table[j+1][i];

table[j+1][i] = tempNum;

}

/temp = bank[j];

bank[j] = bank[j+1];

bank[j+1] = temp;/

}

else if(table[j][k] == table[j+1][k] && k < type2) //此资源量相同时递归下一个资源量排序并且防止超出范围

sort(j, k+1);

}

private boolean noNeed() {

//是否还需要资源

boolean finish = true;

for(int i = 0; i < type; i++) {

if(need[i] != 0) {

finish = false;

break;

}

}

return finish;

}

}

public static void main(String[] args) {

ThreadTest t = new ThreadTest();

//后台线程,设定程序运行多长时间后自动结束

new Timeout(30000, "---Stop!!!---");

}

}

package Project;

import javautilScanner;

public class Complex {

int x;

int y;

Complex(){

x=0;

y=0;

}

Complex(int i,int j){

x=i;

y=j;

}

//显示

public void showComp(){

if(y>0){

Systemoutprint(x+"+"+y+"i"+'\t');

}

if(y<0){

Systemoutprint(x+""+y+"i"+'\t');

}

if(y==0){

Systemoutprint(x+""+'\t');

}

}

//求和

public static Complex addComp(Complex C1,Complex C2){

Complex newComplex=new Complex();

newComplexx=C1x+C2x;

newComplexy=C1y+C2y;

Systemoutprintln();

Systemoutprint("和为:");

return newComplex;

}

//求差

public static Complex subComp(Complex C1,Complex C2){

Complex newComplex=new Complex();

newComplexx=C1x-C2x;

newComplexy=C1y-C2y;

Systemoutprintln();

Systemoutprint("差为:");

return newComplex;

}

//求积

public static Complex multiComp(Complex C1,Complex C2){

Complex newComplex=new Complex();

newComplexx=C1xC2x-C1yC2y;

newComplexy=C1xC2y+C1yC2x;

Systemoutprintln();

Systemoutprint("积为:");

return newComplex;

}

//是否相等

public static boolean equalComp(Complex C1,Complex C2){

Systemoutprintln();

Systemoutprint("是否相等:");

return ((C1x==C2x)&&(C1y==C2y));

}

public static void main(String[] args){

Scanner data=new Scanner(Systemin);

int x=datanextInt();

int y=datanextInt();

int m=datanextInt();

int n=datanextInt();

Complex Comp1=new Complex(x,y);

Complex Comp2=new Complex(m,n);

dataclose();

//显示

Comp1showComp();

Comp2showComp();

ComplexaddComp(Comp1,Comp2)showComp(); //显示 和

ComplexsubComp(Comp1, Comp2)showComp(); //显示 差

ComplexmultiComp(Comp1, Comp2)showComp(); //显示 积

Systemoutprintln( ComplexequalComp(Comp1, Comp2));

}

}

md5加密:

package comncspkiutil;

import javasecurityMessageDigest;

import javasecurityNoSuchAlgorithmException;

public class MD5Test {

private static MessageDigest digest = null;

public synchronized static final String hash(String data) {

if (digest == null) {

try {

digest = MessageDigestgetInstance("MD5");

} catch (NoSuchAlgorithmException nsae) {

Systemerrprintln(

"Failed to load the MD5 MessageDigest "

+ "Jive will be unable to function normally");

nsaeprintStackTrace();

}

}

// Now, compute hash

digestupdate(datagetBytes());

return encodeHex(digestdigest());

}

public static final String encodeHex(byte[] bytes) {

StringBuffer buf = new StringBuffer(byteslength 2);

int i;

for (i = 0; i < byteslength; i++) {

if (((int) bytes[i] & 0xff) < 0x10) {

bufappend("0");

}

bufappend(LongtoString((int) bytes[i] & 0xff, 16));

}

return buftoString();

}

public static String test(){

return null;

}

/

@param args

/

public static void main(String[] args) {

// TODO Auto-generated method stub

Systemoutprintln(MD5Testhash("123456"));

}

}

3des加密:

package comncspkiutil;

import javasecurityKey;

import javasecuritySecureRandom;

import javaxcryptoCipher;

import javaxcryptoKeyGenerator;

import sunmiscBASE64Decoder;

import sunmiscBASE64Encoder;

public class DesEncrypt {

/

使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储

方法: void getKey(String strKey)从strKey的字条生成一个Key

String getEncString(String strMing)对strMing进行加密,返回String密文 String

getDesString(String strMi)对strMin进行解密,返回String明文

byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]

byteD)byte[]型的解密

/

Key key;

/

根据参数生成KEY

@param strKey

/

public void getKey(String strKey) {

try {

KeyGenerator _generator = KeyGeneratorgetInstance("DES");

_generatorinit(new SecureRandom(strKeygetBytes()));

thiskey = _generatorgenerateKey();

_generator = null;

} catch (Exception e) {

eprintStackTrace();

}

}

/

加密String明文输入,String密文输出

@param strMing

@return

/

public String getEncString(String strMing) {

byte[] byteMi = null;

byte[] byteMing = null;

String strMi = "";

BASE64Encoder base64en = new BASE64Encoder();

try {

byteMing = strMinggetBytes("UTF8");

byteMi = thisgetEncCode(byteMing);

strMi = base64enencode(byteMi);

} catch (Exception e) {

eprintStackTrace();

} finally {

base64en = null;

byteMing = null;

byteMi = null;

}

return strMi;

}

/

解密 以String密文输入,String明文输出

@param strMi

@return

/

public String getDesString(String strMi) {

BASE64Decoder base64De = new BASE64Decoder();

byte[] byteMing = null;

byte[] byteMi = null;

String strMing = "";

try {

byteMi = base64DedecodeBuffer(strMi);

byteMing = thisgetDesCode(byteMi);

strMing = new String(byteMing, "UTF8");

} catch (Exception e) {

eprintStackTrace();

} finally {

base64De = null;

byteMing = null;

byteMi = null;

}

return strMing;

}

/

加密以byte[]明文输入,byte[]密文输出

@param byteS

@return

/

private byte[] getEncCode(byte[] byteS) {

byte[] byteFina = null;

Cipher cipher;

try {

cipher = CiphergetInstance("DES");

cipherinit(CipherENCRYPT_MODE, key);

byteFina = cipherdoFinal(byteS);

} catch (Exception e) {

eprintStackTrace();

} finally {

cipher = null;

}

return byteFina;

}

/

解密以byte[]密文输入,以byte[]明文输出

@param byteD

@return

/

private byte[] getDesCode(byte[] byteD) {

Cipher cipher;

byte[] byteFina = null;

try {

cipher = CiphergetInstance("DES");

cipherinit(CipherDECRYPT_MODE, key);

byteFina = cipherdoFinal(byteD);

} catch (Exception e) {

eprintStackTrace();

} finally {

cipher = null;

}

return byteFina;

}

public static void main(String[] args) {

Systemoutprintln("des demo");

DesEncrypt des = new DesEncrypt();// 实例化一个对像

desgetKey("MYKEY");// 生成密匙

Systemoutprintln("key=MYKEY");

String strEnc = desgetEncString("111111");// 加密字符串,返回String的密文

Systemoutprintln("密文=" + strEnc);

String strDes = desgetDesString(strEnc);// 把String 类型的密文解密

Systemoutprintln("明文=" + strDes);

}

}

上面 wuzhikun12同学写的不错,但我想还不能运行,并且还不太完善。我给个能运行的:(注意:文件名为:Testjava)

//要实现对象间的比较,就必须实现Comparable接口,它里面有个compareTo方法

//Comparable最好使用泛型,这样,无论是速度还是代码量都会减少

@SuppressWarnings("unchecked")

class Student implements Comparable<Student>{

private String studentNo; //学号

private String studentName; //姓名

private double englishScore; //英语成绩

private double computerScore; //计算机成绩

private double mathScore; //数学成绩

private double totalScore; //总成绩

//空构造函数

public Student() {}

//构造函数

public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {

thisstudentNo = studentNo;

thisstudentName = studentName;

thisenglishScore = englishSocre;

thiscomputerScore = computerScore;

thismathScore = mathScore;

}

//计算总成绩

public double sum() {

thistotalScore = englishScore+computerScore+mathScore;

return totalScore;

}

//计算评测成绩

public double testScore() {

return sum()/3;

}

//实现compareTO方法

@Override

public int compareTo(Student student) {

double studentTotal = studentgetTotalScore();

return totalScore==studentTotal0:(totalScore>studentTotal1:-1);

}

//重写toString方法

public String toString(){

return "学号:"+thisgetStudentNo()+" 姓名:"+thisgetStudentName()+" 英语成绩:"+thisgetEnglishScore()+" 数学成绩:"+thisgetMathScore()+" 计算机成绩:"+thisgetComputerScore()+" 总成绩:"+thisgetTotalScore();

}

//重写equals方法

public boolean equals(Object obj) {

if(obj == null){

return false;

}

if(!(obj instanceof Student)){

return false;

}

Student student = (Student)obj;

if(thisstudentNoequals(studentgetStudentName())) { //照现实来说,比较是不是同一个学生,应该只是看他的学号是不是相同

return true;

} else {

return false;

}

}

/以下为get和set方法,我个人认为,totalScore的set的方法没必要要,因为它是由其它成绩计算出来的

在set方法中,没设置一次值,调用一次sum方法,即重新计算总成绩

/

public String getStudentNo() {

return studentNo;

}

public void setStudentNo(String studentNo) {

thisstudentNo = studentNo;

sum();

}

public String getStudentName() {

return studentName;

}

public void setStudentName(String studentName) {

thisstudentName = studentName;

sum();

}

public double getEnglishScore() {

return englishScore;

}

public void setEnglishScore(double englishScore) {

thisenglishScore = englishScore;

sum();

}

public double getComputerScore() {

return computerScore;

}

public void setComputerScore(double computerScore) {

thiscomputerScore = computerScore;

sum();

}

public double getMathScore() {

return mathScore;

}

public void setMathScore(double mathScore) {

thismathScore = mathScore;

sum();

}

public double getTotalScore() {

return totalScore;

}

}

//Student子类学习委员类的实现

class StudentXW extends Student {

//重写父类Student的testScore()方法

@Override

public double testScore() {

return sum()/3+3;

}

public StudentXW() {}

//StudentXW的构造函数

public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {

super(studentNo,studentName,englishSocre,computerScore,mathScore);

}

}

//Student子类班长类的实现

class StudentBZ extends Student {

//重写父类Student的testScore()方法

@Override

public double testScore() {

return sum()/3+5;

}

public StudentBZ() {}

//StudentXW的构造函数

public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {

super(studentNo,studentName,englishSocre,computerScore,mathScore);

}

}

//测试类

public class Test {

public static void main(String[] args) {

//生成若干个student类、StudentXW类、StudentBZ类

Student student1 = new Student("s001","张三",705,50,885);

Student student2 = new Student("s002","李四",88,65,885);

Student student3 = new Student("s003","王五",67,77,90);

StudentXW student4 = new StudentXW("s004","李六",99,88,995);

StudentBZ student5 = new StudentBZ("s005","朱漆",56,656,435);

Student[] students = {student1,student2,student3,student4,student5};

for(int i = 0 ; i<studentslength; i++){

double avgScore = students[i]testScore();

Systemoutprintln(students[i]getStudentName()+"学生的评测成绩为:"+ avgScore+"分");

}

}

}

运行结果为:

张三学生的评测成绩为:6966666666666667分

李四学生的评测成绩为:805分

王五学生的评测成绩为:780分

李六学生的评测成绩为:985分

朱漆学生的评测成绩为:6003333333333333分

以上就是关于求java小游戏源代码全部的内容,包括:求java小游戏源代码、在线等一个java程序源代码 急用!!!、Java几种简单的排序源代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10050730.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存