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大世界之程序建模跳蚤实验的更多相关文章

  1. 小JAVA大世界之万年历

    import java.util.Scanner; public class Calendar { public static void main(String[] args) { // 万年历 in ...

  2. 20145223《Java程序程序设计》实验报告5

    20145223杨梦云<Java网络编程> 一.实验内容 ·1.运行下载的TCP代码,结对进行,一人服务器,一人客户端: ·2.利用加解密代码包,编译运行代码,一人加密,一人解密: ·3. ...

  3. 20145227《Java程序设计》第2次实验报告

    20145227<Java程序设计>第2次实验报告 实验步骤与内容 一.实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 ...

  4. 20162330 实验一 《Java开发环境的熟悉》 实验报告

    2016-2017-2 实验报告目录: 1 2 3 4 5 20162330 实验一 <Java开发环境的熟悉> 实验报告 课程名称:<程序设计与数据结构> 学生班级:1623 ...

  5. 2018-2019-2 20175306实验二面向对象程序设计《Java开发环境的熟悉》实验报告

    2018-2019-2 20175306实验二面向对象程序设计<Java开发环境的熟悉>实验报告 面向对象程序设计-1 实验要求: 参考:> http://www.cnblogs.c ...

  6. 2018-2019-2 20175209 实验一《Java开发环境的熟悉》实验报告

    2018-2019-2 20175209 实验一<Java开发环境的熟悉>实验报告 一.实验内容及步骤 1.使用JDK编译.运行简单的Java程序 cd 20175209进入2017520 ...

  7. 2018-2019-2-20175303 实验一 《Java开发环境的熟悉》实验报告

    2018-2019-2-20175303 实验一 <Java开发环境的熟悉>实验报告 一.实验内容及步骤 实验1 1.用mkdir建立“20175303exp1”的目录 2.在“20175 ...

  8. 2018-2019-20175205实验二面向对象程序设计《Java开发环境的熟悉》实验报告

    2018-2019-20175205实验二面向对象程序设计<Java开发环境的熟悉>实验报告 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)>< ...

  9. Java技术开发程序员如果在2019年立足

    2019年的互联网环境相对以往来说要更复杂一些,互联网领域也正在经历从消费互联网向产业互联网转型的阵痛期.其实不少公司从2018年开始已经在陆续进行结构化调整,这些调整中的重要内容就是岗位调整,而岗位 ...

随机推荐

  1. VSS记住用户名和密码

    计算机-属性-高级系统设置-环境变量 新建两个环境变量如下:        SSUSER(VSS的用户名)        SSPWD(VSS的密码)

  2. --查询nvarchar(max)的表和字段

    --查询nvarchar(max)的表和字段 select 'insert into #tempTabelInfo select '''+d.name+''', '''+a.name+''', max ...

  3. ARM开发板上iconv调用失败的解决方法

    当前流行的字符编码格式有:US-ASCII.ISO-8859-1.UTF-8.UTF-16BE.UTF-16LE.UTF-16.GBK.GB2312等,其中GBK.GB2312是专门处理中文编码的.而 ...

  4. python 继承中的super

    python继承中子类访问父类的方法(包括__init__)主要有两种方法,一种是调用父类的未绑定方法,另一种是使用super(仅仅对于新式类),看下面的两个例子: #coding:utf-8 cla ...

  5. error C4996: 'fopen': This function or variable may be unsafe.

    vs2013中错误提示信息: error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s ...

  6. SonarQube-5.6.3 代码分析平台搭建使用

    python代码分析 官网主页: http://docs.sonarqube.org/display/PLUG/Python+Plugin Windows下安装使用: 快速使用: 1.下载jdk ht ...

  7. Numpy Python

    如果一个数组太长,则NumPy自动省略中间部分而只打印两端的数据: 可通过设置printoptions参数来禁用NumPy的这种行为并强制打印整个数组. set_printoptions(thresh ...

  8. GoogleNet tips

    Inception Module googlenet的Inception Module Idea 1: Use 1x1, 3x3, and 5x5 convolutions in parallel t ...

  9. 深入理解display属性

    display 属性在网页布局中很常见,但是之前一直不了解他的各个属性的区别,只是简单的使用block.none属性,对于其他属性都不清楚,今天详细的学习和实践display属性的各个方面 定义 di ...

  10. Oracle 数据库 重新启动

    进入server后 su - oracle lsnrctl stop sqlplus / as sysdba shutdown immediate; quit lsnrctl start sqlplu ...