资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java优秀代码样例 java爬虫代码示例

java编程的冒泡等排序示例

Java排序算法

创新互联公司是一家集网站建设,凌河企业网站建设,凌河品牌网站建设,网站定制,凌河网站建设报价,网络营销,网络优化,凌河网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

1)分类:

1)插入排序(直接插入排序、希尔排序)

2)交换排序(冒泡排序、快速排序)

3)选择排序(直接选择排序、堆排序)

4)归并排序

5)分配排序(箱排序、基数排序)

所需辅助空间最多:归并排序

所需辅助空间最少:堆排序

平均速度最快:快速排序

不稳定:快速排序,希尔排序,堆排序。

1)选择排序算法的时候

1.数据的规模 ; 2.数据的类型 ; 3.数据已有的顺序

一般来说,当数据规模较小时,应选择直接插入排序或冒泡排序。任何排序算法在数据量小时基本体现不出来差距。 考虑数据的类型,比如如果全部是正整数,那么考虑使用桶排序为最优。 考虑数据已有顺序,快排是一种不稳定的排序(当然可以改进),对于大部分排好的数据,快排会浪费大量不必要的步骤。数据量极小,而起已经基本排好序,冒泡是最佳选择。我们说快排好,是指大量随机数据下,快排效果最理想。而不是所有情况。

3)总结:

——按平均的时间性能来分:

1)时间复杂度为O(nlogn)的方法有:快速排序、堆排序和归并排序,其中以快速排序为最好;

2)时间复杂度为O(n2)的有:直接插入排序、起泡排序和简单选择排序,其中以直接插入为最好,特 别是对那些对关键字近似有序的记录序列尤为如此;

3)时间复杂度为O(n)的排序方法只有,基数排序。

当待排记录序列按关键字顺序有序时,直接插入排序和起泡排序能达到O(n)的时间复杂度;而对于快速排序而言,这是最不好的情况,此时的时间性能蜕化为O(n2),因此是应该尽量避免的情况。简单选择排序、堆排序和归并排序的时间性能不随记录序列中关键字的分布而改变。

——按平均的空间性能来分(指的是排序过程中所需的辅助空间大小):

1) 所有的简单排序方法(包括:直接插入、起泡和简单选择)和堆排序的空间复杂度为O(1);

2) 快速排序为O(logn ),为栈所需的辅助空间;

3) 归并排序所需辅助空间最多,其空间复杂度为O(n );

4)链式基数排序需附设队列首尾指针,则空间复杂度为O(rd )。

——排序方法的稳定性能:

1) 稳定的排序方法指的是,对于两个关键字相等的记录,它们在序列中的相对位置,在排序之前和 经过排序之后,没有改变。

2) 当对多关键字的记录序列进行LSD方法排序时,必须采用稳定的排序方法。

3) 对于不稳定的排序方法,只要能举出一个实例说明即可。

4) 快速排序,希尔排序和堆排序是不稳定的排序方法。

4)插入排序:

包括直接插入排序,希尔插入排序。

直接插入排序: 将一个记录插入到已经排序好的有序表中。

1, sorted数组的第0个位置没有放数据。

2,从sorted第二个数据开始处理:

如果该数据比它前面的数据要小,说明该数据要往前面移动。

首先将该数据备份放到 sorted的第0位置当哨兵。

然后将该数据前面那个数据后移。

然后往前搜索,找插入位置。

找到插入位置之后讲 第0位置的那个数据插入对应位置。

O(n*n), 当待排记录序列为正序时,时间复杂度提高至O(n)。

希尔排序(缩小增量排序 diminishing increment sort):先将整个待排记录序列分割成若干个子序列分别进行直接插入排序,待整个序列中的记录基本有序时,再对全体记录进行一次直接插入排序。

面试穿什么,这里找答案!

插入排序Java代码:

public class InsertionSort {

// 插入排序:直接插入排序 ,希尔排序

public void straightInsertionSort(double [] sorted){

int sortedLen= sorted.length;

for(int j=2;jsortedLen;j++){

if(sorted[j]sorted[j-1]){

sorted[0]= sorted[j];//先保存一下后面的那个

sorted[j]=sorted[j-1];// 前面的那个后移。

int insertPos=0;

for(int k=j-2;k=0;k--){

if(sorted[k]sorted[0]){

sorted[k+1]=sorted[k];

}else{

insertPos=k+1;

break;

}

}

sorted[insertPos]=sorted[0];

}

}

}

public void shellInertionSort(double [] sorted, int inc){

int sortedLen= sorted.length;

for(int j=inc+1;jsortedLen;j++ ){

if(sorted[j]sorted[j-inc]){

sorted[0]= sorted[j];//先保存一下后面的那个

int insertPos=j;

for(int k=j-inc;k=0;k-=inc){

if(sorted[k]sorted[0]){

sorted[k+inc]=sorted[k];

//数据结构课本上这个地方没有给出判读,出错:

if(k-inc=0){

insertPos = k;

}

}else{

insertPos=k+inc;

break;

}

}

sorted[insertPos]=sorted[0];

}

}

}

public void shellInsertionSort(double [] sorted){

int[] incs={7,5,3,1};

int num= incs.length;

int inc=0;

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

inc= incs[j];

shellInertionSort(sorted,inc);

}

}

