【问题标题】:I'm Having trouble adding values to a ArrayList with methods我在使用方法向 ArrayList 添加值时遇到问题
【发布时间】:2016-04-05 14:27:20
【问题描述】:

所以基本上我的问题出在这个程序中,我会在放入后更好地解释

    package learning;
//This class has an important method that i have been working on called Dice6 don't delete

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

public class GameBoard {

    public static void main(String[] args) {

        System.out.println("Welcome To The Gameboard");
        System.out.println("What is your name player 1?");

        Scanner Scan = new Scanner(System.in);

        String ScanResult1 = Scan.nextLine();

        System.out.println("Well... " + ScanResult1 + " This is the Board");

        System.out.println("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~|");// Don't Question its
                                                        // placement its weird,
                                                        // but it works
        System.out.println("               |");
        System.out.println("               |~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");

        System.out.println("Lets start with a Dice Roll");

        System.out.println(ScanResult1 + " Got a Roll of " + Dice6(10));

        Scan.close();

    }

    static int Dice6(int y) {
        ArrayList<Integer> Dice = new ArrayList<Integer>();
        Dice.add(1);
        Dice.add(2);
        Dice.add(3);
        Dice.add(4);
        Dice.add(5);
        Dice.add(6);


        int DiceAmount = Dice.size();
        while (DiceAmount != 6) {
            DiceAmount= DiceAmount - 1;
            Dice.add(Dice.size() + 1);
        }
        Collections.shuffle(Dice);
        Collections.shuffle(Dice);
        Integer DiceResult = Dice.get(3);
        return DiceResult;
    }

}

好的,我遇到的问题来自 System.out.println(ScanResult1 + "得到一卷" + Dice6(10)); 到程序的底部,基本上我正在尝试使用参数 10 在 ArrayList 中添加 Array 值......假设发生的情况是 ArrayList 中假设有 10 个值,具体取决于我在参数中输入的内容....我已经尝试了很多事情并做了很多修复,我曾经做过 for(blank x : Dice) 来显示所有值,而 10 并没有出现只有原来的 6....如果任何人都知道如何解决这个问题,请告诉我

【问题讨论】:

  • 我没有关注你。是什么让您认为您的代码应该将 10 个元素放入您的 List
  • 此外,如果您遵循标准命名约定,您的代码将更容易被其他人阅读。特别是,方法和局部变量的名称应该以小写字母开头,采用驼峰式。
  • “根据我在参数中输入的内容,假设 ArrayList 中有 10 个值”是什么意思?我想你在谈论方法Dice6(),但是如果应该有正好10个值,那么参数与它有什么关系?或者你的意思是参数应该指定有多少个值?
  • 另外,您是否认为您的Dice6() 方法是生成随机数的真正可怕的实现?我建议您查看Random 的课程,以获得更直接的方式。
  • @John Bollinger 你是对的。编码标准非常糟糕..我最初认为 Dice6(10) 是一个构造函数。

标签: java if-statement arraylist methods parameters


【解决方案1】:

这段代码没有意义:

int DiceAmount = Dice.size();       /* this will be 6 */
while (DiceAmount != 6) {           /* this will never be true, do you want y here? */
    DiceAmount= DiceAmount - 1;     /* huh? I think you want addition here */
    Dice.add(Dice.size() + 1);      /* this will never happen */
}

“答案表格”中的更多内容...使用此:

int DiceAmount = Dice.size();
while (DiceAmount != y) 
    DiceAmount= DiceAmount + 1;
    Dice.add(Dice.size() + 1);
}

【讨论】:

  • 你说的很对,但这不是问题的答案。
  • 感谢您的帮助,我很困惑,没有意识到 while 命令没有运行我搞混了,因为我忘记了骰子的数量永远是 6.... 是的,我确实意味着放 y 因为我的计划是让它继续制作 dice.add 直到它被减去到 = 6
猜你喜欢
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 2022-11-27
相关资源
最近更新 更多