Java方法返回0而不管方法输出如何

时间:2019-02-25 07:04:45

标签: java

我的YahtzeeHand类中具有以下方法:

int faceValue(int n){

    int count = 0;

    for(Die die : dice){
        if(die.getCurrentValue() == n){
            count++;
        }
    }

    return n * count;
}

dice是一个Die对象的ArrayList,.getCurrentValue获取该die的值。

我正在尝试确定骰子ArrayList中有多少个Die的值为n,然后将该数字(计数)乘以n。

System.out.printf("%s",faceValue(1));

无论是否存在值为1的Die对象,该方法始终返回0。您对我在这里做错什么以及如何获取“ n * count”返回值有任何建议吗?

编辑:为澄清起见,这是我的三个课程:

DieTester.java

package com.company;

public class DieTester {

    public static void main(String[] args){

        YahtzeeHand report = new YahtzeeHand();

        report.reportLine();

    }

}

YahtzeeHand.java

package com.company;

import java.util.ArrayList;
import java.util.Collections;

public class YahtzeeHand {

    public int numDice, numSides, occurrences;
    private int sum = 0;

    private ArrayList<Die> dice = new ArrayList<>();

    YahtzeeHand(){
        numDice = 5;
        numSides = 6;
    }

    YahtzeeHand(int numDice){
        this.numDice = numDice;
    }

    YahtzeeHand(int numDice, int numSides){
        this.numDice = numDice;
        this.numSides = numSides;
    }

    void rollDice(){
        for (Die die : dice){
            die.roll();
        }
    }

    public String toString() {

        String result = "";
        for (Die die : dice) {
            result = result +  " " + die;
        }
        return result;
    }

    int countDice(){
        return numDice;
    }

    void setDice(Die firstDice, Die secondDice, Die thirdDice, Die fourthDice, Die fifthDice){

        dice.add(firstDice);
        dice.add(secondDice);
        dice.add(thirdDice);
        dice.add(fourthDice);
        dice.add(fifthDice);

    }

    int faceValue(int n){

        int count = 0;

        for(Die die : dice){
            if(die.getCurrentValue() == n){
                count++;
            }
        }

        return n * count;
    }

    int threeKindValue() {

        boolean threeKind = false;

        ArrayList<Integer> listOfValues = new ArrayList<>();

        for (Die die : dice) {
            listOfValues.add(die.getCurrentValue());
        }

        for (Integer num : listOfValues) {
            if (Collections.frequency(listOfValues, num)>2 ) threeKind = true;
        }

        if (threeKind){
            for(Integer num2 : listOfValues){
                sum += num2;
            }

            return sum;
        } else return 0;

    }

    int fourKindValue() {

        boolean fourKind = false;

        ArrayList<Integer> listOfValues = new ArrayList<>();

        for (Die die : dice) {
            listOfValues.add(die.getCurrentValue());
        }

        for (Integer num : listOfValues) {
            if (Collections.frequency(listOfValues, num)>3 ) fourKind = true;
        }

        if (fourKind){
            for(Integer num2 : listOfValues){
                sum += num2;
            }

            return sum;
        } else return 0;

    }

    int fullHouseValue(){

        boolean fullHouseThree = false;
        boolean fullHouseTwo = false;

        ArrayList<Integer> listOfValues = new ArrayList<>();

        for (Die die : dice) {
            listOfValues.add(die.getCurrentValue());
        }

        for (Integer num : listOfValues) {
            if (Collections.frequency(listOfValues, num) == 3) fullHouseThree = true;
        }

        for (Integer num : listOfValues) {
            if (Collections.frequency(listOfValues, num) == 2) fullHouseTwo = true;
        }

        if (fullHouseThree && fullHouseTwo) return 25;
        else return 0;
    }

    int largeStraightValue(){

        ArrayList<Integer> listOfValues = new ArrayList<>();

        for (Die die : dice) {
            listOfValues.add(die.getCurrentValue());
        }

        for (int i = 0; i < listOfValues.size() - 1; i++) {
            if (listOfValues.get(i) != listOfValues.get(i + 1) - 1) {
                return 40;
            }
        }
        return 0;
    }

