我有一个计数器,我希望计算在1到300之间。我使用一个最终变量,我从另一个类的计数器中获取,然后打破该数字并在屏幕上显示正确的数字(以三张图片 - 一张,几张,几百张)。但是,我很难让它正确计算。每次用户点击某个区域时,我都希望计数器相应地更新。以下是我正在使用的两个类:
public class Counter {
int count = 0;
public int IncreaseOnes() {
if (count >= 0 && count < 300){
count = count++;
}
else {
count = 0;
}
return count;
}
public int DecreaseOnes() {
if (count >= 0 && count < 300){
count = count--;
}
else {
count = 0;
}
return count;
}
public int IncreaseTens() {
if (count >= 0 && count < 300){
count = count + 10;
}
else {
count = 0;
}
return count;
}
public int DecreaseTens() {
if (count >= 0 && count < 300){
count = count - 10;
}
else {
count = 0;
}
return count;
}
public int IncreaseHundreds() {
if (count >= 0 && count < 300){
count = count + 100;
}
else {
count = 0;
}
return count;
}
public int DecreaseHundreds() {
if (count >= 0 && count < 300){
count = count - 100;
}
else {
count = 0;
}
return count;
}
public int getFinalCount() {
System.out.println(count);
return count;
}
}
然后
public class TempoScreen extends Screen {
public TempoScreen(Game game) {
super(game);
}
Counter counter = new Counter();
private final int number_y = 240;
private final int number_hundreds_x = 75;
private final int number_tens_x = 195;
private final int number_ones_x = 308;
@Override
public void update(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_UP) {
if (inBounds(event, 75,138,145,205)) {
//Increase Tempo
//Upper Button Left
counter.IncreaseHundreds();
}
} if (inBounds (event, 201,141,269,205)) {
//Increase Tempo
//Upper Button Middle
counter.IncreaseTens();
} if (inBounds (event, 318,143,385,203)) {
//Increase Tempo
//Upper Button Right
counter.IncreaseOnes();
} if (inBounds (event, 75,435,142,498)) {
//Decrease Tempo
//Lower Button Left
counter.DecreaseHundreds();
} if (inBounds (event, 199,437,268,499)) {
//Decrease Tempo
//Lower Button Middle
counter.DecreaseTens();
} if (inBounds (event, 319,435,382,497)) {
//Decrease Tempo
//Lower Button Right
counter.DecreaseOnes();
} if (inBounds(event, 4,662,143,794)) {
//GO BACK TO MAINMENU
game.setScreen(new MainMenuScreen (game));
// TO SAVE WHICH ONE IT USE USE FILE IO CLASSES
} if (inBounds (event, 341,665,477,794)) {
//GO TO GAME
game.setScreen(new GameScreen (game));
}
}
}
private boolean inBounds(TouchEvent event, int x, int y, int width,
int height) {
if (event.x > x && event.x < x + width - 1 && event.y > y
&& event.y < y + height - 1)
return true;
else
return false;
}
@Override
public void paint(float deltaTime) {
Graphics g = game.getGraphics();
int finalCount = counter.getFinalCount();
g.drawImage(Assets.tempo, 0, 0);
g.drawImage(Assets.zero, number_hundreds_x, number_y); //draw 0 hundreds
g.drawImage(Assets.zero, number_tens_x, number_y); //draw 0 tens
g.drawImage(Assets.zero, number_ones_x, number_y); //draw 0 ones
int ones = finalCount % 10;
int tens = finalCount / 10 % 10;
int hundreds = finalCount / 100 % 10;
if (ones == 2) {
g.drawImage(Assets.two, number_ones_x, number_y);
}
if (ones == 3) {
g.drawImage(Assets.three, number_ones_x, number_y);
}
if (ones == 4) {
g.drawImage(Assets.four, number_ones_x, number_y);
}
if (ones == 5) {
g.drawImage(Assets.five, number_ones_x, number_y);
}
if (ones == 6) {
g.drawImage(Assets.six, number_ones_x, number_y);
}
if (ones == 7) {
g.drawImage(Assets.seven, number_ones_x, number_y);
}
if (ones == 8) {
g.drawImage(Assets.eight, number_ones_x, number_y);
}
if (ones == 9) {
g.drawImage(Assets.nine, number_ones_x, number_y);
}
if (tens == 1 && ones == 0) {
g.drawImage(Assets.one, number_tens_x, number_y);
g.drawImage(Assets.zero, number_ones_x, number_y);
}
if (tens == 2 && ones == 0) {
g.drawImage(Assets.two, number_tens_x, number_y);
g.drawImage(Assets.zero, number_ones_x, number_y);
}
if (tens == 3 && ones == 0) {
g.drawImage(Assets.three, number_tens_x, number_y);
g.drawImage(Assets.zero, number_ones_x, number_y);
}
if (tens == 4 && ones == 0) {
g.drawImage(Assets.four, number_tens_x, number_y);
g.drawImage(Assets.zero, number_ones_x, number_y);
}
if (tens == 5 && ones == 0) {
g.drawImage(Assets.five, number_tens_x, number_y);
g.drawImage(Assets.zero, number_ones_x, number_y);
}
if (tens == 6 && ones == 0) {
g.drawImage(Assets.six, number_tens_x, number_y);
g.drawImage(Assets.zero, number_ones_x, number_y);
}
if (tens == 7 && ones == 0) {
g.drawImage(Assets.seven, number_tens_x, number_y);
g.drawImage(Assets.zero, number_ones_x, number_y);
}
if (tens == 8 && ones == 0) {
g.drawImage(Assets.eight, number_tens_x, number_y);
g.drawImage(Assets.zero, number_ones_x, number_y);
}
if (tens == 9 && ones == 0) {
g.drawImage(Assets.nine, number_tens_x, number_y);
g.drawImage(Assets.zero, number_ones_x, number_y);
}
if (tens == 1) {
g.drawImage(Assets.one, number_tens_x, number_y);
}
if (tens == 2) {
g.drawImage(Assets.two, number_tens_x, number_y);
}
if (tens == 3) {
g.drawImage(Assets.three, number_tens_x, number_y);
}
if (tens == 4) {
g.drawImage(Assets.four, number_tens_x, number_y);
}
if (tens == 5) {
g.drawImage(Assets.five, number_tens_x, number_y);
}
if (tens == 6) {
g.drawImage(Assets.six, number_tens_x, number_y);
}
if (tens == 7) {
g.drawImage(Assets.seven, number_tens_x, number_y);
}
if (tens == 8) {
g.drawImage(Assets.eight, number_tens_x, number_y);
}
if (tens == 9) {
g.drawImage(Assets.nine, number_tens_x, number_y);
}
if (hundreds == 1) {
g.drawImage(Assets.one, number_hundreds_x, number_y);
}
if (hundreds == 2) {
g.drawImage(Assets.two, number_hundreds_x, number_y);
}
if (hundreds == 3) {
g.drawImage(Assets.three, number_hundreds_x, number_y);
}
}
那么如何让我的计数器正确计数?而且,有没有更好的方法来格式化我的代码,所以我不会使用尽可能多的if语句?谢谢!
答案 0 :(得分:3)
更改
public int IncreaseOnes() {
...
count = count++;
...
}
public int DecreaseOnes() {
...
count = count--;
...
}
到
public int IncreaseOnes() {
...
count++;
...
}
public int DecreaseOnes() {
...
count--;
...
}
检查this,了解 post-pre 增量&amp; amp;减少运营商。
答案 1 :(得分:2)
尝试以下代码。我试图减少那里的代码量。
您还需要线程安全的Counter类。
专柜课:
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
public class Counter {
private static final int MAX_COUNT = 299;
/*Just to be safe*/
private AtomicInteger counter = new AtomicInteger();
private ReentrantLock stepLock = new ReentrantLock();
public void step(int stepValue) {
stepLock.lock();
try {
int steppedValue = counter.get() + stepValue;
if(steppedValue < 0 || steppedValue > MAX_COUNT) {
/*reset counter*/
counter.set(0);
} else {
counter.set(steppedValue);
}
} finally {
stepLock.unlock();
}
}
public int getFinalCount() {
System.out.println(counter.get());
return counter.get();
}
}
TempoScreen类:
public class TempoScreen extends Screen {
Dimension[][] updownDims = new Dimension[2][3];
Dimension mainMenuDim;
Dimension gameMenuDim;
//I wasn't sure what the asset types were, replace Object[] wity assetType[]
Object[] assets = [Assets.zero, Azzets.one,Azzets.two,Azzets.three,Azzets.four,Azzets.five,Azzets.six,Azzets.seven,Azzets.eight,Azzets.nine];
Counter counter = new Counter();
private final int number_y = 240;
private final int number_hundreds_x = 75;
private final int number_tens_x = 195;
private final int number_ones_x = 308;
private final int[] number_xs = {number_ones_x, number_tens_x, number_hundreds_x};
public TempoScreen(Game game) {
super(game);
//up buttons
updownDims[0][0] = new Dimension(318,143,385,203);//+1
updownDims[0][1] = new Dimension(201,141,269,205);//+10
updownDims[0][2] = new Dimension(75,138,145,205);//+100
//down buttons
updownDims[1][0] = new Dimension(319,435,382,497);//-1
updownDims[1][1] = new Dimension(199,437,268,499);//-10
updownDims[1][2] = new Dimension(75,435,142,498);//-100
mainMenuDim = new Dimension(4,662,143,794);
gameMenuDim = new Dimension(341,665,477,794);
}
public class Dimension {
private int x;
private int y;
private int width;
private int height;
public Dimension(int x, int y, int width, int height) {
super();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public boolean contains (int x, int y) {
boolean isInside = false;
if (x > this.x && x < (this.x + width - 1) && y > this.y
&& y < (this.y + height - 1)) {
isInside = true;
}
return isInside;
}
}
@Override
public void update(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
int len = touchEvents.size();
for (TouchEvent event : touchEvents) {
if (event.type != TouchEvent.TOUCH_UP) {
continue;
}
int x = event.x;
int y = event.y;
boolean inUpdowndims = false;
for(int row = 0; row < updownDims.length; row++) {
for(int col = 0; col < updownDims[0].length; col++) {
if(inUpdowndims = updownDims[row][col].contains(x, y)) {
int stepValue = (int)Math.pow(10, col);
counter.step(row == 0 ? stepValue : -stepValue);
}
}
}
if(!inUpdowndims) {
if(mainMenuDim.contains(x, y) {
game.setScreen(new MainMenuScreen(game))
} else if(gameMenuDim.contains(x, y)) {
game.setScreen(new GameScreen(game));
}
}
}
}
@Override
public void paint(float deltaTime) {
Graphics g = game.getGraphics();
int finalCount = counter.getFinalCount();
g.drawImage(Assets.tempo, 0, 0);
//one loop is enough for all the images
for(int xi = 0; xi < number_xs.length; xi++) {
int num = finalCount % 10;
finalCount /= 10;
g.drawImage(assets[num], number_xs[xi], number_y);
}
}
}
答案 2 :(得分:0)
使类计数器的每个方法同步如
public syncronized int IncreaseOnes(){
}
答案 3 :(得分:0)
public class Counter {
private int count = 0;
public int increaseOnes(){
return increase(1);
}
public int decreaseOnes(){
return increase(-1);
}
public int increaseTens(){
return increase(10);
}
public int decreaseTens(){
return increase(-10);
}
public int increaseHundreds(){
return increase(100);
}
public int decreaseHundreds(){
increase(-10);
}
private int increase(int delta){
count+=delta;
if (count < 0 || count > 300){
count = 0;
}
return count;
}
}
对于paint方法,请考虑使用开关结构。
switch (ones){
case 1:
g.drawImage(Assets.one, number_ones_x, number_y);
break;
case 2:
g.drawImage(Assets.two, number_ones_x, number_y);
break;
...
default: //Is 0
g.drawImage(Assets.two, number_ones_x, number_y);
//Invoke an internal Method doing the same but for tens
}