[英]How do I access my method (Java)?
我已經在調試器和測試中盡了最大努力,但我無法解決這個問題。
在這個方法中,它跳過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;
}
}
在課堂游戲中,您完成了創建骰子列表的過程(設置總數並使用roll添加值)=>數組列表具有值
但是你忘了和人類一起做
====================
編輯
以下是您修復游戲的一些建議
/**
* 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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.