2016-09-22 1 views
2

J'ai créé un code dans lequel l'utilisateur entre le nombre de fois qu'un dé sera lancé. Le programme affiche ensuite les valeurs de visage, le nombre de fois où chaque visage est apparu, et un pourcentage de fréquence de chaque visage. Je dois utiliser System.out.printf() pour formater la sortie.Formatage de sortie de jet de dés Java

Mon problème est que chaque fois que je entrée un rouleau qui est plus de 9, la mise en forme de ma sortie est complètement jeté hors ... Voici mon code:

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%"); 


    } 
} 

Je vous serais reconnaissant toute aide!

Répondre

1

Une solution possible est la suivante:

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

Entier mise en forme

%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. 

Chaîne mise en forme

%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. 

flottant point de mise en forme

%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 

Un tutoriel complet here.

+1

Ce n'est pas une "solution de contournement", c'est juste la bonne façon d'utiliser 'printf' –

+0

@Stephen P Je l'ai édité. :) – GOXR3PLUS