    int yahtzeeValue(){
        boolean yahtzee = false;

        ArrayList<Integer> listOfValues = new ArrayList<>();

        for (Die die : dice) {
            listOfValues.add(die.getCurrentValue());
        }

        for (Integer num : listOfValues) {
            if (Collections.frequency(listOfValues, num) == 5 ) yahtzee = true;
        }

        if (yahtzee){  return 50; } else return 0;
    }

    int chanceValue(){

        ArrayList<Integer> listOfValues = new ArrayList<>();

        for (Die die : dice) {
            listOfValues.add(die.getCurrentValue());
        }

        for (Integer num : listOfValues) {
            sum += num;
        }

        return sum;
    }

    void reportLine (int lineNum){

        YahtzeeHand hand = new YahtzeeHand();

        Die die1 = new Die(hand.numSides);
        Die die2 = new Die(hand.numSides);
        Die die3 = new Die(hand.numSides);
        Die die4 = new Die(hand.numSides);
        Die die5 = new Die(hand.numSides);

        hand.setDice(die1, die2, die3, die4, die5);
        hand.rollDice();

        System.out.printf("%s.  %s    %s", lineNum, hand.toString(), faceValue(1));
    }
}

Die.java

import java.util.*;

public class Die {

    private int numSides, roll;

    private Random random = new Random();

    Die(int numSides) { this.numSides = numSides; }

    public Die() { this(6); }

    public int getCurrentValue() { return roll; }

    public String toString() { return roll +  " "; }

    int roll() {
        roll = random.nextInt(numSides) + 1;
        return roll;
    }

    public void cheat(int cheater) {
        if (cheater < 0 ) System.out.println("Can't be negative");
        else this.roll = cheater;
    }

    public void reallycheat(int reallyCheat) {
        this.roll = reallyCheat;
        System.out.println("Stop trying to cheat so much!");
    }
}

2 个答案:

答案 0 :(得分:3)

YahtzeeHand(int numDice)不会设置numSides,因此滚动的上限为0。

防止这种过时的构造函数的一种方法是调用一个完整的构造函数:

YahtzeeHand() {
    this(5, 6);
}

YahtzeeHand(int numDice) {
    this(numDice, 6);

}

YahtzeeHand(int numDice, int numSides) {
    this.numDice = numDice;
    this.numSides = numSides;
}

答案 1 :(得分:3)

您的问题是您有两个YahtzeeHand实例。

第一个在您的main中创建:

public static void main(String[] args){
    YahtzeeHand report = new YahtzeeHand();
    report.reportLine();
}

但是,您在reportLine中创建了另一个实例:

void reportLine (int lineNum)
{
    YahtzeeHand hand = new YahtzeeHand(); // second instance created

    Die die1 = new Die(hand.numSides);
    Die die2 = new Die(hand.numSides);
    Die die3 = new Die(hand.numSides);
    Die die4 = new Die(hand.numSides);
    Die die5 = new Die(hand.numSides);

    hand.setDice(die1, die2, die3, die4, die5); // dice added to second instance
    hand.rollDice();

    System.out.printf("%s.  %s    %s", lineNum, hand.toString(), faceValue(1)); // faceValue(1) called for original instance
}

您为第二个实例(setDice)调用hand.setDice(die1, die2, die3, die4, die5),但为没有骰子的原始实例调用faceValue(1)

只需消除第二个实例:

void reportLine (int lineNum)
{
    Die die1 = new Die(numSides);
    Die die2 = new Die(numSides);
    Die die3 = new Die(numSides);
    Die die4 = new Die(numSides);
    Die die5 = new Die(numSides);

    setDice(die1, die2, die3, die4, die5);
    rollDice();

    System.out.printf("%s.  %s    %s", lineNum, toString(), faceValue(1));
}