public static void main(String[] args) {

Random random= new Random(6);

int arraysize= 21;

double [] sorted=new double[arraysize];

System.out.print("Before Sort:");

for(int j=1;jarraysize;j++){

sorted[j]= (int)(random.nextDouble()* 100);

System.out.print((int)sorted[j]+" ");

}

System.out.println();

InsertionSort sorter=new InsertionSort();

// sorter.straightInsertionSort(sorted);

sorter.shellInsertionSort(sorted);

System.out.print("After Sort:");

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

System.out.print((int)sorted[j]+" ");

}

System.out.println();

}

}

面试穿什么,这里找答案!

5)交换排序:

包括冒泡排序,快速排序。

冒泡排序法:该算法是专门针对已部分排序的数据进行排序的一种排序算法。如果在你的数据清单中只有一两个数据是乱序的话,用这种算法就是最快的排序算法。如果你的数据清单中的数据是随机排列的,那么这种方法就成了最慢的算法了。因此在使用这种算法之前一定要慎重。这种算法的核心思想是扫描数据清单,寻找出现乱序的两个相邻的项目。当找到这两个项目后,交换项目的位置然后继续扫描。重复上面的操作直到所有的项目都按顺序排好。

快速排序:通过一趟排序,将待排序记录分割成独立的两个部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。具体做法是:使用两个指针low,high, 初值分别设置为序列的头,和序列的尾,设置pivotkey为第一个记录,首先从high开始向前搜索第一个小于pivotkey的记录和pivotkey所在位置进行交换,然后从low开始向后搜索第一个大于pivotkey的记录和此时pivotkey所在位置进行交换,重复知道low=high了为止。

交换排序Java代码:

public class ExchangeSort {

public void BubbleExchangeSort(double [] sorted){

int sortedLen= sorted.length;

for(int j=sortedLen;j0;j--){

int end= j;

for(int k=1;kend-1;k++){

double tempB= sorted[k];

sorted[k]= sorted[k]sorted[k+1]?

sorted[k]:sorted[k+1];

if(Math.abs(sorted[k]-tempB)10e-6){

sorted[k+1]=tempB;

}

}

}

}

public void QuickExchangeSortBackTrack(double [] sorted,

int low,int high){

if(lowhigh){

int pivot= findPivot(sorted,low,high);

QuickExchangeSortBackTrack(sorted,low,pivot-1);

QuickExchangeSortBackTrack(sorted,pivot+1,high);

}

}

public int findPivot(double [] sorted, int low, int high){

sorted[0]= sorted[low];

while(lowhigh){

while(lowhigh sorted[high]= sorted[0])--high;

sorted[low]= sorted[high];

while(lowhigh sorted[low]=sorted[0])++low;

sorted[high]= sorted[low];

}

sorted[low]=sorted[0];

return low;

}

public static void main(String[] args) {

Random random= new Random(6);

int arraysize= 21;

double [] sorted=new double[arraysize];

System.out.print("Before Sort:");

for(int j=1;jarraysize;j++){

sorted[j]= (int)(random.nextDouble()* 100);

System.out.print((int)sorted[j]+" ");

}

System.out.println();

ExchangeSort sorter=new ExchangeSort();

// sorter.BubbleExchangeSort(sorted);

sorter.QuickExchangeSortBackTrack(sorted, 1, arraysize-1);

System.out.print("After Sort:");

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

System.out.print((int)sorted[j]+" ");

}

System.out.println();

}

}

6)选择排序:

分为直接选择排序, 堆排序

直接选择排序:第i次选取 i到array.Length-1中间最小的值放在i位置。

堆排序:首先,数组里面用层次遍历的顺序放一棵完全二叉树。从最后一个非终端结点往前面调整,直到到达根结点,这个时候除根节点以外的所有非终端节点都已经满足堆得条件了,于是需要调整根节点使得整个树满足堆得条件,于是从根节点开始,沿着它的儿子们往下面走(最大堆沿着最大的儿子走,最小堆沿着最小的儿子走)。 主程序里面,首先从最后一个非终端节点开始调整到根也调整完,形成一个heap, 然后将heap的根放到后面去(即:每次的树大小会变化,但是 root都是在1的位置,以方便计算儿子们的index,所以如果需要升序排列,则要逐步大顶堆。因为根节点被一个个放在后面去了。 降序排列则要建立小顶堆)

