下面是我寫的代碼,我想添加每個鼠標和鼠標解決的總數,但它顯示的數量不正確。如何添加For循環的結果 - Java
鼠標1解決多少個迷宮? 4
鼠標2解決了多少個迷宮? 0
鼠標3解決多少個迷宮? 1
鼠標4解決多少迷宮? 4
大鼠1解決了多少個迷宮? 2
大鼠2解決了多少個迷宮? 3
大鼠3解決了多少個迷宮? 0
大鼠4解決了多少個迷宮? 4
老鼠解決了8。而老鼠解決8
import java.util.Random;
public class ratloop {
public static void main(String[] args) {
mazeRun();
System.exit(0);
}
public static void mazeRun()
{
Random randomGenerator = new Random();
int mouseTest;
int mouseSolved = 1;
for (mouseTest= 1; mouseTest <=4; mouseTest++)
{
mouseSolved = randomGenerator.nextInt(5);
System.out.println("How many mazes did mouse " + mouseTest + " solve? " + mouseSolved);
mouseSolved = mouseSolved + mouseTest;
}
int ratTest;
int ratSolved = 1;
for (ratTest= 1; ratTest <=4; ratTest++)
{
ratSolved = randomGenerator.nextInt(5);
System.out.println("How many mazes did rat " + ratTest + " solve? " + ratSolved);
ratSolved = ratSolved + ratTest;
}
System.out.println("The mouse solved " + mouseSolved + " . And the rats solved " + ratSolved);
}
}
我明白我現在做錯了什麼。該計劃顯示正確的總數。謝謝! – AbbenGabben