简体   繁体   English

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

[英]How do I access my method (Java)?

I have tried my best looking in the debugger and testing, but i cannot wrap my mind around this problem. 我已经在调试器和测试中尽了最大努力,但我无法解决这个问题。

In this method it skips going inside the for loop. 在这个方法中,它跳过for循环。 But in the roll() method, the arraylist gets values? 但是在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;
}

I will post all my classes to create a better image of the code, the troublemaker is at the bottom of the last class I have linked. 我将发布所有类以创建更好的代码图像,麻烦制造者位于我已链接的最后一个类的底部。

Main 主要

package dicegame;

public class DiceGame {
    public static void main(String[] args) {
        Game test = new Game();
        test.numberDices(); 
        Player spil = new Human();
        spil.takeTurn(test);
    }
} 

Game 游戏

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();
    }
}

Die

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) + ", ");
        }
    }
 }

Human - the last method in this class is the troublemaker 人类 - 本课程的最后一个方法是麻烦制造者

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;
    }
}

In class game, you finished the process to create dice list (set the total number and add the valuue with roll) => array list has values 在课堂游戏中,您完成了创建骰子列表的过程(设置总数并使用roll添加值)=>数组列表具有值

But you forgot to do it with human class 但是你忘了和人类一起做

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

EDIT 编辑

Here is some suggestion for you to fix your game 以下是您修复游戏的一些建议

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

Just do it like this 就这样做吧

Game object;

And then store the game object when you have it from takeTurn method 然后在使用takeTurn方法存储游戏对象时存储它

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

        .....
    }

And then in your lol method, try to loop through all the dice of game which you just have from takeTurn method 然后在你的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;
}

Finaly, remember to create getDices method for Game class :) 最后,记得为Game类创建getDices方法:)

public class Game {

    Die DieObjekt = new Die();

    public List<Integer> getDices() {
        return DieObjekt.getDices();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM