我怎样才能在java中编写代码 [英] How can I code this in java

查看:93
本文介绍了我怎样才能在java中编写代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我们有一个存储粒子的盒子。最初,我们在该框中随机放置3个粒子。在每个步骤之后,这些颗粒将在盒子内自由移动。如果两个粒子碰撞,一个新的粒子将随机放在盒子里。

我们想模拟n步的运动粒子并计算粒子的数量。盒子。

实现一盒粒子(写一个盒子类,一个粒子类),这样就可以了。

1.一个盒子有一个固定的大小:固定的宽度和高度

2.每个粒子的位置(x,y),其中0≤x≤盒子的宽度,0≤y≤盒子的高度



3.粒子可以在下面的一个方向上移动但不能移出框(20pts)。

+ North(将y减少1),

+东北(将y减少1并将x增加1),

+ East(将x增加1),

+东南(将其y增加1并将其x增加1)

+ South(将y增加1),

+西南(增加其y)乘以1并将其x减1),

+ West(将x减少x 1),

+西北(将其y减1并将x减小1)

提示:声明方向的枚举类型

4.如果两个粒子发生碰撞,一个新的粒子将被随机放入盒子里。

和一个模拟课程,每个步骤,

5.它使所有盒子里的颗粒移动

6.它显示了盒子里的颗粒数量

7.它显示了里面带有颗粒的盒子*

-------------------------------

| * * * |

| * * * |

| * |

-------------------------------

one粒子=一颗星

8.搜索单身模式并将盒子作为单身人物



我尝试过:



我用java编写代码但是我刚刚创建了这个框,不知道怎么做休息。救命。这是我的代码:

公共类框

{

private int width;

private int height; < br $>


公共框(int h,int w){

height = h;

width = w;



if(height< 0)

{

System.out.println(无效的高度。设置为1);

height = 1;



}

if(width< 0)

{

System.out.println(无效宽度。设置为1);

宽度= 1;

}



}

public void drawline(int width,String s)

{

for(int i = 0; i< width; i ++)

{

System.out.print(s);

}

}

public void drawhollowline(int width)

{

System.out.print(| );

drawline(宽度 - 2,);

System.out.print(|);

}

public void drawbox(int height,in t宽度)

{

drawline(宽度, - );

System.out.println();

for(int i = 0; i< height; i ++)

{

drawhollowline(width);

System.out。 println();

}

drawline(宽度, - );

}



}

Imagine that we have a box to store particles. Initially, we place randomly 3 particles in that box. After each step, these particles will move freely inside the box. If two particles collide, a new particle will be
placed randomly in the box.
We want to simulate the movement particles for n steps and count the number of particles in the box.
Implement a box of particles (write a class of box, a class for the particle) in such a way that
1. A box has a fixed size: fixed width and height
2. Each particle has a position (x, y) where 0 ≤ x ≤ width of the box, and 0 ≤ y ≤ height of the box

3. A particle can move in one of the directions below but cannot move out of the box (20pts).
+ North (decreasing its y by 1),
+ North East (decreasing its y by 1 and increasing its x by 1),
+ East (increasing its x by 1),
+ South East (increasing its y by 1 and increasing its x by 1)
+ South (increasing its y by 1),
+ South West (increasing its y by 1 and decreasing its x by 1),
+ West (decreasing its x by 1),
+ North West (decreasing its y by 1 and decreasing its x by 1)
Hint: declare an enum type for Direction
4. If two particles collide, a new particle will be placed randomly in the box
and a class for the simulation where for each step,
5. It makes all particles in the box move
6. It shows the number of particles in the box
7. It visualizes the box with particles inside *
-------------------------------
| * * * |
| * * * |
| * |
-------------------------------
one particle = one star
8. Search about singleton pattern and make the box as a singleton

What I have tried:

I write a code in java but I just created the box and don't know how to do the rests. Help. Here is my code:
public class box
{
private int width;
private int height;

public box(int h, int w) {
height = h;
width = w;

if(height<0)
{
System.out.println("Invalid heigth. Setting to 1");
height=1;

}
if(width<0)
{
System.out.println("Invalid width. Setting to 1");
width=1;
}

}
public void drawline(int width, String s)
{
for (int i=0; i<width;i++)
{
System.out.print(s);
}
}
public void drawhollowline (int width)
{
System.out.print("|");
drawline(width - 2, " ");
System.out.print("|");
}
public void drawbox(int height,int width)
{
drawline(width,"-");
System.out.println();
for (int i=0;i<height;i++)
{
drawhollowline(width);
System.out.println();
}
drawline(width,"-");
}

}

推荐答案

Quote:

if(height< 0 )

{

System.out.println(无效的高度。设置为1);

height = 1;



}

if(width< 0)< br $>
{

System.out.println(无效宽度。设置为1);

width = 1;

}

if(height<0)
{
System.out.println("Invalid heigth. Setting to 1");
height=1;

}
if(width<0)
{
System.out.println("Invalid width. Setting to 1");
width=1;
}



我想你应该使用更好的约束(例如至少20)。

你应该设计一个粒子具有当前位置(x,y)的类和当前方向(建议的枚举)作为状态变量。

然后你有移动粒子,处理粒子之间的碰撞(简单,只是确保粒子具有相同的当前位置)和粒子与盒子壁之间的碰撞(要求没有说明,我认为你可以安全地假设通常的弹性行为,例如粒子撞击垂直墙上的x方向反射。)


I guess you should use better constraints (e.g at least 20).
Your should design a Particle class with current position (x,y) and current direction (the suggested enum) as state variables.
Then you have to move particles, handle collisions between particles (simple, just make sure the particles have the same current position) and collisions between particles and the walls of the box (the requirements says nothing about, I think you may safely assuming usual elastic behaviour, e.g. reflection of x direction on particle hit against a vertical wall).


这篇关于我怎样才能在java中编写代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