我已经在调试器和测试中尽了最大努力,但我无法解决这个问题。
在此方法中,它跳过for循环。但是在roll()方法中,arraylist获取值?
public int lol(int a) {
num = 0;
for (int i = 0; i < objectDie.getDices().size(); i++) {
if (objectDie.getDices().get(i) == a) {
num += a;
}
}
return num;
}
我将发布所有课程以创建更好的代码图像,麻烦制造者位于我链接的最后一个课程的底部。
主要
package dicegame;
public class DiceGame {
public static void main(String[] args) {
Game test = new Game();
test.numberDices();
Player spil = new Human();
spil.takeTurn(test);
}
}
游戏
package dicegame;
import java.util.Scanner;
public class Game {
Die DieObjekt = new Die();
Scanner sc = new Scanner(System.in);
public void numberDices() {
System.out.println("How many dices would you like to play with?");
int numberOfDices = sc.nextInt();
DieObjekt.setNumber(numberOfDices);
}
public void play() {
DieObjekt.roll();
DieObjekt.printRoll();
}
}
模具
package dicegame;
import java.util.ArrayList;
import java.util.Random;
public class Die {
ArrayList<Integer> dices = new ArrayList<>();
Random ran = new Random();
int number;
public ArrayList<Integer> getDices() {
return dices;
}
public void setNumber(int number) {
this.number = number;
}
public void roll() {
for (int i = 0; i < number; i++) {
dices.add(ran.nextInt(6) + 1);
}
}
public void printRoll() {
System.out.println("You got ");
for (int i = 0; i < dices.size(); i++) {
System.out.print(dices.get(i) + ", ");
}
}
}
人类 - 此课程中的最后一个方法是麻烦制造者
package dicegame;
import java.util.Scanner;
public class Human implements Player {
int one, two, three, four, five, six, num;
Scanner sc = new Scanner(System.in);
Die objectDie = new Die();
Game object = new Game();
@Override
public void takeTurn(Game object) {
object.play();
System.out.println("If you want to stop, press enter, if not press"
+"the number on the die that you would like to save");
int valg = sc.nextInt();
switch (valg) {
case 1:
one = lol(1);
System.out.println("You got " + one + " points in the"
+"ones");
break;
case 2:
two = lol(2);
System.out.println("You got " + two + " points in the"
+"twos");
break;
case 3:
three = lol(3);
System.out.println("You got " + three + " points in the"
+"threes");
break;
case 4:
four = lol(4);
System.out.println("You got " + four + " points in the"
+"fours");
break;
case 5:
five = lol(5);
System.out.println("You got " + five + " points in the"
+"fives");
break;
case 6:
six = lol(6);
System.out.println("You got" + six + " points in the"
+"sixes");
break;
default:
System.out.println("You have stopped, and that is OK");
break;
}
}
public int lol(int a) {
num = 0;
for (int i = 0; i < objectDie.getDices().size(); i++) {
if (objectDie.getDices().get(i) == a) {
num += a;
}
}
return num;
}
}
答案 0 :(得分:3)
在课堂游戏中,您完成了创建骰子列表的过程(设置总数并使用roll添加值)=&gt;数组列表具有值
但是你忘了用人类做这件事
====================
修改强>
以下是您修复游戏的一些建议
/**
* Don't try to create dice and game in human class, you can take it from previous game which you were created in the static main
*/
//Die objectDie = new Die();
//Game object = new Game();
就这样做吧
Game object;
然后在使用takeTurn方法
时存储游戏对象public void takeTurn(Game object) {
this.object = object;
object.play();
.....
}
然后在你的lol方法中,尝试使用takeTurn方法遍历游戏的所有骰子
public int lol(int a) {
num = 0;
for (int i = 0; i < object.getDices().size(); i++) {
if (object.getDices().get(i) == a) {
num += a;
}
}
return num;
}
最后,记得为Game类创建getDices方法:)
public class Game {
Die DieObjekt = new Die();
public List<Integer> getDices() {
return DieObjekt.getDices();
}