java优秀源代码,优秀java开源项目

java 小程序源代码

求素数,比较经典的,下面是代码及注释

目前创新互联已为上千多家的企业提供了网站建设、域名、雅安服务器托管网站托管、企业网站设计、石柱土家族网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

================================================

public class Sushu {

/**

* 判断一个数是不是素数

* @param a 被判断的数

* @return 是素数返回真

*/

public boolean isSuhu(int a){

boolean isSushu=true;

//根据素数的性质判断一个数是否为素数

for(int i=2;ia;i++){

if(a%i==0){

isSushu=false;

break;

}

}

return isSushu;

}

/**

* 判断连续若干个数中那些是素数

* @param start 起始数

* @param end 终止数

*/

public void selectSushu(int start,int end){

//判断一串数中那些为素数,并将结果打印出来

for(int i=start;i=end;i++){

if(isSuhu(i)){

System.out.println(i);

}

}

}

public static void main(String []args){

//定义起始位置和终止位置

int start=1;

int end=100;

//声明变量

Sushu s=new Sushu();

//调用方法

s.selectSushu(start, end);

}

}

求Java 日历的小程序的源代码

也不知道你具体需求是什么,以前改过一个日历程序,一共四个java类,放在同一个包里。经测试可以运行。

//Start.java

import java.awt.*;

import javax.swing.*;

class Start{

public static void main(String [] args){

DateFrame frame=new DateFrame();

frame.setLocationRelativeTo(frame);

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

//DateInfo.java

import java.util.*;

public class DateInfo{

private int mYear, mMonth;

private int mDayOfMonth, mFristWeek;

public DateInfo(int year, int month) throws DateException{

mYear = year;

if (month 0 || month 12){

throw (new DateException());

}

mMonth = month;

mDayOfMonth = getDayOfMonth(mYear, mMonth);

mFristWeek = getFristWeek(mYear, mMonth);

}

private int getDayOfMonth(int year, int month){

int[][] ary = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},

{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};

return (ary[isLeapYear(year)][month]);

}

private int isLeapYear(int year){

if (year % 4 == 0 year % 100 != 0 ||year % 400 == 0){

return (1);

}

else{

return (0);

}

}

private int getFristWeek(int year, int month){

java.util.Calendar cal = Calendar.getInstance();

cal.set(year, month - 1, 1);

return (cal.get(Calendar.DAY_OF_WEEK) - 1);

}

public String toString(){

String str;

str = "\t\t" + mYear + "年" + mMonth + "月\n";

str += "日\t一\t二\t三\t四\t五\t六\n";

int i;

for (i = 1; i = mFristWeek; i++){

str += " \t";

}

for (int j = 1; j = mDayOfMonth; j++, i++){

str +=j+"\t" ;

if (i % 7 == 0){

str += "\n";

}

}

return (str);

}

}

//DateFrame.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.Calendar;

class DateFrame extends JFrame implements Runnable{

Calendar date=Calendar.getInstance();

String[] str={"1","2","3","4","5","6","7","8","9","10","11","12"};

JLabel lblYear=new JLabel("年 ");

JLabel lblMonth=new JLabel("月 ");

JLabel lblDate=new JLabel("现在的时间是:");

JLabel lblShowDate=new JLabel();

// javax.swing.JTextField trxt =new JTextField(10);

// trxt.setHorizontalAlignment(JTextField.RIGHT); //设置文本从右边输入

JComboBox cboMonth=new JComboBox(str);

JComboBox cboYear=new JComboBox();

JTextArea txaShow=new JTextArea();

JPanel pnlNorth=new JPanel();

JPanel pnlSOUTH=new JPanel();

JButton btnShow=new JButton("显示");

JButton btnClose=new JButton("关闭");

JScrollPane jsp=new JScrollPane(txaShow);

Container c=this.getContentPane();

public DateFrame(){

Thread thread=new Thread(this);

thread.start();

this.setTitle("玩玩日历拉!!!");

this.setSize(300,260);

for (int i = 1990; i=2025; i++) {

cboYear.addItem(""+i);

}

cboYear.setSelectedItem(""+(date.get(Calendar.YEAR)));

cboMonth.setSelectedItem(""+(date.get(Calendar.MONTH)+1));

pnlNorth.add(cboYear);

txaShow.setTabSize(4); //设置tab键的距离

txaShow.setForeground(Color.GREEN);

pnlNorth.add(lblYear);

pnlNorth.add(cboMonth);

pnlNorth.add(lblMonth);

pnlNorth.add(lblDate);

pnlNorth.add(lblShowDate);

c.add(pnlNorth,BorderLayout.NORTH);

c.add(jsp);

pnlSOUTH.add(btnShow);

pnlSOUTH.add(btnClose);

c.add(pnlSOUTH,BorderLayout.SOUTH);

btnShow.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

int year=Integer.parseInt((String)cboYear.getSelectedItem());

int month=Integer.parseInt((String)cboMonth.getSelectedItem());

try {

DateInfo date=new DateInfo(year,month);

txaShow.setText(""+date);

}

catch (DateException ex) {

ex.printStackTrace();

}

}

});

btnClose.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

System.exit(0);

}

});

}

public void run(){

try {

while(true){

Thread.sleep(1000);

int hour=date.get(Calendar.HOUR);

int minute=date.get(Calendar.MINUTE);

int second=date.get(Calendar.SECOND);

String str=hour+":"+minute+":"+second;

lblShowDate.setText(str);

//this.repaint();

}

}

catch (Exception ex) {

ex.printStackTrace();

}

}

}

//DateException.java

public class DateException extends Exception{

public DateException(){

super("日期数据不合法.");

}

}

求:用JAVA语言编写的银行家算法的源代码

import java.util.*;

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] = rand.nextInt(10) + 80;

System.out.print("Resource:");

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

System.out.print(" " + resource[i]);

System.out.println("");

for(int i = 0; i bank.length; 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[bank.length][type*4]; //二维资源分配表

private void init() {

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

synchronized(resource) {

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

max[i] = rand.nextInt(5) + 10;

need[i] = rand.nextInt(10);

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

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

}

printer();

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

if(resource[i] 0) {

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

System.out.println("The summation of Threads' allocations is out of range!");

System.exit(1);

}

}

}

}

public Bank(String s) {

setName(s);

init();

start();

}

public Bank() {

//none

}

public void run() {

try {

sleep(rand.nextInt(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);

}

System.out.println(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() {

//打印当前资源信息

System.out.print(getName() + " Max:");

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

System.out.print(" " + max[i]);

System.out.print(" Allocation:");

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

System.out.print(" " + allocation[i]);

System.out.print(" Need:");

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

System.out.print(" " + need[i]);

System.out.print(" Available:");

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

System.out.print(" " + resource[i]);

System.out.println("");

}

private boolean askFor() {

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

boolean canAsk = false;

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

request[i] = rand.nextInt(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];

}

System.out.print(getName() + " ask for:");

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

System.out.print(" " + request[i]);

System.out.println("");

if(checkSafe() == true) {

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

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

resource[i] = copyResource[i];

allocation[i] += request[i];

need[i] -= request[i];

}

System.out.println(getName() + " request succeed!");

}

else

System.out.println(getName() + " request fail!");

}

}

private boolean checkSafe() {

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

synchronized(bank) {

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

for(int i = 0; i bank.length; i++) {

for(int j = type; j type*2; j++) {

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

}

for(int j = type*2; j type*3; j++) {

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

}

}

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

for(int i = 0; i bank.length; i++) {

for(int j = i; j bank.length-1; j++) {

sort(j, 4);

}

}

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

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

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

table[0][i+type*3] = table[0][i] + table[0][i+type*2];

if(table[0][i+type*3] table[1][i+type])

return false;

}

for(int j = 1; j bank.length-1; j++) {

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

table[j][k] = table[j-1][k+type*3];

table[j][k+type*3] = table[j][k] + table[j][k+type*2];

if(table[j][k+type*3] 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 type*2; 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 type*2) //此资源量相同时递归下一个资源量排序并且防止超出范围

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!!!---");

}

}

JAVA代码

连连看java源代码

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class lianliankan implements ActionListener

{

JFrame mainFrame; //主面板

Container thisContainer;

JPanel centerPanel,southPanel,northPanel; //子面板

JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组

JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮

JLabel fractionLable=new JLabel("0"); //分数标签

JButton firstButton,secondButton; //分别记录两次被选中的按钮

int grid[][] = new int[8][7];//储存游戏按钮位置

static boolean pressInformation=false; //判断是否有按钮被选中

int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标

int i,j,k,n;//消除方法控制

public void init(){

mainFrame=new JFrame("JKJ连连看");

thisContainer = mainFrame.getContentPane();

thisContainer.setLayout(new BorderLayout());

centerPanel=new JPanel();

southPanel=new JPanel();

northPanel=new JPanel();

thisContainer.add(centerPanel,"Center");

thisContainer.add(southPanel,"South");

thisContainer.add(northPanel,"North");

centerPanel.setLayout(new GridLayout(6,5));

for(int cols = 0;cols 6;cols++){

for(int rows = 0;rows 5;rows++ ){

diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));

diamondsButton[cols][rows].addActionListener(this);

centerPanel.add(diamondsButton[cols][rows]);

}

}

exitButton=new JButton("退出");

exitButton.addActionListener(this);

resetButton=new JButton("重列");

resetButton.addActionListener(this);

newlyButton=new JButton("再来一局");

newlyButton.addActionListener(this);

southPanel.add(exitButton);

southPanel.add(resetButton);

southPanel.add(newlyButton);

fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));

northPanel.add(fractionLable);

mainFrame.setBounds(280,100,500,450);

mainFrame.setVisible(true);

}

public void randomBuild() {

int randoms,cols,rows;

for(int twins=1;twins=15;twins++) {

randoms=(int)(Math.random()*25+1);

for(int alike=1;alike=2;alike++) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

while(grid[cols][rows]!=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

}

this.grid[cols][rows]=randoms;

}

}

}

public void fraction(){

fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));

}

