Java方法(DiceProblem)[关闭]

Posted

技术标签:

【中文标题】Java方法(DiceProblem)[关闭]【英文标题】:Java Method(DiceProblem) [closed] 【发布时间】:2021-01-22 00:56:49 【问题描述】:

好吧,接下来的程序在运行时会让我“掷一对骰子”n 次(即 生成一个介于 1 和 6 之间的数字),其中 n 是输入的正整数。这 然后程序输出这对数字(“掷骰子”),以及数字的总和 (每对骰子)。数字对存储在二维数组中,总和 每对存储在一个单维数组中。

import java.util.Scanner;
import java.util.Random;

public class diceProblem
public static void main(String[] args)
    Scanner input = new Scanner(System.in); 
    
    System.out.print("Enter the amount of times a pair of dice will roll: ");
    int times = input.nextInt(); 
    
    int[][]dicePairs = rollDicePairs(times);
    System.out.println("The rolled pairs of numbers are: ");
    printDicePairs(dicePairs);
    
    int[][]dicePairsSums = addDicePairs(dicePairs);
    System.out.println("The sum of each pairs of numbers are: ");
    printDicePairsSums(dicePairsSums);


public static int[][]rollDicePairs(int times) 
    int[][]dice = new int[times][2];
    Random die = new Random();
    
    for(int x=0;x<times;x++)
        for(int y=0;y<2;y++)
            dice[x][y] = 1+die.nextInt(6);
        
    
    return dice;


public static void printDicePairs(int[][]dicePairs)
    for(int[]pair: dicePairs)
        for(int roll: pair)
            System.out.print(roll+" ");
        
        System.out.println("");
    


public static int[][]addDicePairs()
    int[]pairSums = new int[dicePairs];
    for(int x=0;x<pairSums.length;x++)
        int sum = 0;
        for(int y=0;y<dicePairs[x].length;y++)
            sum += dicePairs[x][y];
        
        pairSums[x] = sum;
    
    return pairSums;


public static void printDicePairsSum(int[]dicePairsSums)
    for(int sum: dicePairsSums)
        System.out.print(sum+" ");
    


问题是这个程序有太多错误,它没有运行或输出成对的数字和和

【问题讨论】:

【参考方案1】:

您的代码无法编译。下面是可以编译的更正代码,并且在您运行它时也会产生正确的结果。将其与您的代码进行比较以发现差异。

import java.util.Random;
import java.util.Scanner;

public class DiceProb 
    public static void main(String[] args) 
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the amount of times a pair of dice will roll: ");
        int times = input.nextInt();

        int[][] dicePairs = rollDicePairs(times);
        System.out.println("The rolled pairs of numbers are: ");
        printDicePairs(dicePairs);

        int[] dicePairsSums = addDicePairs(dicePairs);
        System.out.println("The sum of each pairs of numbers are: ");
        printDicePairsSum(dicePairsSums);
    

    public static int[][] rollDicePairs(int times) 
        int[][] dice = new int[times][2];
        Random die = new Random();

        for (int x = 0; x < times; x++) 
            for (int y = 0; y < 2; y++) 
                dice[x][y] = 1 + die.nextInt(6);
            
        
        return dice;
    

    public static void printDicePairs(int[][] dicePairs) 
        for (int[] pair : dicePairs) 
            for (int roll : pair) 
                System.out.print(roll + " ");
            
            System.out.println("");
        
    

    public static int[] addDicePairs(int[][] dicePairs) 
        int[] pairSums = new int[dicePairs.length];
        for (int x = 0; x < pairSums.length; x++) 
            int sum = 0;
            for (int y = 0; y < dicePairs[x].length; y++) 
                sum += dicePairs[x][y];
            
            pairSums[x] = sum;
        
        return pairSums;
    

    public static void printDicePairsSum(int[] dicePairsSums) 
        for (int sum : dicePairsSums) 
            System.out.print(sum + " ");
        
    

【讨论】:

以上是关于Java方法(DiceProblem)[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

Java - 如何使用密码短语手动加密数据? [关闭]

OAuth Provider 库(Java)[关闭]

H.323 请求 Java [关闭]

NotePad++安装及配置

用于对 Java 项目进行逆向工程的 UML 工具 [关闭]

C ++迭代器模型与Java迭代器模型[关闭]