如何访问我的方法(Java)?

如何访问我的方法(Java)?,java,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 +

我已经在调试器和测试中尽了最大努力,但是我不能把注意力集中在这个问题上

在这种方法中,它跳过了进入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;

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 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) + ", ");
        }
    }
 }
包装骰子游戏;
导入java.util.ArrayList;
导入java.util.Random;
公共级模具{
ArrayList骰子=新的ArrayList();
Random ran=新的Random();
整数;
公共数组列表getDices(){
返回骰子;
}
公共无效集合号(整数){
这个数字=数字;
}
公众登记册(){
for(int i=0;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;
    }
}
包装骰子游戏;
导入java.util.Scanner;
公共类人类工具播放器{
int一,二,三,四,五,六,num;
扫描仪sc=新的扫描仪(System.in);
模具对象模具=新模具();
游戏对象=新游戏();
@凌驾
公共无效轮流(游戏对象){
object.play();
System.out.println(“如果要停止,请按enter,否则按”
+“模具上要保存的编号”);
int valg=sc.nextInt();
开关(valg){
案例1:
1=lol(1);
System.out.println(“你得到了“+1+”分”
+“一个”);
打破
案例2:
二=lol(2);
System.out.println(“你得到了“+2+”分”
+“两个”);
打破
案例3:
三=lol(3);
System.out.println(“你得到了“+3+”分”
+“三”);
打破
案例4:
四=lol(4);
System.out.println(“你得到了“+4+”分”
+“四个”);
打破
案例5:
五=lol(5);
System.out.println(“你在考试中得到“+5+”分”
+“五”);
打破
案例6:
六=lol(6);
System.out.println(“你得到了“+6+”分”
+“六”);
打破
违约:
System.out.println(“您已经停止了,这很好”);
打破
}
}
公共内部lol(内部a){
num=0;
for(int i=0;i
在课堂游戏中,您完成了创建骰子列表的过程(设置总数并使用掷骰添加值)=>数组列表有值

但是你忘了在人类课堂上做这件事

====================

编辑

以下是一些建议,供您修复游戏

    /**
     * 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();

        .....
    }
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;
}
然后在你的lol方法中,试着循环通过你刚才从takeTurn方法得到的所有游戏骰子

public void takeTurn(Game object) {
        this.object = object;
        object.play();

        .....
    }
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;
}
public int lol(int a){
num=0;
for(int i=0;i
最后,记住为游戏类创建getDices方法:)

公共类游戏{
Die DieObjekt=新模具();
公共列表getDices(){
返回DieObjekt.getDices();
}

你从不在骰子列表中添加任何内容。这应该怎么做?你从不调用objectDie.roll-Method。因此你的骰子列表保持为空。本杰明,是的,我是这样做的。它在play方法中,在人类类中被调用。请相信我,Murat K,你也曾经是这门语言的新手。你说它从未被分配是什么意思?你相信吗每次在对象上调用一个新的arraylist?object.play的调用调用roll()到Game类中的一个DieObject。但是这和你的Human类中的对象不同。有那么一会儿,我以为你在写德语!(“DieObjekt”).无论如何,
Dice
已经是复数了,所以你不需要在末尾附加“s”。更严格地说,他在
人类对象中创建了一个全新的
Die
实例。他必须使用
游戏中的
Die
对象。我的意思是,总体而言,整个代码都很混乱,需要重新编写itten.是否正确理解,ArrayList存储在对象上而不在类中?我有点困惑。如果是这样,我如何将数组从对象骰子中获取到人类类中?我是否可以以某种方式将骰子对象获取到这个类中?我更新了我的答案,并给你一些建议来完成游戏:)如果我可以投票给你更多的话有一次,我会的。这太棒了,消除了我对这个话题的所有困惑。谢谢你洪沃,干得好!