无论方法输出如何,Java 方法都返回 0

标签 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 获取骰子的值。

我正在尝试确定 dice 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));
    }
}

死.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!");
    }
}

最佳答案

您的问题是您有两个 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));
}

关于无论方法输出如何,Java 方法都返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54861059/

相关文章:

java - Junit4 运行一个测试类固定次数并显示结果(eclipse)

java - 在 2 秒内移动到另一个 Activity

java - 问题是 while 循环没有以 Java 中的 system.in 获取输入段落结束

java - 从外部中间件获取 RecordStore 数据

java - 平滑的网格运动

java - 在 Java 中编码可变长度的 utf8 字节数组

java - 我希望能够撤消在 JTextArea 中所做的更改

java - Sonar 不使用项目 pom.xml 中的 exceptFilterFile

java - 如何在素数范围程序的输出中打印行号?

Java 等同于 C# 的类型?