繁体   English   中英

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

[英]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.

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