連連看java源代碼

站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到城北網(wǎng)站設(shè)計(jì)與城北網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:成都網(wǎng)站制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋城北地區(qū)。
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];//游戲按鈕數(shù)組
JButton exitButton,resetButton,newlyButton; //退出,重列,重新開(kāi)始侍禪按鈕
JLabel fractionLable=new JLabel("老首塵0"); //分?jǐn)?shù)標(biāo)簽
JButton firstButton,secondButton; //分別記錄兩次被選中的按鈕
int grid[][] = new int[8][7];//儲(chǔ)存游戲按鈕位置
static boolean pressInformation=false; //判斷是否有按鈕被選中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位置坐標(biāo)
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("再來(lái)一局");
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; //這里一定要將按鈕點(diǎn)擊信息歸為初始
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() { //相同的情況下能不能消去。仔細(xì)分析,不一條條注釋
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){ //判斷第一個(gè)按鈕同行哪個(gè)按鈕為空
if (yj) { //如果第二個(gè)按鈕的Y坐標(biāo)大于空按鈕的Y坐標(biāo)說(shuō)明第一按鈕在第二按鈕左邊
for (i=y-1;i=j;i-- ){ //判斷第二按鈕左側(cè)直到第一按鈕中間有沒(méi)有按鈕
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1說(shuō)明通過(guò)了第一次驗(yàn)證
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二個(gè)按鈕的Y坐標(biāo)小于空按鈕的Y坐標(biāo)說(shuō)明第一按鈕在第二按鈕右邊
for (i=y+1;i=j ;i++ ){ //判斷第二按鈕左側(cè)直到第一按鈕中間有沒(méi)有按鈕
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
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 { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
for (j=x+1;j=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
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-- ){ //判斷第一按鈕同左側(cè)空按鈕之間有沒(méi)按鈕
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2說(shuō)明通過(guò)了第二次驗(yàn)證
}
}
if (y0j){ //第一按鈕同行空按鈕在與第二按鈕之間
for (i=y0+1;i=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0i) {
for (j=x0+1;j=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
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的3D坦克游戲源代碼
JAVA猜數(shù)字小游戲源代碼
/*1、編寫(xiě)一個(gè)猜數(shù)字的游戲,由電腦隨機(jī)產(chǎn)生一個(gè)100以?xún)?nèi)的整數(shù),讓用戶(hù)去猜,如果用戶(hù)猜的比電腦大,則輸出“大了,再小點(diǎn)!”,反之則輸出“小了,再大點(diǎn)!”,用戶(hù)總共只能猜十次,并根據(jù)用戶(hù)正確猜出答案所用的次數(shù)輸出相應(yīng)的信息,如:只用一次就猜對(duì),輸出“你是個(gè)天才!”,八次才猜對(duì),輸出“笨死了!”,如果十次還沒(méi)有猜對(duì),則游戲結(jié)束!*/
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("請(qǐng)輸入一個(gè)100以?xún)?nèi)的整數(shù):");
for (int i=0;i=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shunum)
System.out.println("輸入的數(shù)大了,輸小點(diǎn)的!");
else if (shunum)
System.out.println("輸入的數(shù)小了,輸大點(diǎn)的!");
else {
System.out.println("恭喜你,猜對(duì)了!");
if (i=2)
System.out.println("你真是個(gè)天才!");
else if (i=6)
System.out.println("還將就,你過(guò)關(guān)了!");
else if (i=8)
System.out.println("但是你還……真笨!");
else
System.out.println("你和豬沒(méi)有兩樣了!");
break;}
}
}
}
簡(jiǎn)單說(shuō)就是規(guī)范,代碼整潔,遵循項(xiàng)目開(kāi)發(fā)的開(kāi)發(fā)規(guī)范伏鉛。
另外就是一定要多些注釋?zhuān)@樣便于后期維護(hù),或者他人更好熟悉代碼
別的,寫(xiě)的多了就明白了,就那樣,感覺(jué)高質(zhì)局槐量更缺臘好多還是對(duì)sql的效率優(yōu)化這方面
第一空,因?yàn)楸救酥R(shí)不到家,不能根據(jù)你的驅(qū)動(dòng)看出薯輪用的哪個(gè)數(shù)據(jù)庫(kù),所以無(wú)法寫(xiě)。
2.createStatement()
3.select * from 表名
4.executeQuery(sql)
5.long xh = rs.getLong(2)//昌絕數(shù)據(jù)類(lèi)型(要看你第二字段的類(lèi)型) 變量名=rs.get數(shù)據(jù)類(lèi)型(2),這個(gè)不是很數(shù)迅信有把握
6.System.out.println(xh)
說(shuō)到代碼優(yōu)化,每個(gè)人或多或少都掌握一到兩種方法,但是這樣的方法對(duì)提升鉛正代碼運(yùn)行效率效果不大,最重要是對(duì)代碼的重視和了解,這樣才能提升代碼的運(yùn)行效率。在進(jìn)行代碼優(yōu)化的過(guò)程中,方法是非常重要的,多掌握幾種方法,根據(jù)代碼的不同情況選擇適合的方法進(jìn)行優(yōu)化。下面電腦培訓(xùn)為大家介紹Java代碼優(yōu)化的幾種方法。
1、使用指定類(lèi)、方法的final修飾符
具有final修飾符的類(lèi)不可派生。在Java核心API中,有許多槐吵悔最終應(yīng)用程序的例子,例如java.lang.String,整個(gè)類(lèi)都是final。為類(lèi)指定final修飾符允許繼承類(lèi),并且為方法指定final修飾符允許覆蓋該方法。如果將類(lèi)指定為final,IT培訓(xùn)認(rèn)為該類(lèi)的所有方法都是final。Java編譯器將尋找內(nèi)聯(lián)所有最終方法的機(jī)會(huì)。內(nèi)聯(lián)對(duì)于提高Java操作的效率非常重要。這可以將性能平均提高50%。
2、重用對(duì)象
String對(duì)象的使用是非常重要的,StringBuilder/StringBuffer并不是字符串連接。由于Java虛擬機(jī)需要時(shí)間來(lái)生成對(duì)象,所以將來(lái)垃圾收集和處理這些對(duì)象可能需要一些時(shí)間。因此,生成太多對(duì)象將對(duì)程序的性能產(chǎn)生很大影響。
3、使用局部變量
調(diào)用方法時(shí)傳遞的參數(shù)以及在調(diào)用中創(chuàng)建的臨時(shí)變量都保存在堆棧中,速度更快。其他變量(如靜態(tài)變量和實(shí)例變量)在堆中創(chuàng)建并且速度較慢。此外,北京北大青鳥(niǎo)發(fā)現(xiàn)在堆棧中創(chuàng)建的變量,當(dāng)方法完成運(yùn)行時(shí),內(nèi)容消失,不需要進(jìn)行額外的垃圾收集。
4、及時(shí)關(guān)閉流
在Java編程過(guò)程中,在執(zhí)行數(shù)據(jù)庫(kù)連接和I/O流操作時(shí)要小心。使用后,北大青鳥(niǎo)昌平校區(qū)官網(wǎng)建議應(yīng)及時(shí)關(guān)閉以釋放資源。因?yàn)檫@些大型物體的操作會(huì)導(dǎo)致系統(tǒng)的大量開(kāi)銷(xiāo),碰消稍微粗心會(huì)導(dǎo)致嚴(yán)重的后果。
文章標(biāo)題:java高級(jí)代碼 java爬蟲(chóng)代碼示例
網(wǎng)頁(yè)路徑:http://chinadenli.net/article30/ddpedso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)站導(dǎo)航、軟件開(kāi)發(fā)、網(wǎng)站收錄、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容