連連看java源代碼
創(chuàng)新互聯(lián)建站不只是一家網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司;我們對(duì)營(yíng)銷(xiāo)、技術(shù)、服務(wù)都有自己獨(dú)特見(jiàn)解,公司采取“創(chuàng)意+綜合+營(yíng)銷(xiāo)”一體化的方式為您提供更專業(yè)的服務(wù)!我們經(jīng)歷的每一步也許不一定是最完美的,但每一步都有值得深思的意義。我們珍視每一份信任,關(guān)注我們的成都做網(wǎng)站、網(wǎng)站制作質(zhì)量和服務(wù)品質(zhì),在得到用戶滿意的同時(shí),也能得到同行業(yè)的專業(yè)認(rèn)可,能夠?yàn)樾袠I(yè)創(chuàng)新發(fā)展助力。未來(lái)將繼續(xù)專注于技術(shù)創(chuàng)新,服務(wù)升級(jí),滿足企業(yè)一站式營(yíng)銷(xiāo)型網(wǎng)站需求,讓再小的品牌網(wǎng)站設(shè)計(jì)也能產(chǎn)生價(jià)值!
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
//我寫(xiě)了個(gè)fixString方法可以把s,轉(zhuǎn)化成漢字,然后你在比較,不就可以了
public class t {
public static void main(String[] args) {
String s = "%u4e2d%u56fd";// “中國(guó)”的Unicode編碼
s = s.replaceAll("%", "\\\\");
System.out.println("s=" + s);// 結(jié)果是s=\u4e2d\u56fd
String s2 = "\u4e2d\u56fd";
System.out.println("s2=" + s2);// 結(jié)果是“s2=中國(guó)”
System.out.println(fixString(s).equals(s2));
}
static String fixString(String s) {
while (true) {
int index = s.indexOf("\\u");
if (index != -1) {
String s1 = s.substring(index, index + 6);
if (s1.matches("\\\\u[0-9A-Fa-f]{4}")) {
char c = (char) Integer.parseInt(s1.replace("\\u", ""), 16);
s = s.substring(0, index) + c + s.substring(index + 6);
}
} else {
break;
}
}
return s;
}
}
package?org.xmh.demo;
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileFilter;
import?java.io.FileNotFoundException;
import?java.io.FileReader;
import?java.io.FileWriter;
import?java.io.IOException;
import?java.util.ArrayList;
import?java.util.List;
import?java.util.Scanner;
public?class?Test?{
public?static?void?main(String[]?args)?{
String?str?=?"";
File?file?=?new?File("D://stuinfo.txt");
FileWriter?fw?=?null;
if(!file.exists()){
try?{
file.createNewFile();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
try?{
fw?=?new?FileWriter(file);
}?catch?(IOException?e2)?{
e2.printStackTrace();
}
while(!"over".equals(str)){
Scanner?sc?=?new?Scanner(System.in);
System.out.println("請(qǐng)輸入學(xué)生信息(包括姓名,3門(mén)成績(jī),中間用逗號(hào)隔開(kāi),如果輸入over則結(jié)束輸入):");
str?=?sc.next();
if(!"over".equals(str)){
try?{
String[]?infos?=?str.split(",");
Student?st?=?new?Student(infos[0],
Integer.parseInt(infos[1]),
Integer.parseInt(infos[2]),
Integer.parseInt(infos[3]));
fw.write(st.toString()+"\n");
}?catch?(Exception?e)?{
System.out.println(e.getMessage());
System.out.println("輸入不正確,請(qǐng)重新輸入");
continue;
}
}else{
System.out.println("學(xué)生成績(jī)錄入結(jié)束");
}
}
try?{
if(fw!=null){
fw.close();
}
}?catch?(IOException?e1)?{
//?TODO?Auto-generated?catch?block
e1.printStackTrace();
}
FileReader?fr?=?null;
BufferedReader?br?=?null;
try?{
fr?=?new?FileReader(file);
br?=?new?BufferedReader(fr);
String?line?=?null;
while((line=br.readLine())!=null){
System.out.println(line);
}
}?catch?(IOException?e)?{
e.printStackTrace();
}finally{
try?{
if(br!=null){
br.close();
}
if(fr!=null){
fr.close();
}
}?catch?(IOException?e1)?{
//?TODO?Auto-generated?catch?block
e1.printStackTrace();
}
}
}
}
class?Student{
String?name;
int?score1;
int?score2;
int?score3;
public?Student(String?name,int?score1,int?score2,int?score3){
this.name?=?name;
this.score1?=?score1;
this.score2?=?score2;
this.score3?=?score3;
}
@Override
public?String?toString()?{
return?"student["+name+","+score1+","+score2+","+score3+"]";
}
}
運(yùn)行結(jié)果:
請(qǐng)輸入學(xué)生信息(包括姓名,3門(mén)成績(jī),中間用逗號(hào)隔開(kāi),如果輸入over則結(jié)束輸入):
zhangsan,20,20,20
請(qǐng)輸入學(xué)生信息(包括姓名,3門(mén)成績(jī),中間用逗號(hào)隔開(kāi),如果輸入over則結(jié)束輸入):
lisi,30,30,30
請(qǐng)輸入學(xué)生信息(包括姓名,3門(mén)成績(jī),中間用逗號(hào)隔開(kāi),如果輸入over則結(jié)束輸入):
wangwu,40,40,40
請(qǐng)輸入學(xué)生信息(包括姓名,3門(mén)成績(jī),中間用逗號(hào)隔開(kāi),如果輸入over則結(jié)束輸入):
zhaoliu,50,50,50
請(qǐng)輸入學(xué)生信息(包括姓名,3門(mén)成績(jī),中間用逗號(hào)隔開(kāi),如果輸入over則結(jié)束輸入):
over
學(xué)生成績(jī)錄入結(jié)束
student[zhangsan,20,20,20]
student[lisi,30,30,30]
student[wangwu,40,40,40]
student[zhaoliu,50,50,50]
望采納
1.
public?class?Gun?{??????????//設(shè)置一個(gè)槍類
private?String?name;??????//槍名????
private?int?num;??????????//子彈的個(gè)數(shù)
private??int??Id;???//槍的Id
static?int?count;???????
public??Gun(){}
public?Gun(String?name,?int?num,?int?id)?{
this.name=name;
this.num=12;
}
public?Gun(String?name,int?num)?{
this.name=name;
this.num=num;
this.Id=count++;
}?
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?int?getNum()?{
return?num;
}
public?void?setNum(int?num)?{
this.num?=?num;
}
public??int?getId(){
return?Id;
}
public??void?setId(int?id)?{
Id?=?id;
}
public?void?fire(){???????//槍的發(fā)射方法
??
}
@Override
public?String?toString()?{????//覆寫(xiě)toString?
return?"Gun?[name="?+?name?+?",?num="?+?num?+?",?Id="?+?Id?+?"]";
}??
}
public?class?Ak_47?extends?Gun?{
//構(gòu)造器的重載
public?Ak_47(){}???????//構(gòu)造器
public?Ak_47(String?name,int?num){???
super(name,num);
}
public??void?fire(){?????//發(fā)射子彈?????,每次射出3發(fā)
???super.setNum(super.getNum()-3);??//得到子彈的數(shù)目???
}
public?void??clip(){????//裝彈夾???每個(gè)彈夾30發(fā)子彈
???super.setNum(super.getNum()+30);???
}
public?String?toString(){?????//toString的重寫(xiě)
???return?"槍名:"+super.getName()+"\t"+"編號(hào):"+super.getId()+"\t"+"子彈數(shù):"+super.getNum();
}
}
public?class?Usp?extends?Gun?{
public?Usp(){}
public?Usp(String?name,int?num){???
super(name,num);
}
?public??void?fire(){?????//發(fā)射子彈?,每次發(fā)射一發(fā)
???super.setNum(super.getNum()-1);??//得到子彈的數(shù)目???
???}
???public?void??clip(){????//裝彈夾
???super.setNum(super.getNum()+12);???
???}
???public?String?toString(){
???return?"槍名:"+super.getName()+"\t"+"編號(hào):"+super.getId()+"\t"+"子彈數(shù):"+super.getNum();
???}
}
public?class?GunTest?{
public?static?void?main(String[]?args)?{
Ak_47?ak=new?Ak_47("ak",20);//Ak-47的彈夾可以裝30子彈
Ak_47?bk=new?Ak_47("us",12);
ak.fire();
bk.fire();
ak.clip();
bk.clip();
System.out.println(ak);
System.out.println(bk);
}
}
可以了
最簡(jiǎn)單的java代碼肯定就是這個(gè)了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是應(yīng)該是所有學(xué)java的新手看的第一個(gè)代碼了。如果是零基礎(chǔ)的新手朋友們可以來(lái)我們的java實(shí)驗(yàn)班試聽(tīng),有免費(fèi)的試聽(tīng)課程幫助學(xué)習(xí)java必備基礎(chǔ)知識(shí),有助教老師為零基礎(chǔ)的人提供個(gè)人學(xué)習(xí)方案,學(xué)習(xí)完成后有考評(píng)團(tuán)進(jìn)行專業(yè)測(cè)試,幫助測(cè)評(píng)學(xué)員是否適合繼續(xù)學(xué)習(xí)java,15天內(nèi)免費(fèi)幫助來(lái)報(bào)名體驗(yàn)實(shí)驗(yàn)班的新手快速入門(mén)java,更好的學(xué)習(xí)java!