我试图在1到12之间做一个随机。但结果已经是负面的,这让我停止了应用程序。
这是我的代码:
public class PlacementPerso extends AireDeJeu{
private boolean trou1, trou2, trou3, trou4, trou5, trou6, trou7, trou8, trou9, trou10, trou11, trou12;
private Random r = new Random();
private int random, baseMax, baseMin;
public void PlacementPerso(){
trou1 = true; trou2 = true; trou3 = true; trou4 = true; trou5 = true; trou6 = true; trou7 = true; trou8 = true; trou9 = true; trou10 = true; trou11 = true; trou12 = true;
random = 1; baseMax = 1; baseMin = 12;
}
public void aPlacer(Perso pPerso){
final int ressource = pPerso.getRessource();
//This is the crash line
random = r.nextInt((baseMax - baseMin) + 1) + baseMin;
/**
*
* AND MORE ....
*
*/
来电者:
public class GenerateurPartie extends GenerateurPerso{
private int mNiveau;
public static int mAvancement = 0;
private int mDelai;
private Thread mThread;
private Perso mPerso;
private final HandlerDelai handlerDelai = new HandlerDelai();
protected static PlacementPerso mPlacementPerso = new PlacementPerso();
public GenerateurPartie(int pNiveau) {
this.mNiveau = pNiveau;
this.mDelai = 2500;
mPlacementPerso.PlacementPerso();
mThread = new Thread(new Runnable() {
@Override
public void run() {
try {
int i = 0;
while (mAvancement < 100) {
i += 1;
if (mAvancement >= 80)
mDelai = mDelai / 2;
Thread.sleep(mDelai);
handlerDelai.postDelayed(new Runnable() {
@Override
public void run() {
mPerso = GenerateurPerso(mNiveau, mAvancement);
mPlacementPerso.aPlacer(mPerso);
}
}, mDelai);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}});
mThread.start();
}
}
我已经阅读了stackOverFlow上的一些线程并尝试了他们的建议,但没有解决我的问题。 我试着离开“random = 1;”在我的班级“PlacementPerso”中,但方式已经相同。
这是我的日志:
10-07 10:42:17.885 490-490/com.dunomade.ecraseurdetaupe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dunomade.ecraseurdetaupe, PID: 490
java.lang.IllegalArgumentException: n <= 0: -10
at java.util.Random.nextInt(Random.java:182)
at com.dunomade.ecraseurdetaupe.PlacementPerso.aPlacer(PlacementPerso.java:27)
at com.dunomade.ecraseurdetaupe.GenerateurPartie$1$1.run(GenerateurPartie.java:56)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
也许有人知道如何解决它。我不明白为什么我的结果已经&lt; = 0。 谢谢先进=)
答案 0 :(得分:1)
正如@ cherry-wave所说“你翻了最大值和最小值”。将来最好通过Thorwing例外处理这个错误,或者如果你不知道哪个值是什么,你也可以做这样的事情:
//your values in class
int baseMax = 1;
int baseMin = 12;
//when calling method to do math, first find the right min and max value
int fixedMax = Math.max(baseMax, baseMin);
int fixedMin = Math.min(baseMax, baseMin);
//your further coce
Random r = new Random();
int random = r.nextInt((fixedMax - fixedMin) + 1) + fixedMin;
或通过例外处理您的操作,即:
//checking if all is OK, because it MUST BE OK
if (baseMax < baseMin) {
throw new UnsupportedOperationException("hey first value is smaller than second");
}
random = r.nextInt((baseMax - baseMin) + 1) + baseMin;
答案 1 :(得分:0)
只是你误认为baseMin和baseMax的位置。我们应该有baseMax&gt; baseMin。通过交换它们,您的问题将得到解决。 这是baseMax = 12,baseMin = 1;