小JAVA大世界之程式建模跳蚤實驗

來源:互聯網
上載者:User

標籤:nal   isp   應該   public   move   自己的   判斷   package   cti   

package com.chigoe;
//房子類
class House {
private int m;// 儲存行數
private int n;// 儲存列數
private int[][] a;

public House() { // 無參構造方法
m = 10;
n = 10;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
a[i][j] = 0;
}
}
public House(int m,int n){//帶參構造方法
this.m=n;this.n=n;
a=new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
a[i][j] = 0;
}
}
public int getM() {
return m;
}
public int getN() {
return n;
}
public int[][] getA() {
return a;
}
public int getElment(int i,int j){
return a[i][j];
}
public void setElment(int i,int j,int v){
a[i][j]=v;
}
//判斷跳蚤是否經過了所以瓷磚
public boolean checkZero(){
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
if(a[i][j]==0){
return true;
}
}
return false;
}
//遍曆數組
public void display(){
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
System.out.print(" "+a[i][j]+" ");
}
System.out.println();//滿足一行瓷磚就換行
}
}

}

//跳蚤類
public class Tiaozao {

private static final int UP=0;
private static final int DOWN=1;
private static final int RIGHT=2;
private static final int LEFT=3;
private int x,y;
private int totals;//儲存總的次數
private House ahouse;//表示此類的成員變數是自己本身,也就是自己的一個對象。
public Tiaozao(House h){
ahouse=h;
totals=0;
x=(int)(Math.random()*ahouse.getM());//應該是隨機產生跳蚤的初始位置
y=(int)(Math.random()*ahouse.getN());

}
public int getTotals(){
return totals;
}
public boolean walk(int direction){
System.out.println("x="+x+",y="+y+"direction="+direction);
switch(direction){
case UP:if(y==0) return false;
else{
ahouse.setElment(x, y, ahouse.getElment(x, y)+1);
y=y-1;
return true;
}
case DOWN:if(y==ahouse.getN()-1) return false;
else{
ahouse.setElment(x, y, ahouse.getElment(x, y)+1);
y=y+1;
return true;
}
case LEFT:if(x==0) return false;
else{
ahouse.setElment(x, y, ahouse.getElment(x, y)+1);
x=x-1;
return true;
}
case RIGHT:if(x==ahouse.getM()-1) return false;
else{
ahouse.setElment(x, y, ahouse.getElment(x, y)+1);
x=x+1;
return true;
}

default:
System.out.println("非法移動");
return true;
}
}
public void move(){
int nexdirection;
boolean success;
do{
nexdirection=(int) (Math.random()*4);
success=walk(nexdirection);
if(success){
totals++;
}
}while(ahouse.checkZero());
}
public static void main(String[] args) {

House ahouse=new House(4,4);
Tiaozao atiaozao=new Tiaozao(ahouse);//將對象作為參數,引用資料傳遞
atiaozao.move();
ahouse.display();
System.out.println("totals ="+atiaozao.getTotals());

}

}

小JAVA大世界之程式建模跳蚤實驗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.