代码中的问题: 有时候第2个和第3个顺序不对(原因还没搞明白到底代码哪里有错)

选择排序Java代码:

public class SelectionSort {

public void straitSelectionSort(double [] sorted){

int sortedLen= sorted.length;

for(int j=1;jsortedLen;j++){

int jMin= getMinIndex(sorted,j);

exchange(sorted,j,jMin);

}

}

public void exchange(double [] sorted,int i,int j){

int sortedLen= sorted.length;

if(isortedLen jsortedLen ij i=0 j=0){

double temp= sorted[i];

sorted[i]=sorted[j];

sorted[j]=temp;

}

}

public int getMinIndex(double [] sorted, int i){

int sortedLen= sorted.length;

int minJ=1;

double min= Double.MAX_VALUE;

for(int j=i;jsortedLen;j++){

if(sorted[j]min){

min= sorted[j];

minJ= j;

}

}

return minJ;

}

public void heapAdjust(double [] sorted,int start,int end){

if(startend){

double temp= sorted;

// 这个地方jend与课本不同,j=end会报错:

for(int j=2*start;jend;j *=2){

if(j+1end sorted[j]-sorted[j+1]10e-6){

++j;

}

if(temp=sorted[j]){

break;

}

sorted=sorted[j];

start=j;

}

sorted=temp;

}

}

public void heapSelectionSort(double [] sorted){

int sortedLen = sorted.length;

for(int i=sortedLen/2;i0;i--){

heapAdjust(sorted,i,sortedLen);

}

for(int i=sortedLen;i1;--i){

exchange(sorted,1,i);

heapAdjust(sorted,1,i-1);

}

}

public static void main(String [] args){

Random random= new Random(6);

int arraysize=9;

double [] sorted=new double[arraysize];

System.out.print("Before Sort:");

for(int j=1;jarraysize;j++){

sorted[j]= (int)(random.nextDouble()* 100);

System.out.print((int)sorted[j]+" ");

}

System.out.println();

SelectionSort sorter=new SelectionSort();

// sorter.straitSelectionSort(sorted);

sorter.heapSelectionSort(sorted);

System.out.print("After Sort:");

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

System.out.print((int)sorted[j]+" ");

}

System.out.println();

}

}

面试穿什么,这里找答案!

7)归并排序:

将两个或两个以上的有序表组合成一个新的有序表。归并排序要使用一个辅助数组,大小跟原数组相同,递归做法。每次将目标序列分解成两个序列,分别排序两个子序列之后,再将两个排序好的子序列merge到一起。

归并排序Java代码:

public class MergeSort {

private double[] bridge;//辅助数组

public void sort(double[] obj){

if (obj == null){

throw new NullPointerException("

The param can not be null!");

}

bridge = new double[obj.length]; // 初始化中间数组

mergeSort(obj, 0, obj.length - 1); // 归并排序

bridge = null;

}

private void mergeSort(double[] obj, int left, int right){

if (left right){

int center = (left + right) / 2;

mergeSort(obj, left, center);

mergeSort(obj, center + 1, right);

merge(obj, left, center, right);

}

}

private void merge(double[] obj, int left,

int center, int right){

int mid = center + 1;

int third = left;

int tmp = left;

while (left = center mid = right){

// 从两个数组中取出小的放入中间数组

if (obj[left]-obj[mid]=10e-6){

bridge[third++] = obj[left++];

} else{

bridge[third++] = obj[mid++];

}

}

// 剩余部分依次置入中间数组

while (mid = right){

bridge[third++] = obj[mid++];

}

while (left = center){

bridge[third++] = obj[left++];

}

// 将中间数组的内容拷贝回原数组

copy(obj, tmp, right);

}

private void copy(double[] obj, int left, int right)

{

while (left = right){

obj[left] = bridge[left];

left++;

}

}

public static void main(String[] args) {

Random random = new Random(6);

int arraysize = 10;

double[] sorted = new double[arraysize];

System.out.print("Before Sort:");

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

sorted[j] = (int) (random.nextDouble() * 100);

System.out.print((int) sorted[j] + " ");

}

System.out.println();

MergeSort sorter = new MergeSort();

sorter.sort(sorted);

System.out.print("After Sort:");

for (int j = 0; j sorted.length; j++) {

System.out.print((int) sorted[j] + " ");

}

System.out.println();

}

}

面试穿什么,这里找答案!

8)基数排序:

使用10个辅助队列,假设最大数的数字位数为 x, 则一共做 x次,从个位数开始往前,以第i位数字的大小为依据,将数据放进辅助队列,搞定之后回收。下次再以高一位开始的数字位为依据。

