【问题标题】:Formatting Dice Roll Output Java格式化掷骰子输出 Java
【发布时间】:2016-09-23 08:17:47
【问题描述】:

我创建了一个代码,用户可以在其中输入掷骰子的次数。然后程序输出面值、每张脸出现的次数以及每张脸的百分比频率。我必须使用 System.out.printf() 来格式化输出。

我的问题是,每当我输入一个大于 9 的掷骰子时,我的输出格式就完全被抛弃了……这是我的代码:

package variousprograms;
import java.util.*;
public class DiceRoll 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] array = new int[7];
        System.out.print("Enter number of rolls: ");
        int roll = input.nextInt();

        System.out.printf("%s%8s%6s\n", "#", "Count", "Freq");
        for (int i = 1;i<=roll;i++)
        {
            array[(int)(6*Math.random()) + 1]++;
        }
        for(int a = 1; a<array.length; a++)
        {
            double percentage = ((double) array[ a ]/roll)*100;
            System.out.printf("%1d%6d%15.2f%%\n", a, array[ a ], percentage);
        }
        System.out.printf("%s%2s%10s\n", "Total", roll, "100%");


    }
}

我将不胜感激!

【问题讨论】:

    标签: java dice


    【解决方案1】:

    一个可能的解决方案如下:

    public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int[] array = new int[7];
            System.out.print("Enter number of rolls: ");
            int roll = input.nextInt();
    
            System.out.printf("%-12s%-12s%-12s\n", "#", "Count", "Freq");
            for (int i = 1; i <= roll; i++) {
                array[(int) (6 * Math.random()) + 1]++;
            }
            for (int a = 1; a < array.length; a++) {
                double percentage = ((double) array[a] / roll) * 100;
                System.out.printf("%-12d%-12d%5.2f%%\n", a, array[a], percentage);
            }
            System.out.printf("%-12s%-14s%-12s\n", "Total", roll, "100%");
        }
    

    整数格式化

    %d   : will print the integer as it is.
    %6d  : will print the integer as it is. If the number of digits is less than 6, the output will be padded on the left.
    %-6d : will print the integer as it is. If the number of digits is less than 6, the output will be padded on the right.
    %06d : will print the integer as it is. If the number of digits is less than 6, the output will be padded on the left with zeroes.
    %.2d : will print maximum 2 digits of the integer.
    

    字符串格式

    %s    : will print the string as it is.
    %15s  : will print the string as it is. If the string has less than 15 characters, the output will be padded on the left.
    %-6s  : will print the string as it is. If the string has less than 6 characters, the output will be padded on the right.
    %.8d  : will print maximum 8 characters of the string.
    

    浮点点格式

    %f    : will print the number as it is.
    %15f  : will print the number as it is. If the number has less than 15 digits, the output will be padded on the left.
    %.8f  : will print maximum 8 decimal digits of the number.
    %9.4f : will print maximum 4 decimal digits of the number. The output will occupy 9 characters at least. If the number of digits is not enough, it will be padded
    

    完整教程here

    【讨论】:

    • 这不是“解决方法”,这只是使用printf的正确方法
    猜你喜欢
    • 2017-11-06
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多