Java I';我在使用方法向ArrayList添加值时遇到问题

Java I';我在使用方法向ArrayList添加值时遇到问题,java,if-statement,arraylist,methods,parameters,Java,If Statement,Arraylist,Methods,Parameters,所以基本上我的问题是在这个程序中,在我把它放进去之后,我会更好地解释 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 Gam

所以基本上我的问题是在这个程序中,在我把它放进去之后,我会更好地解释

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

}
包学习;
//这个类有一个我一直在研究的重要方法,叫做Dice6 don'tdelete
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Scanner;
公共类游戏板{
公共静态void main(字符串[]args){
System.out.println(“欢迎来到游戏板”);
System.out.println(“你叫什么名字?”;
扫描仪扫描=新扫描仪(System.in);
字符串ScanResult1=Scan.nextLine();
System.out.println(“嗯…”+ScanResult1+“这是电路板”);
System.out.println(“~~~~~~~~~~~”;//不要质疑它的
//这很奇怪,
//但它是有效的
System.out.println(“|”);
System.out.println(“| ~~~~~~~~~~”;
System.out.println(“让我们从掷骰子开始”);
System.out.println(ScanResult1+“得到一卷“+Dice6(10));
Scan.close();
}
静态整数Dice6(整数y){
ArrayList骰子=新的ArrayList();
骰子。加入(1);
骰子。加入(2);
骰子。加入(3);
骰子。加入(4);
骰子。加入(5);
骰子。加入(6);
int DiceAmount=Dice.size();
while(DiceAmount!=6){
DiceAmount=DiceAmount-1;
添加骰子(骰子大小()+1);
}
收集。洗牌(骰子);
收集。洗牌(骰子);
整数DiceResult=Dice.get(3);
返回结果;
}
}
好吧,我现在的问题是 System.out.println(ScanResult1+“得到一卷“+Dice6(10));
在程序的底部,我尝试使用参数10在ArrayList中添加数组值。。。。假设ArrayList中有10个值,这取决于我在参数中输入的内容……我尝试了很多方法,做了很多修复,我曾经做过一次(空白x:Dice)要显示所有的值和10,不只是原来的6…如果有人知道如何解决这个问题,请告诉我这段代码毫无意义:

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

我不明白你的意思。是什么让你认为你的代码应该把10个元素放入你的
列表
?另外,如果你遵循标准的命名约定,你的代码会更容易让其他人阅读。特别是,方法和局部变量名应该以小写字母开头,以camelCase表示。什么意思是“根据我在参数中的输入,ArrayList中应该有10个值”?我想你说的是method
Dice6()
,但是如果假设正好有10个值,那么参数与它有什么关系呢?或者,您的意思是参数应该指定有多少个值?另外,您是否考虑过您的
Dice6()
方法是生成随机数的真正可怕的实现?我建议你用一种更直接的方法来研究class
Random
。@John Bollinger你是对的。编码标准很差。。我最初认为骰子6(10)是一个构造函数。你说得很对,但这并不是问题的答案。谢谢你的帮助。我很困惑,没有意识到while命令没有运行,我搞混了,因为我忘了骰子的数量永远是6。。。。是的,我确实想把y放进去,因为我的计划是让它继续做一个骰子。加,直到它被减到等于6为止