Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
龙与地下城骰子掷骰机重新掷骰 import java.util.*; 公共类项目2主要{ 公共静态void main(字符串参数[]){ 扫描仪kb=新扫描仪(System.in); int numSets=0; System.out.println(“您想要掷多少组骰子?”); numSets=kb.nextInt(); kb.nextLine(); RollingDice roller=新的RollingDice(); 列表骰子集=滚轮。滚轮集(numSets); 用于(列表骰子卷:骰子集){ 整数总计=总和(骰子); 最低整数=Collections.min(骰子); System.out.println(“您的卷总数为:“+total+”,最低卷数为a:“+lost”); } kb.close(); } 公共静态整数和(列表){ 整数和=0; 用于(整数编号:列表) 总和=总和+数字; 回报金额; } } 导入java.util.*; 公共类滚动骰子{ 私有随机兰德; 公共滚动骰子(){ this.rand=new Random(); } 公共列表ROLL4D6DROPOWEST(){ List retList=new ArrayList(); 对于(int i=0;i_Java - Fatal编程技术网

龙与地下城骰子掷骰机重新掷骰 import java.util.*; 公共类项目2主要{ 公共静态void main(字符串参数[]){ 扫描仪kb=新扫描仪(System.in); int numSets=0; System.out.println(“您想要掷多少组骰子?”); numSets=kb.nextInt(); kb.nextLine(); RollingDice roller=新的RollingDice(); 列表骰子集=滚轮。滚轮集(numSets); 用于(列表骰子卷:骰子集){ 整数总计=总和(骰子); 最低整数=Collections.min(骰子); System.out.println(“您的卷总数为:“+total+”,最低卷数为a:“+lost”); } kb.close(); } 公共静态整数和(列表){ 整数和=0; 用于(整数编号:列表) 总和=总和+数字; 回报金额; } } 导入java.util.*; 公共类滚动骰子{ 私有随机兰德; 公共滚动骰子(){ this.rand=new Random(); } 公共列表ROLL4D6DROPOWEST(){ List retList=new ArrayList(); 对于(int i=0;i

龙与地下城骰子掷骰机重新掷骰 import java.util.*; 公共类项目2主要{ 公共静态void main(字符串参数[]){ 扫描仪kb=新扫描仪(System.in); int numSets=0; System.out.println(“您想要掷多少组骰子?”); numSets=kb.nextInt(); kb.nextLine(); RollingDice roller=新的RollingDice(); 列表骰子集=滚轮。滚轮集(numSets); 用于(列表骰子卷:骰子集){ 整数总计=总和(骰子); 最低整数=Collections.min(骰子); System.out.println(“您的卷总数为:“+total+”,最低卷数为a:“+lost”); } kb.close(); } 公共静态整数和(列表){ 整数和=0; 用于(整数编号:列表) 总和=总和+数字; 回报金额; } } 导入java.util.*; 公共类滚动骰子{ 私有随机兰德; 公共滚动骰子(){ this.rand=new Random(); } 公共列表ROLL4D6DROPOWEST(){ List retList=new ArrayList(); 对于(int i=0;i,java,Java,你好,Stackoverflow,我对这个程序有一个小问题。我正在尝试制作一个程序来掷四个骰子,将最高的三个骰子加在一起,然后打印出这三个骰子中最低的一个。我犯了一个错误,把最高的三卷纸加在一起,然后打印出四卷中最低的一卷 如何解决此问题?我在谷歌上搜索了很多关于如何使用列表和集合的信息,但是我找不到任何方法来解决这个问题 另外,我如何才能使它达到我强制用户选择一个集合并重新滚动该集合的位置 基本上,我想做的是将每个集合分配给一个数字(集合1是1,集合2是2,等等),然后用户输入一个与他们拥有的

你好,Stackoverflow,我对这个程序有一个小问题。我正在尝试制作一个程序来掷四个骰子,将最高的三个骰子加在一起,然后打印出这三个骰子中最低的一个。我犯了一个错误,把最高的三卷纸加在一起,然后打印出四卷中最低的一卷

如何解决此问题?我在谷歌上搜索了很多关于如何使用列表和集合的信息,但是我找不到任何方法来解决这个问题

另外,我如何才能使它达到我强制用户选择一个集合并重新滚动该集合的位置

基本上,我想做的是将每个集合分配给一个数字(集合1是1,集合2是2,等等),然后用户输入一个与他们拥有的集合数量相关的数字,然后重新滚动最高的三个集合中最低的一个


非常感谢您的帮助

在这里使用列表是一种轻微的过度使用,这个怎么样:

import java.util.*;

public class Project2Main {

public static void main(String args[]) {
    Scanner kb = new Scanner(System.in);
    int numSets = 0;
    System.out.println("How many sets of dice would you like to roll?");
    numSets = kb.nextInt();
    kb.nextLine();

    RollingDice roller = new RollingDice();
    List<List<Integer>> diceSets = roller.rollSets(numSets);

    for (List<Integer> diceRolls : diceSets) {
        Integer total = sum(diceRolls);
        Integer lowest = Collections.min(diceRolls);
        System.out.println("Your roll total is: " + total + " and the lowest roll was a: " + lowest);
    }
    kb.close();
}

public static int sum(List<Integer> list) {
    int sum = 0;
    for (int number : list)
        sum = sum + number;
    return sum;
}
}

import java.util.*;

public class RollingDice {
private Random rand;

public RollingDice() {
    this.rand = new Random();
}

public List<Integer> roll4d6DropLowest() {
    List<Integer> retList = new ArrayList<Integer>();

    for (int i = 0; i < 4; i++) {
        retList.add(rand.nextInt(6) + 1);
    }

    retList.remove(Collections.min(retList));

    return retList;
}

public List<List<Integer>> rollSets(int numSets) {
    List<List<Integer>> results = new ArrayList<List<Integer>>();
    for (int i = 0; i < numSets; i++) {
        results.add(roll4d6DropLowest());
    }
    return results;
}
}
公共类项目2主{
private static Random rand=new Random(System.currentTimeMillis());
公共静态void main(字符串参数[]){
int numSets=4;//如果需要,通过扫描仪设置
列表骰子=新数组列表(numSets);
对于(int c=0;c0;c--){
sum+=(int)数组。get(c);
如果(c==1){
low=(int)array.get(c);
}
}
int[]对={low,sum};
返回对;
}
}
我承认,返回一个
int[]
并不是所有解决方案中最好的,但是对于这段代码来说应该是可以的

注意按当前时间毫秒初始化Random,以确保随机性


诀窍是将骰子卷存储在一个由
整数组成的
列表中,并在
集合的帮助下根据其“自然顺序”对其进行排序。最高的是最高的。

您的代码正在添加和打印前3名中最低的,而不是全部4名。什么使你认为它不是?@Polansi你能显示预期的输入和预期的输出吗?如果你能做到这一点,并且清楚你想要什么,我可以保证你会得到一个很好的答案。我同意@Dukeling。。。它似乎完全符合您的要求(不包括您尚未实现的功能)
roll4d6DropLowest
只返回三个项目的
列表,因此
Project2Main.main
不可能使用(不再存在)第四个值进行任何操作。如果您在上一篇文章中对我的答案有疑问,请对答案进行评论,我将尝试相应更新。
public class Project2Main {
    private static Random rand = new Random(System.currentTimeMillis());

    public static void main(String args[]) {
        int numSets = 4; //set via Scanner, if wanted

        List<Integer> dices = new ArrayList<Integer>(numSets);
        for (int c = 0; c < numSets; c++) {
            int roll = rand.nextInt(6) + 1;
            dices.add(Integer.valueOf(roll));
            System.out.println(roll);
        }

        int[] pair = roll4d6DropLowest(dices);
        System.out.println("lowest of three highest: " + pair[0]);
        System.out.println("sum of 3 highest is: " + pair[1]);    
    }


    /**@param diceRolls Array of dice rolls
     * @return return array of two ints: 
           0: lowest of three highest
           1: sum of three highest */
    private static int[] rollXd6DropLowest(List<Integer> array) {
        int sum = 0;
        int low = 0;

        Collections.sort(array);
        for (int c = (array.size() - 1); c > 0; c--) {
            sum += (int) array.get(c);
            if (c == 1) {
                low = (int) array.get(c);
            }
        }

        int[] pair = { low, sum };
        return pair;
    }

}