如何在java中切换类 [英] How to switch the class in java
问题描述
Hello编码器,我想将switch语句重构为枚举类。代码的结果:创建一个盒子并将粒子放入盒子内,然后让粒子自由移动,如果两个粒子碰撞,新的粒子将被添加到盒子内。帮助我们一些提示。还有一个,我知道当我在这里粘贴我的代码时(我问我的团队他们同意了),它会被另一个人轻易复制。我怎么能防止这种情况?谢谢。
我尝试过:
盒子类:
import java.util.Random;
公共类框{
private final int WIDTH = 25;
private final int HEIGHT = 6;
private final int PARTICLE_MAX = 300;
私人静电箱;
private particle [] particleList = new particle [PARTICLE_MAX];
private Random r = new Random();
private box(){
for(int i = 0; i< 3; i ++){
addNewParticle();
}
}
public int getWidth(){
返回WIDTH;
}
public int getHeight(){
return HEIGHT;
}
public int getParticleMax(){
return PARTICLE_MAX;
}
公共静态框getInstance(){
if(box == null)
box = new box();
返回框;
}
public void makeParticlesMove(){
int n;
for(particle particle:particleList){
if(particle!= null){
n = r.nextInt(8)+1;
switch(n){
case 1:if(particle.getY() - 1> 0){
particle.north();
}
休息;
case 2:if(particle.getY() - 1> 0&& particle.getX()+ 1< WIDTH-1){
particle.northeast();
}
休息;
case 3:if(particle.getX()+ 1< WIDTH-1){
particle.east();
}
休息;
case 4:if(particle.getY()+ 1< HEIGHT-1&& particle.getX()+ 1< WIDTH-1){
particle.southeast();
}
休息;
case 5:if(particle.getY()+ 1< HEIGHT-1){
particle.south();
}
休息;
case 6:if(particle.getY()+ 1< HEIGHT-1&& particle.getX() - 1> 0){
particle.southwest();
}
休息;
case 7:if(particle.getX() - 1> 0){
particle.west();
}
休息;
case 8:if(particle.getY() - 1> 0&& particle.getX() - 1> 0){
particle.northwest();
}
休息;
}
}
}
collisionCheck();
}
public int particleCount(){
int p = 0;
for(particle particle:particleList){
if(particle!= null){
p ++;
}
}
返回p;
}
public void showParticles(){
char [] [] map = new char [WIDTH] [HEIGHT];
for(particle p:particleList){
if(p!= null){
int x = p.getX();
int y = p.getY();
map [x] [y] ='*';
}
}
for(int i = 0; i< WIDTH; i ++)
System.out.print( - );
System.out.println();
for(int j = 1; j< HEIGHT-1; j ++){
System.out.print(|);
for(int c = 1; c< WIDTH-1; c ++){
if(map [c] [j] =='*')
System.out.print( *);
else
System.out.print();
}
System.out.println(|);
}
for(int i = 0; i< WIDTH; i ++)
System.out.print( - );
System.out.println();
}
public void collisionCheck(){
boolean [] [] map = new boolean [WIDTH] [HEIGHT];
for(int i = 0; i< HEIGHT; i ++){
for(int j = 0; j< WIDTH; j ++)
map [j] [i] = false;
}
for(粒子p:particleList){
if(p!= null){
int x = p.getX();
int y = p.getY();
if(map [x] [y]){
System.out.println(Collided !!!!);
addNewParticle();
}
其他
map [x] [y] = true;
}
}
}
public void addNewParticle(){
for(int i = 0; i< PARTICLE_MAX; i ++){
if(particleList [i] == null){
particleList [i] = new particle(r.nextInt(WIDTH-2)+ 1,r.nextInt(HEIGHT-2)+1);
休息;
}
}
}
}
粒子类:
public class Particle {
private int x;
private int y;
public Particle(int x,int y){
this.x = x;
this.y = y;
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public void north(){
y- = 1;
}
public void northeast(){
y- = 1;
x + = 1;
}
public void east(){
x + = 1;
}
public void southeast(){
y + = 1;
x + = 1;
}
public void south(){
y + = 1;
}
public void southwest(){
y + = 1;
x- = 1;
}
public void west(){
x- = 1;
}
public void northwest(){
y- = 1;
x- = 1;
}
}
测试:
公共类主要{
public static void main(String [] args){
Box b = Box.getInstance();
do {
System.out.println(粒子数:+ b.particleCount());
b.makeParticlesMove();
b.showParticles();
try {
Thread.sleep(300);
} catch(例外e){
System.out.println(e);
}
} while(b.particleCount()< b.getParticleMax());
}
}
您可以定义枚举类似于:
enum 方向{
无效( - 1 ),
北( 0 ),
NorthEast( 1 ),
南( 2 ),
...
...
SouthEast( 7 跨度>);
private int val;
private static Map< int,Direction> intToTypeMap;
private Direction( int v){
此 .val = v;
this .addToMap(v);
}
public int getValue(){
return this .val;
}
private void addToMap( int v){
if (intToTypeMap == null){
intToTypeMap = new TreeMap< int,Direction>();
}
intToTypeMap.put(v, this );
}
public static 方向fromInt( int v){
Direction type = intToTypeMap.get(Integer.valueOf(v));
if (type == null){
return Direction.Invalid;
}
返回类型;
}
}
在上面的枚举之后,你可以改变你的开关语句,如下所示:
。 。 。
。 。 。
for(粒子:particleList){
if(particle!= null){
switch(Direction.fromtInt(r.nextInt(8))){
case North: if(particle.getY() - 1> 0){
particle.north();
}
休息;
。 。 。
。 。
Hello coders, I want to refactor the switch statement to a enum class. The result of the code: creates a box and put particles inside the box, then make particles move freely and if two particles collide a new particle will be added inside the box. Help us some hints to do. One more, I knew when I pasted my code in here (I asked my team and they agreed), it will be copied easily by another person. How can I prevent that? Thanks.
What I have tried:
Box class:
import java.util.Random; public class box{ private final int WIDTH = 25; private final int HEIGHT = 6; private final int PARTICLE_MAX = 300; private static box box; private particle[] particleList = new particle[PARTICLE_MAX]; private Random r = new Random(); private box(){ for(int i = 0; i<3;i++){ addNewParticle(); } } public int getWidth(){ return WIDTH; } public int getHeight(){ return HEIGHT; } public int getParticleMax(){ return PARTICLE_MAX; } public static box getInstance(){ if(box == null) box = new box(); return box; } public void makeParticlesMove(){ int n; for(particle particle:particleList){ if(particle != null){ n = r.nextInt(8)+1; switch(n){ case 1: if(particle.getY()-1 > 0){ particle.north(); } break; case 2: if(particle.getY()-1 > 0 && particle.getX()+1 < WIDTH-1){ particle.northeast(); } break; case 3: if(particle.getX()+1 < WIDTH-1){ particle.east(); } break; case 4: if(particle.getY()+1 < HEIGHT-1 && particle.getX()+1 < WIDTH-1){ particle.southeast(); } break; case 5: if(particle.getY()+1 < HEIGHT-1){ particle.south(); } break; case 6: if(particle.getY()+1 < HEIGHT-1 && particle.getX()-1 > 0){ particle.southwest(); } break; case 7: if(particle.getX()-1 > 0){ particle.west(); } break; case 8: if(particle.getY()-1 > 0 && particle.getX()-1 > 0){ particle.northwest(); } break; } } } collisionCheck(); } public int particleCount(){ int p = 0; for(particle particle:particleList){ if(particle !=null){ p++; } } return p; } public void showParticles(){ char[][] map = new char[WIDTH][HEIGHT]; for(particle p:particleList){ if(p != null){ int x = p.getX(); int y = p.getY(); map[x][y] = '*'; } } for(int i = 0; i < WIDTH; i++) System.out.print("-"); System.out.println(); for(int j = 1; j < HEIGHT-1; j++){ System.out.print("|"); for(int c = 1; c < WIDTH-1; c++){ if(map[c][j] == '*') System.out.print("*"); else System.out.print(" "); } System.out.println("|"); } for(int i = 0; i < WIDTH; i++) System.out.print("-"); System.out.println(); } public void collisionCheck(){ boolean[][] map = new boolean[WIDTH][HEIGHT]; for(int i =0; i<HEIGHT; i++){ for(int j = 0; j<WIDTH; j++) map[j][i] = false; } for(particle p:particleList){ if(p != null){ int x = p.getX(); int y = p.getY(); if(map[x][y]) { System.out.println("Collided!!!!"); addNewParticle(); } else map[x][y] = true; } } } public void addNewParticle(){ for(int i = 0; i < PARTICLE_MAX; i++){ if(particleList[i] == null){ particleList[i] = new particle(r.nextInt(WIDTH-2)+1, r.nextInt(HEIGHT-2)+1); break; } } } }
particle class:
public class Particle{ private int x; private int y; public Particle(int x, int y){ this.x = x; this.y = y; } public void setX(int x){ this.x = x; } public void setY(int y){ this.y = y; } public int getX(){ return x; } public int getY(){ return y; } public void north(){ y-=1; } public void northeast(){ y-=1; x+=1; } public void east(){ x+=1; } public void southeast(){ y+=1; x+=1; } public void south(){ y+=1; } public void southwest(){ y+=1; x-=1; } public void west(){ x-=1; } public void northwest(){ y-=1; x-=1; } }
test:
public class Main{ public static void main(String[] args){ Box b = Box.getInstance(); do{ System.out.println("Particle count: " + b.particleCount()); b.makeParticlesMove(); b.showParticles(); try{ Thread.sleep(300); }catch (Exception e){ System.out.println(e); } }while(b.particleCount() < b.getParticleMax()); } }
You can define an enum something like:
enum Direction { Invalid(-1), North(0), NorthEast(1), South(2), ... ... SouthEast(7); private int val; private static Map<int, Direction> intToTypeMap; private Direction(int v) { this.val = v; this.addToMap(v); } public int getValue() { return this.val; } private void addToMap(int v) { if (intToTypeMap == null) { intToTypeMap = new TreeMap<int, Direction>(); } intToTypeMap.put(v, this); } public static Direction fromInt(int v) { Direction type = intToTypeMap.get(Integer.valueOf(v)); if (type == null) { return Direction.Invalid; } return type; } }
After above enum, you can change your switch statement something like as below:
. . . . . . for (particle particle:particleList) { if(particle != null){ switch(Direction.fromtInt(r.nextInt(8))){ case North: if(particle.getY()-1 > 0){ particle.north(); } break; . . . . . .
这篇关于如何在java中切换类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!