public void reload() {

int save[] = new int[30];

int n=0,cols,rows;

int grid[][]= new int[8][7];

for(int i=0;i=6;i++) {

for(int j=0;j=5;j++) {

if(this.grid[i][j]!=0) {

save[n]=this.grid[i][j];

n++;

}

}

}

n=n-1;

this.grid=grid;

while(n=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

while(grid[cols][rows]!=0) {

cols=(int)(Math.random()*6+1);

rows=(int)(Math.random()*5+1);

}

this.grid[cols][rows]=save[n];

n--;

}

mainFrame.setVisible(false);

pressInformation=false; //这里一定要将按钮点击信息归为初始

init();

for(int i = 0;i 6;i++){

for(int j = 0;j 5;j++ ){

if(grid[i+1][j+1]==0)

diamondsButton[i][j].setVisible(false);

}

}

}

public void estimateEven(int placeX,int placeY,JButton bz) {

if(pressInformation==false) {

x=placeX;

y=placeY;

secondMsg=grid[x][y];

secondButton=bz;

pressInformation=true;

}

else {

x0=x;

y0=y;

fristMsg=secondMsg;

firstButton=secondButton;

x=placeX;

y=placeY;

secondMsg=grid[x][y];

secondButton=bz;

if(fristMsg==secondMsg secondButton!=firstButton){

xiao();

}

}

}

public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释

if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))){ //判断是否相邻

remove();

}

else{

for (j=0;j7;j++ ) {

if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空

if (yj) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边

for (i=y-1;i=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮

if (grid[x][i]!=0) {

k=0;

break;

}

else //K=1说明通过了第一次验证

}

if (k==1) {

linePassOne();

}

}

if (yj){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边

for (i=y+1;i=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮

if (grid[x][i]!=0){

k=0;

break;

}

else

}

if (k==1){

linePassOne();

}

}

if (y==j ) {

linePassOne();

}

}

if (k==2) {

if (x0==x) {

remove();

}

if (x0x) {

for (n=x0;n=x-1;n++ ) {

if (grid[n][j]!=0) {

k=0;

break;

}

if(grid[n][j]==0 n==x-1) {

remove();

}

}

}

if (x0x) {

for (n=x0;n=x+1 ;n-- ) {

if (grid[n][j]!=0) {

k=0;

break;

}

if(grid[n][j]==0 n==x+1) {

remove();

}

}

}

}

}

for (i=0;i8;i++ ) { //列

if (grid[i][y0]==0) {

if (xi) {

for (j=x-1;j=i ;j-- ) {

if (grid[j][y]!=0) {

k=0;

break;

}

else

}

if (k==1) {

rowPassOne();

}

}

if (xi) {

for (j=x+1;j=i;j++ ) {

if (grid[j][y]!=0) {

k=0;

break;

}

else

}

if (k==1) {

rowPassOne();

}

}

if (x==i) {

rowPassOne();

}

}

if (k==2){

if (y0==y) {

remove();

}

if (y0y) {

for (n=y0;n=y-1 ;n++ ) {

if (grid[i][n]!=0) {

k=0;

break;

}

if(grid[i][n]==0 n==y-1) {

remove();

}

}

}

if (y0y) {

for (n=y0;n=y+1 ;n--) {

if (grid[i][n]!=0) {

k=0;

break;

}

if(grid[i][n]==0 n==y+1) {

remove();

}

}

}

}

}

}

}

public void linePassOne(){

if (y0j){ //第一按钮同行空按钮在左边

for (i=y0-1;i=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮

if (grid[x0][i]!=0) {

k=0;

break;

}

else //K=2说明通过了第二次验证

}

}

if (y0j){ //第一按钮同行空按钮在与第二按钮之间

for (i=y0+1;i=j ;i++){

if (grid[x0][i]!=0) {

k=0;

break;

}

else

}

}

}

public void rowPassOne(){

if (x0i) {

for (j=x0-1;j=i ;j-- ) {

if (grid[j][y0]!=0) {

k=0;

break;

}

else

}

}

if (x0i) {

for (j=x0+1;j=i ;j++ ) {

if (grid[j][y0]!=0) {

k=0;

break;

}

else

}

}

}

public void remove(){

firstButton.setVisible(false);

secondButton.setVisible(false);

fraction();

pressInformation=false;

k=0;

grid[x0][y0]=0;

grid[x][y]=0;

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==newlyButton){

int grid[][] = new int[8][7];

this.grid = grid;

randomBuild();

mainFrame.setVisible(false);

pressInformation=false;

init();

}

if(e.getSource()==exitButton)

System.exit(0);

if(e.getSource()==resetButton)

reload();

for(int cols = 0;cols 6;cols++){

for(int rows = 0;rows 5;rows++ ){

if(e.getSource()==diamondsButton[cols][rows])

estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);

}

}

}

public static void main(String[] args) {

lianliankan llk = new lianliankan();

llk.randomBuild();

llk.init();

}

}

//old 998 lines

//new 318 lines

文章名称:java优秀源代码,优秀java开源项目
URL分享:https://www.cdcxhl.com/article18/dsepigp.html

成都网站建设公司_创新互联,为您提供网站制作网站收录关键词优化网站内链用户体验网站排名

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都seo排名网站优化