具體如下:
成都創(chuàng)新互聯(lián)主營白云鄂網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件開發(fā),白云鄂h5微信小程序搭建,白云鄂網(wǎng)站營銷推廣歡迎白云鄂等地區(qū)企業(yè)咨詢
連連看的小源碼
package Lianliankan;
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; //退出,重列,重新開始按鈕
JLabel fractionLable=new JLabel("0"); //分?jǐn)?shù)標(biāo)簽
JButton firstButton,secondButton; //
分別記錄兩次62616964757a686964616fe59b9ee7ad9431333335326239被選中的按鈕
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;//消除方法控制
代碼(code)是程序員用開發(fā)工具所支持的語言寫出來的源文件,是一組由字符、符號(hào)或信號(hào)碼元以離散形式表示信息的明確的規(guī)則體系。
對(duì)于字符和Unicode數(shù)據(jù)的位模式的定義,此模式代表特定字母、數(shù)字或符號(hào)(例如 0x20 代表一個(gè)空格,而 0x74 代表字符“t”)。一些數(shù)據(jù)類型每個(gè)字符使用一個(gè)字節(jié);每個(gè)字節(jié)可以具有 256 個(gè)不同的位模式中的一個(gè)模式。
在計(jì)算機(jī)中,字符由不同的位模式(ON 或 OFF)表示。每個(gè)字節(jié)有 8 位,這 8 位可以有 256 種不同的 ON 和 OFF 組合模式。對(duì)于使用 1 個(gè)字節(jié)存儲(chǔ)每個(gè)字符的程序,通過給每個(gè)位模式指派字符可表示最多 256 個(gè)不同的字符。2 個(gè)字節(jié)有 16 位,這 16 位可以有 65,536 種唯一的 ON 和 OFF 組合模式。使用 2 個(gè)字節(jié)表示每個(gè)字符的程序可表示最多 65,536 個(gè)字符。
單字節(jié)代碼頁是字符定義,這些字符映射到每個(gè)字節(jié)可能有的 256 種位模式中的每一種。代碼頁定義大小寫字符、數(shù)字、符號(hào)以及 !、@、#、% 等特殊字符的位模式。每種歐洲語言(如德語和西班牙語)都有各自的單字節(jié)代碼頁。
雖然用于表示 A 到 Z 拉丁字母表字符的位模式在所有的代碼頁中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代碼頁中卻不同。如果在運(yùn)行不同代碼頁的計(jì)算機(jī)間交換數(shù)據(jù),必須將所有字符數(shù)據(jù)由發(fā)送計(jì)算機(jī)的代碼頁轉(zhuǎn)換為接收計(jì)算機(jī)的代碼頁。如果源數(shù)據(jù)中的擴(kuò)展字符在接收計(jì)算機(jī)的代碼頁中未定義,那么數(shù)據(jù)將丟失。
如果某個(gè)數(shù)據(jù)庫為來自許多不同國家的客戶端提供服務(wù),則很難為該數(shù)據(jù)庫選擇這樣一種代碼頁,使其包括所有客戶端計(jì)算機(jī)所需的全部擴(kuò)展字符。而且,在代碼頁間不停地轉(zhuǎn)換需要花費(fèi)大量的處理時(shí)間。
(我也學(xué)編程的但我沒學(xué)過這個(gè),知乎找的)Java開發(fā)黑科技LomBok
動(dòng)力節(jié)點(diǎn)
動(dòng)力節(jié)點(diǎn)
已認(rèn)證的官方帳號(hào)
Java開發(fā)黑科技LomBok
來自專欄動(dòng)力節(jié)點(diǎn)Java教程視頻庫
2 人贊同了文章
為你朗讀
6 分鐘
在面向?qū)ο缶幊讨斜夭豢缮傩枰诖a中定義對(duì)象模型,而在基于Java的業(yè)務(wù)平臺(tái)開發(fā)實(shí)踐中尤其如此。相信大家在平時(shí)開發(fā)中也深有感觸,本來是沒有多少代碼開發(fā)量的,但是因?yàn)槎x的業(yè)務(wù)模型對(duì)象比較多,而需要重復(fù)寫Getter/Setter、構(gòu)造器方法、字符串輸出的ToString方法和Equals/HashCode方法等。那么是否一款插件或工具能夠替大家完成這些繁瑣的操作呢?本文將向大家介紹一款在Eclipse/Intellij IDEA主流的開發(fā)環(huán)境中都可以使用的Java開發(fā)神器,同時(shí)簡(jiǎn)要地介紹下其背后自定義注解的原理。
Lombok的簡(jiǎn)介
Lombok是一款Java開發(fā)插件,使得Java開發(fā)者可以通過其定義的一些注解來消除業(yè)務(wù)工程中冗長(zhǎng)和繁瑣的代碼,尤其對(duì)于簡(jiǎn)單的Java模型對(duì)象(POJO)。在開發(fā)環(huán)境中使用Lombok插件后,Java開發(fā)人員可以節(jié)省出重復(fù)構(gòu)建,諸如hashCode和equals這樣的方法以及各種業(yè)務(wù)對(duì)象模型的accessor和ToString等方法的大量時(shí)間。對(duì)于這些方法,它能夠在編譯源代碼期間自動(dòng)幫我們生成這些方法,并沒有如反射那樣降低程序的性能。
在Intellij中安裝Lombok的插件
想要體驗(yàn)一把Lombok的話,得先在自己的開發(fā)環(huán)境中安裝上對(duì)應(yīng)的插件。下面先為大家展示下如何在Intellij中安裝上Lombok插件。
通過IntelliJ的插件中心尋找Lombok
從Intellij插件中心安裝Lombok
另外需要注意的是,在使用lombok注解的時(shí)候記得要導(dǎo)入lombok.jar包到工程,如果使用的是Maven的工程項(xiàng)目的話,要在其pom.xml中添加依賴如下:
package IO;
import java.io.*;
public class FileDirectoryDemo {
public static void main(String[] args) {
// 如果沒有指定參數(shù),則缺省為當(dāng)前目錄。
if (args.length == 0) {
args = new String[] { "." };
}
try {
// 新建指定目錄的File對(duì)象。
File currentPath = new File(args[0]);
// 在指定目錄新建temp目錄的File對(duì)象。
File tempPath = new File(currentPath, "temp");
// 用“tempPath”對(duì)象在指定目錄下創(chuàng)建temp目錄。
tempPath.mkdir();
// 在temp目錄下創(chuàng)建兩個(gè)文件。
File temp1 = new File(tempPath, "temp1.txt");
temp1.createNewFile();
File temp2 = new File(tempPath, "temp2.txt");
temp2.createNewFile();
// 遞歸顯示指定目錄的內(nèi)容。
System.out.println("顯示指定目錄的內(nèi)容");
listSubDir(currentPath);
// 更改文件名“temp1.txt”為“temp.txt”。
File temp1new = new File(tempPath, "temp.txt");
temp1.renameTo(temp1new);
// 遞歸顯示temp子目錄的內(nèi)容。
System.out.println("更改文件名后,顯示temp子目錄的內(nèi)容");
listSubDir(tempPath);
// 刪除文件“temp2.txt”。
temp2.delete();
// 遞歸顯示temp子目錄的內(nèi)容。
System.out.println("刪除文件后,顯示temp子目錄的內(nèi)容");
listSubDir(tempPath);
} catch (IOException e) {
System.err.println("IOException");
}
}
// 遞歸顯示指定目錄的內(nèi)容。
static void listSubDir(File currentPath) {
// 取得指定目錄的內(nèi)容列表。
String[] fileNames = currentPath.list();
try {
for (int i = 0; i fileNames.length; i++) {
File f = new File(currentPath.getPath(), fileNames[i]);
// 如果是目錄,則顯示目錄名后,遞歸調(diào)用,顯示子目錄的內(nèi)容。
if (f.isDirectory()) {
// 以規(guī)范的路徑格式顯示目錄。
System.out.println(f.getCanonicalPath());
// 遞歸調(diào)用,顯示子目錄。
listSubDir(f);
}
// 如果是文件,則顯示文件名,不包含路徑信息。
else {
System.out.println(f.getName());
}
}
} catch (IOException e) {
System.err.println("IOException");
}
}
}
package IO;
import java.io.*;
public class FileExample {
public FileExample() {
super();
}
public static void main(String[] args) {
try {
String outfile = "demoout.xml";
String infile = "demoin.xml";
/**
* 用FileOutputStream定義一個(gè)輸入流文件,然后用BuferedOutputStream調(diào)用FileOutputStream對(duì)象生成一個(gè)緩沖輸出流
然后用DataOutputStream調(diào)用BuferedOutputStream對(duì)象生成數(shù)據(jù)格式化輸出流
*/
DataOutputStream dt=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outfile)));
BufferedWriter NewFile = new BufferedWriter(new OutputStreamWriter(dt, "GBK"));
// 對(duì)中文的處理
// 定義一個(gè)輸入流
DataInputStream rafFile1 = new DataInputStream(new BufferedInputStream(new FileInputStream(infile)));
// 定義一個(gè)輸入緩沖
BufferedReader rafFile = new BufferedReader(new InputStreamReader(rafFile1, "GBK"));
String xmlcontent = "";
char tag = 0;// 文件友字符0結(jié)束
while (tag != (char) (-1)) {
xmlcontent = xmlcontent + tag + rafFile.readLine() + '\n';
tag = (char) rafFile.read();
}
NewFile.write(xmlcontent);
NewFile.flush();
NewFile.close();
rafFile.close();
System.gc();
} catch (NullPointerException exc) {
exc.printStackTrace();
} catch (java.lang.IndexOutOfBoundsException outb) {
System.out.println(outb.getMessage());
outb.printStackTrace();
} catch (FileNotFoundException fex) {
System.out.println("fex" + fex.getMessage());
} catch (IOException iex) {
System.out.println("iex" + iex.getMessage());
}
}
}
package IO;
import java.io.*;
public class FileRandomRW {
// 需要輸入的person數(shù)目。
public static int NUMBER = 3;
public static void main(String[] args) {
Persons[] people = new Persons[NUMBER];
people[0] = new Persons("張峰", 26, 2000, "N");
people[1] = new Persons("艷娜", 25, 50000, "Y");
people[2] = new Persons("李朋", 50, 7000, "F");
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(
"peoplerandom.dat"));
// 將人員數(shù)據(jù)保存至“peoplerandom.dat”二進(jìn)制文件中。
writeData(people, out);
// 關(guān)閉流。
out.close();
// 從二進(jìn)制文件“peoplerandom.dat”中逆序讀取數(shù)據(jù)。
RandomAccessFile inOut = new RandomAccessFile("peoplerandom.dat",
"rw");
Persons[] inPeople = readDataReverse(inOut);
// 輸出讀入的數(shù)據(jù)。
System.out.println("原始數(shù)據(jù):");
for (int i = 0; i inPeople.length; i++) {
System.out.println(inPeople[i]);
}
// 修改文件的第三條記錄。
inPeople[2].setSalary(4500);
// 將修改結(jié)果寫入文件。
inPeople[2].writeData(inOut, 3);
// 關(guān)閉流。
inOut.close();
// 從文件中讀入的第三條記錄,并輸出,以驗(yàn)證修改結(jié)果。
RandomAccessFile in = new RandomAccessFile("peoplerandom.dat", "r");
Persons in3People = new Persons();
// 隨機(jī)讀第三條記錄。
in3People.readData(in, 3);
// 關(guān)閉流。
in.close();
System.out.println("修改后的記錄");
System.out.println(in3People);
} catch (IOException exception) {
System.err.println("IOException");
}
}
// 將數(shù)據(jù)寫入輸出流。
static void writeData(Persons[] p, DataOutputStream out) throws IOException {
for (int i = 0; i p.length; i++) {
p[i].writeData(out);
}
}
// 將數(shù)據(jù)從輸入流中逆序讀出。
static Persons[] readDataReverse(RandomAccessFile in) throws IOException {
// 獲得記錄數(shù)目。
int record_num = (int) (in.length() / Persons.RECORD_LENGTH);
Persons[] p = new Persons[record_num];
// 逆序讀取。
for (int i = record_num - 1; i = 0; i--) {
p[i] = new Persons();
// 文件定位。
in.seek(i * Persons.RECORD_LENGTH);
p[i].readData(in, i + 1);
}
return p;
}
}
class Persons {
private String name;
private int age; // 4個(gè)字節(jié)
private double salary; // 8個(gè)字節(jié)
private String married;
public static final int NAME_LENGTH = 20; // 姓名長(zhǎng)度
public static final int MARRIED_LENGTH = 2; // 婚否長(zhǎng)度
public static final int RECORD_LENGTH = NAME_LENGTH * 2 + 4 + 8
+ MARRIED_LENGTH * 2;
public Persons() {
}
public Persons(String n, int a, double s) {
name = n;
age = a;
salary = s;
married = "F";
}
public Persons(String n, int a, double s, String m) {
name = n;
age = a;
salary = s;
married = m;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getMarried() {
return married;
}
public String setName(String n) {
name = n;
return name;
}
public int setAge(int a) {
age = a;
return age;
}
public double setSalary(double s) {
salary = s;
return salary;
}
public String setMarried(String m) {
married = m;
return married;
}
// 設(shè)置輸出格式。
public String toString() {
return getClass().getName() + "[name=" + name + ",age=" + age
+ ",salary=" + salary + ",married=" + married + "]";
}
// 寫入一條固定長(zhǎng)度的記錄,即一個(gè)人的數(shù)據(jù)到輸出流。
public void writeData(DataOutput out) throws IOException {
FixStringIO.writeFixString(name, NAME_LENGTH, out);
out.writeInt(age);
out.writeDouble(salary);
FixStringIO.writeFixString(married, MARRIED_LENGTH, out);
}
// 寫入一條固定長(zhǎng)度的記錄到隨機(jī)讀取文件中。
private void writeData(RandomAccessFile out) throws IOException {
FixStringIO.writeFixString(name, NAME_LENGTH, out);
out.writeInt(age);
out.writeDouble(salary);
FixStringIO.writeFixString(married, MARRIED_LENGTH, out);
}
// 隨機(jī)寫入一條固定長(zhǎng)度的記錄到輸出流的指定位置。
public void writeData(RandomAccessFile out, int n) throws IOException {
out.seek((n - 1) * RECORD_LENGTH);
writeData(out);
}
// 從輸入流隨機(jī)讀入一條記錄,即一個(gè)人的數(shù)據(jù)。
private void readData(RandomAccessFile in) throws IOException {
name = FixStringIO.readFixString(NAME_LENGTH, in);
age = in.readInt();
salary = in.readDouble();
married = FixStringIO.readFixString(MARRIED_LENGTH, in);
}
// 從輸入流隨機(jī)讀入指定位置的記錄。
public void readData(RandomAccessFile in, int n) throws IOException {
in.seek((n - 1) * RECORD_LENGTH);
readData(in);
}
}
// 對(duì)固定長(zhǎng)度字符串從文件讀出、寫入文件
class FixStringIO {
// 讀取固定長(zhǎng)度的Unicode字符串。
public static String readFixString(int size, DataInput in)
throws IOException {
StringBuffer b = new StringBuffer(size);
int i = 0;
boolean more = true;
while (more i size) {
char ch = in.readChar();
i++;
if (ch == 0) {
more = false;
} else {
b.append(ch);
}
}
// 跳過剩余的字節(jié)。
in.skipBytes(2 * (size - i));
return b.toString();
}
// 寫入固定長(zhǎng)度的Unicode字符串。
public static void writeFixString(String s, int size, DataOutput out)
throws IOException {
int i;
for (i = 0; i size; i++) {
char ch = 0;
if (i s.length()) {
ch = s.charAt(i);
}
out.writeChar(ch);
}
}
}
package IO;
import java.io.*;
import java.util.*;
public class FileRW {
// 需要輸入的person數(shù)目。
public static int NUMBER = 3;
public static void main(String[] args) {
Person[] people = new Person[NUMBER];
// 暫時(shí)容納輸入數(shù)據(jù)的臨時(shí)字符串?dāng)?shù)組。
String[] field = new String[4];
// 初始化field數(shù)組。
for (int i = 0; i 4; i++) {
field[i] = "";
}
// IO操作必須捕獲IO異常。
try {
// 用于對(duì)field數(shù)組進(jìn)行增加控制。
int fieldcount = 0;
// 先使用System.in構(gòu)造InputStreamReader,再構(gòu)造BufferedReader。
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
for (int i = 0; i NUMBER; i++) {
fieldcount = 0;
System.out.println("The number " + (i + 1) + " person");
System.out
.println("Enter name,age,salary,married(optional),please separate fields by ':'");
// 讀取一行。
String personstr = stdin.readLine();
// 設(shè)置分隔符。
StringTokenizer st = new StringTokenizer(personstr, ":");
// 判斷是否還有分隔符可用。
while (st.hasMoreTokens()) {
field[fieldcount] = st.nextToken();
fieldcount++;
}
// 如果輸入married,則field[3]不為空,調(diào)用具有四個(gè)參數(shù)的Person構(gòu)造函數(shù)。
if (field[3] != "") {
people[i] = new Person(field[0],
Integer.parseInt(field[1]), Double
.parseDouble(field[2]), field[3]);
// 置field[3]為空,以備下次輸入使用。
field[3] = "";
}
// 如果未輸入married,則field[3]為空,調(diào)用具有三個(gè)參數(shù)的Person構(gòu)造函數(shù)。
else {
people[i] = new Person(field[0],
Integer.parseInt(field[1]), Double
.parseDouble(field[2]));
}
}
// 將輸入的數(shù)據(jù)保存至“people.dat”文本文件中。
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter("people.dat")));
writeData(people, out);
// 關(guān)閉流。
out.close();
// 從文件“people.dat”讀取數(shù)據(jù)。
BufferedReader in = new BufferedReader(new FileReader("people.dat"));
Person[] inPeople = readData(in);
// 關(guān)閉流。
in.close();
// 輸出從文件中讀入的數(shù)據(jù)。
for (int i = 0; i inPeople.length; i++) {
System.out.println(inPeople[i]);
}
} catch (IOException exception) {
System.err.println("IOException");
}
}
// 將所有數(shù)據(jù)寫入輸出流。
static void writeData(Person[] p, PrintWriter out) throws IOException {
// 寫入記錄條數(shù),即人數(shù)。
out.println(p.length);
for (int i = 0; i p.length; i++) {
p[i].writeData(out);
}
}
// 將所有數(shù)據(jù)從輸入流中讀出。
static Person[] readData(BufferedReader in) throws IOException {
// 獲取記錄條數(shù),即人數(shù)。
int n = Integer.parseInt(in.readLine());
Person[] p = new Person[n];
for (int i = 0; i n; i++) {
p[i] = new Person();
p[i].readData(in);
}
return p;
}
}
class Person {
private String name;
private int age;
private double salary;
private String married;
public Person() {
}
public Person(String n, int a, double s) {
name = n;
age = a;
salary = s;
married = "F";
}
public Person(String n, int a, double s, String m) {
name = n;
age = a;
salary = s;
married = m;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getMarried() {
return married;
}
// 設(shè)置輸出格式。
public String toString() {
return getClass().getName() + "[name=" + name + ",age=" + age
+ ",salary=" + salary + ",married=" + married + "]";
}
// 寫入一條記錄,即一個(gè)人的數(shù)據(jù)到輸出流。
public void writeData(PrintWriter out) throws IOException {
// 格式化輸出。
out.println(name + ":" + age + ":" + salary + ":" + married);
}
// 從輸入流讀入一條記錄,即一個(gè)人的數(shù)據(jù)。
public void readData(BufferedReader in) throws IOException {
String s = in.readLine();
StringTokenizer t = new StringTokenizer(s, ":");
name = t.nextToken();
age = Integer.parseInt(t.nextToken());
salary = Double.parseDouble(t.nextToken());
married = t.nextToken();
}
}
package IO;
import java.io.*;
public class IOStreamExample {
public static void main(String[] args) throws IOException {
// 1. 讀入一行數(shù)據(jù):
BufferedReader in = new BufferedReader(new FileReader(
"FileStdRead.java"));
String s, s2 = new String();
while ((s = in.readLine()) != null) {
s2 += s + "\n";
}
in.close();
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
System.out.print("Enter a line:");
System.out.println(stdin.readLine());
// 2. 從內(nèi)存中讀入
StringReader in2 = new StringReader(s2);
int c;
while ((c = in2.read()) != -1) {
System.out.print((char) c);
}
// 3. 格式化內(nèi)存輸入
try {
DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(
s2.getBytes()));
while (true) {
System.out.print((char) in3.readByte());
}
} catch (EOFException e) {
System.err.println("End of stream");
}
// 4. 文件輸入
try {
BufferedReader in4 = new BufferedReader(new StringReader(s2));
PrintWriter out1 = new PrintWriter(new BufferedWriter(
new FileWriter("IODemo.out")));
int lineCount = 1;
while ((s = in4.readLine()) != null) {
out1.println(lineCount++ + ": " + s);
}
out1.close();
} catch (EOFException e) {
System.err.println("End of stream");
}
// 5. 接收和保存數(shù)據(jù)
try {
DataOutputStream out2 = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream("Data.txt")));
out2.writeDouble(3.14159);
out2.writeUTF("That was pi");
out2.writeDouble(1.41413);
out2.writeUTF("Square root of 2");
out2.close();
DataInputStream in5 = new DataInputStream(new BufferedInputStream(
new FileInputStream("Data.txt")));
System.out.println(in5.readDouble());
System.out.println(in5.readUTF());
System.out.println(in5.readDouble());
System.out.println(in5.readUTF());
} catch (EOFException e) {
throw new RuntimeException(e);
}
// 6. 隨機(jī)讀取文件內(nèi)容
RandomAccessFile rf = new RandomAccessFile("rtest.dat", "rw");
for (int i = 0; i 10; i++) {
rf.writeDouble(i * 1.414);
}
rf.close();
rf = new RandomAccessFile("rtest.dat", "rw");
rf.seek(5 * 8);
rf.writeDouble(47.0001);
rf.close();
rf = new RandomAccessFile("rtest.dat", "r");
for (int i = 0; i 10; i++) {
System.out.println("Value " + i + ": " + rf.readDouble());
}
rf.close();
}
}
package IO;
import java.io.*;
/**
* p
* Title: JAVA進(jìn)階訣竅
* /p
*
* @author 張峰
* @version 1.0
*/
public class MakeDirectoriesExample {
private static void fileattrib(File f) {
System.out.println("絕對(duì)路徑: " + f.getAbsolutePath() + "\n 可讀屬性: "
+ f.canRead() + "\n 可定屬性: " + f.canWrite() + "\n 文件名: "
+ f.getName() + "\n 父目錄: " + f.getParent() + "\n 當(dāng)前路徑: "
+ f.getPath() + "\n 文件長(zhǎng)度: " + f.length() + "\n 最后更新日期: "
+ f.lastModified());
if (f.isFile()) {
System.out.println("輸入的是一個(gè)文件");
} else if (f.isDirectory()) {
System.out.println("輸入的是一個(gè)目錄");
}
}
public static void main(String[] args) {
if (args.length 1) {
args = new String[3];
}
args[0] = "d";
args[1] = "test1.txt";
args[2] = "test2.txt";
File old = new File(args[1]), rname = new File(args[2]);
old.renameTo(rname);
fileattrib(old);
fileattrib(rname);
int count = 0;
boolean del = false;
if (args[0].equals("d")) {
count++;
del = true;
}
count--;
while (++count args.length) {
File f = new File(args[count]);
if (f.exists()) {
System.out.println(f + " 文件己經(jīng)存在");
if (del) {
System.out.println("刪除文件" + f);
f.delete();
}
} else { // 如果文件不存在
if (!del) {
f.mkdirs();
System.out.println("創(chuàng)建文件: " + f);
}
}
fileattrib(f);
}
}
}
連連看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];//游戲按鈕數(shù)組
JButton exitButton,resetButton,newlyButton; //退出,重列,重新開始按鈕
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("再來一局");
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)說明第一按鈕在第二按鈕左邊
for (i=y-1;i=j;i-- ){ //判斷第二按鈕左側(cè)直到第一按鈕中間有沒有按鈕
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1說明通過了第一次驗(yàn)證
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二個(gè)按鈕的Y坐標(biāo)小于空按鈕的Y坐標(biāo)說明第一按鈕在第二按鈕右邊
for (i=y+1;i=j ;i++ ){ //判斷第二按鈕左側(cè)直到第一按鈕中間有沒有按鈕
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è)空按鈕之間有沒按鈕
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2說明通過了第二次驗(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、編寫一個(gè)猜數(shù)字的游戲,由電腦隨機(jī)產(chǎn)生一個(gè)100以內(nèi)的整數(shù),讓用戶去猜,如果用戶猜的比電腦大,則輸出“大了,再小點(diǎn)!”,反之則輸出“小了,再大點(diǎn)!”,用戶總共只能猜十次,并根據(jù)用戶正確猜出答案所用的次數(shù)輸出相應(yīng)的信息,如:只用一次就猜對(duì),輸出“你是個(gè)天才!”,八次才猜對(duì),輸出“笨死了!”,如果十次還沒有猜對(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以內(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ān)了!");
else if (i=8)
System.out.println("但是你還……真笨!");
else
System.out.println("你和豬沒有兩樣了!");
break;}
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame
extends JFrame {
JTextField text;
JLabel nowBomb, setBomb;
int BombNum, BlockNum; // 當(dāng)前雷數(shù),當(dāng)前方塊數(shù)
int rightBomb, restBomb, restBlock; // 找到的地雷數(shù),剩余雷數(shù),剩余方塊數(shù)
JButton start = new JButton(" 開始 ");
JPanel MenuPamel = new JPanel();
JPanel bombPanel = new JPanel();
Bomb[][] bombButton;
JPanel c;
BorderLayout borderLayout1 = new BorderLayout();
GridLayout gridLayout1 = new GridLayout();
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
c = (JPanel) getContentPane();
setTitle("掃雷");
c.setBackground(Color.WHITE);
MenuPamel.setBackground(Color.GRAY);
c.setLayout(borderLayout1);
setSize(new Dimension(600, 600));
setResizable(false);
BlockNum = 144;
BombNum = 10;
text = new JTextField("10 ", 3);
nowBomb = new JLabel("當(dāng)前雷數(shù)" + ":" + BombNum);
setBomb = new JLabel("設(shè)置地雷數(shù)");
start.addActionListener(new Frame1_start_actionAdapter(this));
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel, java.awt.BorderLayout.SOUTH);
bombPanel.setLayout(gridLayout1);
gridLayout1.setColumns( (int) Math.sqrt(BlockNum));
gridLayout1.setRows( (int) Math.sqrt(BlockNum));
bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)];
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j] = new Bomb(i, j);
//bombButton[i][j].setSize(10, 10);
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//設(shè)置字體大小
bombButton[i][j].setForeground(Color.white);
bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));
bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));
bombPanel.add(bombButton[i][j]);
}
}
c.add(bombPanel, java.awt.BorderLayout.CENTER);
startBomb();
}
/* 開始按鈕 */
public void start_actionPerformed(ActionEvent e) {
int num=Integer.parseInt(text.getText().trim());
if (num = 5 num 50) {
BombNum = num;
startBomb();
}
else if (num 5) {
JOptionPane.showMessageDialog(null, "您設(shè)置的地雷數(shù)太少了,請(qǐng)重設(shè)!", "錯(cuò)誤",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
else {
JOptionPane.showMessageDialog(null, "您設(shè)置的地雷數(shù)太多了,請(qǐng)重設(shè)!", "錯(cuò)誤",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
}
/* 開始,布雷 */
public void startBomb() {
nowBomb.setText("當(dāng)前雷數(shù)" + ":" + BombNum);
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j].isBomb = false;
bombButton[i][j].isClicked = false;
bombButton[i][j].isRight = false;
bombButton[i][j].BombFlag = 0;
bombButton[i][j].BombRoundCount = 9;
bombButton[i][j].setEnabled(true);
bombButton[i][j].setText("");
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//設(shè)置字體大小
bombButton[i][j].setForeground(Color.BLUE);
rightBomb = 0;
restBomb = BombNum;
restBlock = BlockNum - BombNum;
}
}
for (int i = 0; i BombNum; ) {
int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
if (bombButton[x][y].isBomb != true) {
bombButton[x][y].isBomb = true;
i++;
}
}
CountRoundBomb();
}
/* 計(jì)算方塊周圍雷數(shù) */
public void CountRoundBomb() {
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
int count = 0;
// 當(dāng)需要檢測(cè)的單元格本身無地雷的情況下,統(tǒng)計(jì)周圍的地雷個(gè)數(shù)
if (bombButton[i][j].isBomb != true) {
for (int x = i - 1; x i + 2; x++) {
for (int y = j - 1; y j + 2; y++) {
if ( (x = 0) (y = 0)
(x ( (int) Math.sqrt(BlockNum)))
(y ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == true) {
count++;
}
}
}
}
bombButton[i][j].BombRoundCount = count;
}
}
}
}
/* 是否挖完了所有的雷 */
public void isWin() {
restBlock = BlockNum - BombNum;
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isClicked == true) {
restBlock--;
}
}
}
if (rightBomb == BombNum || restBlock == 0) {
JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您勝利了!", "勝利",
JOptionPane.INFORMATION_MESSAGE);
startBomb();
}
}
/** 當(dāng)選中的位置為空,則翻開周圍的地圖* */
public void isNull(Bomb ClickedButton) {
int i, j;
i = ClickedButton.num_x;
j = ClickedButton.num_y;
for (int x = i - 1; x i + 2; x++) {
for (int y = j - 1; y j + 2; y++) {
if ( ( (x != i) || (y != j)) (x = 0) (y = 0)
(x ( (int) Math.sqrt(BlockNum)))
(y ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == false
bombButton[x][y].isClicked == false
bombButton[x][y].isRight == false) {
turn(bombButton[x][y]);
}
}
}
}
}
/* 翻開 */
public void turn(Bomb ClickedButton) {
ClickedButton.setEnabled(false);
ClickedButton.isClicked = true;
if (ClickedButton.BombRoundCount 0) {
ClickedButton.setText(ClickedButton.BombRoundCount + "");
}
else {
isNull(ClickedButton);
}
}
/* 左鍵點(diǎn)擊 */
public void actionPerformed(ActionEvent e) {
if ( ( (Bomb) e.getSource()).isClicked == false
( (Bomb) e.getSource()).isRight == false) {
if ( ( (Bomb) e.getSource()).isBomb == false) {
turn( ( (Bomb) e.getSource()));
isWin();
}
else {
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isBomb == true) {
bombButton[i][j].setText("b");
}
}
}
( (Bomb) e.getSource()).setForeground(Color.RED);
( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20));
( (Bomb) e.getSource()).setText("X");
JOptionPane.showMessageDialog(this, "你踩到地雷了,按確定重來", "踩到地雷", 2);
startBomb();
}
}
}
/* 右鍵點(diǎn)擊 */
public void mouseClicked(MouseEvent e) {
Bomb bombSource = (Bomb) e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e);
if ( (right == true) (bombSource.isClicked == false)) {
bombSource.BombFlag = (bombSource.BombFlag + 1) % 3;
if (bombSource.BombFlag == 1) {
if (restBomb 0) {
bombSource.setForeground(Color.RED);
bombSource.setText("F");
bombSource.isRight = true;
restBomb--;
}
else {
bombSource.BombFlag = 0;
}
}
else if (bombSource.BombFlag == 2) {
restBomb++;
bombSource.setText("Q");
bombSource.isRight = false;
}
else {
bombSource.setText("");
}
if (bombSource.isBomb == true) {
if (bombSource.BombFlag == 1) {
rightBomb++;
}
else if (bombSource.BombFlag == 2) {
rightBomb--;
}
}
nowBomb.setText("當(dāng)前雷數(shù)" + ":" + restBomb);
isWin();
}
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
}
}
class Frame1_start_actionAdapter
implements ActionListener {
private Frame adaptee;
Frame1_start_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.start_actionPerformed(e);
}
}
////////////////////////////
class Bomb
extends JButton {
int num_x, num_y; // 第幾號(hào)方塊
int BombRoundCount; // 周圍雷數(shù)
boolean isBomb; // 是否為雷
boolean isClicked; // 是否被點(diǎn)擊
int BombFlag; // 探雷標(biāo)記
boolean isRight; // 是否點(diǎn)擊右鍵
public Bomb(int x, int y) {
num_x = x;
num_y = y;
BombFlag = 0;
BombRoundCount = 9;
isBomb = false;
isClicked = false;
isRight = false;
}
}
class Bomb_actionAdapter
implements ActionListener {
private Frame adaptee;
Bomb_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.actionPerformed(e);
}
}
class Bomb_mouseAdapter
extends MouseAdapter {
private Frame adaptee;
Bomb_mouseAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.mouseClicked(e);
}
}
Shape.java接口代碼
public interface Shape {
public static final double PI = 3.14d;
public double area();
}
Circle.java圓類代碼
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
? this.radius = radius;
}
@Override
public double area() {
? return PI * this.radius * this.radius;
}
public double perimeter() {
? return 2 * PI * this.radius;
}
}
Cylinder.java圓柱體類代碼
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
? super(radius);
? this.height = height;
}
public double area() {
? return 2 * super.area() + super.perimeter() * this.height;
}
public double volume() {
? return super.area() * this.height;
}
}
X5_3_6.java主類代碼
public class X5_3_6 {
public static void main(String[] args) {
? Circle cir1 = new Circle(5);
? System.out.println("圓的面積為:" + cir1.area());
? System.out.println("圓的周長(zhǎng)為:" + cir1.perimeter());
? Cylinder cy1 = new Cylinder(10, 15);
? System.out.println("圓柱體的表面積為:" + cy1.area());
? System.out.println("圓柱體的體積為:" + cy1.volume());
}
}
上面是我寫的代碼,下圖是執(zhí)行結(jié)果,麻煩看一下,是否可以。