以Vector作辅助队列,基数排序的Java代码:

public class RadixSort {

private int keyNum=-1;

private VectorVectorDouble util;

public void distribute(double [] sorted, int nth){

if(nth=keyNum nth0){

util=new VectorVectorDouble();

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

Vector Double temp= new Vector Double();

util.add(temp);

}

for(int j=0;jsorted.length;j++){

int index= getNthDigit(sorted[j],nth);

util.get(index).add(sorted[j]);

}

}

}

public int getNthDigit(double num,int nth){

String nn= Integer.toString((int)num);

int len= nn.length();

if(len=nth){

return Character.getNumericValue(nn.charAt(len-nth));

}else{

return 0;

}

}

public void collect(double [] sorted){

int k=0;

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

int len= util.get(j).size();

if(len0){

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

sorted[k++]= util.get(j).get(i);

}

}

}

util=null;

}

public int getKeyNum(double [] sorted){

double max= Double.MIN_VALUE;

for(int j=0;jsorted.length;j++){

if(sorted[j]max){

max= sorted[j];

}

}

return Integer.toString((int)max).length();

}

public void radixSort(double [] sorted){

if(keyNum==-1){

keyNum= getKeyNum(sorted);

}

for(int i=1;i=keyNum;i++){

distribute(sorted,i);

collect(sorted);

}

}

public static void main(String[] args) {

Random random = new Random(6);

int arraysize = 21;

double[] sorted = new double[arraysize];

System.out.print("Before Sort:");

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

sorted[j] = (int) (random.nextDouble() * 100);

System.out.print((int) sorted[j] + " ");

}

System.out.println();

RadixSort sorter = new RadixSort();

sorter.radixSort(sorted);

System.out.print("After Sort:");

for (int j = 0; j sorted.length; j++) {

System.out.print((int) sorted[j] + " ");

}

System.out.println();

}

}

//copy而来

用java编写一个简单例子,题目如下

package test;

public class Student {

private String name;

private String id;

private String clazz;

private int age;

private String address;

/**

* sayHello方法

*/

public void sayHello() {

System.out.println("学号为" + this.id + "的同学的具体信息如下:");

System.out.println("姓名:" + this.name);

System.out.println("班级:" + this.clazz);

System.out.println("年龄:" + this.age);

System.out.println("家庭住址:" + this.address);

}

/**

* 测试方法

*

* @param args

*/

public static void main(String[] args) {

// 第一问

Student student = new Student();

student.setAddress("百度知道");

student.setAge(1);

student.setClazz("一班");

student.setId("071251000");

student.setName("lsy605604013");

student.sayHello();

// 第二问

Student studentNew = new Student();

studentNew.setAddress("搜搜知道");

studentNew.setAge(2);

studentNew.setClazz("二班");

studentNew.setId("071251001");

studentNew.setName("lady");

if (student.getAge() studentNew.getAge())

studentNew.sayHello();

else if (student.getAge() studentNew.getAge())

student.sayHello();

else

System.out.println("两人一样大");

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getClazz() {

return clazz;

}

public void setClazz(String clazz) {

this.clazz = clazz;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

}

跪地求好玩的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; } //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 { 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-- ){ //判断第一按钮同左侧空按钮之间有没按钮

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

k=0;

break;

}

else { k=2; } //K=2说明通过了第二次验证

}

}

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猜数字小游戏源代码

/*1、编写一个猜数字的游戏,由电脑随机产生一个100以内的整数,让用户去猜,如果用户猜的比电脑大,则输出“大了,再小点!”,反之则输出“小了,再大点!”,用户总共只能猜十次,并根据用户正确猜出答案所用的次数输出相应的信息,如:只用一次就猜对,输出“你是个天才!”,八次才猜对,输出“笨死了!”,如果十次还没有猜对,则游戏结束!*/

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("请输入一个100以内的整数:");

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("输入的数大了,输小点的!");

else if (shunum)

System.out.println("输入的数小了,输大点的!");

else {

System.out.println("恭喜你,猜对了!");

if (i=2)

System.out.println("你真是个天才!");

else if (i=6)

System.out.println("还将就,你过关了!");

else if (i=8)

System.out.println("但是你还……真笨!");

else

System.out.println("你和猪没有两样了!");

break;}

}

}

}

Java实验,代码怎么写?

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("圆的周长为:" + cir1.perimeter());

  Cylinder cy1 = new Cylinder(10, 15);

  System.out.println("圆柱体的表面积为:" + cy1.area());

  System.out.println("圆柱体的体积为:" + cy1.volume());

}

}

上面是我写的代码,下图是执行结果,麻烦看一下,是否可以。


分享名称:java优秀代码样例 java爬虫代码示例
当前URL:http://cdkjz.cn/article/dosidjj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220