-1

Possible Duplicate:
Simple way to repeat a String in java

How can I convert numbers into symbols? the last part of my assignment goes like this:The histogram should display the bar graphs from 2-12 based upon how many times that value was rolled.like this: like this: currently my output is like this:enter image description here

    public static void main(String[] args) {
    // TODO code application logic here
    System.out.print("Please enter how many times you want to roll two dice?");
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int [] rolls = new int[n];

    Random r1 = new Random();
    Random r2 = new Random();

    int dice1;
    int dice2;

    int two = 0;
    int three = 0;
    int four = 0;
    int five = 0;
     int six = 0;
    int seven = 0;
    int eight = 0;
    int nine = 0;
     int ten = 0;
    int eleven = 0;
    int twelve = 0;

    for (int roll=0; roll < rolls.length; roll++)
    {
         dice2 = r2.nextInt(6)+1;
         dice1 = r1.nextInt(6)+1;
         System.out.println(roll + " The first dice rolled a " + dice1 + " the second dice rolled a " + dice2);

         int sum;
         sum = dice1 + dice2;

         if (sum == 2)
             two++;
         if (sum == 3)
             three++;
         if (sum == 4)
             four++;
         if (sum == 5)
             five++;
         if (sum == 6)
             six++;
         if (sum == 7)
             seven++;
         if (sum == 8)
             eight++;
         if (sum == 9)
             nine++;
         if (sum == 10)
             ten++;
         if (sum == 11)
             eleven++;
         if (sum == 12)
             twelve++;

    }
    System.out.println("Histogram of rolls:" );  
    System.out.println("2 occurred " + two + " times");
    System.out.println("3 occurred " + three + " times");
    System.out.println("4 occurred " + four + " times");
    System.out.println("5 occurred " + five + " times");
    System.out.println("6 occurred " + six + " times");
    System.out.println("7 occurred " + seven + " times");
    System.out.println("8 occurred " + eight + " times");
    System.out.println("9 occurred " + nine + " times");
    System.out.println("10 occurred " + ten + " times");
    System.out.println("11 occurred " + eleven + " times");
    System.out.println("12 occurred " + twelve + " times");




}

}

Community
  • 1
  • 1
UnPatoCuacCuac
  • 315
  • 2
  • 7
  • 18
  • 1
    I would note that your code could be simplified *drastically* by using an array for all of counts instead of a bunch of variables for each one. Clearly, you do know about arrays ... – Stephen C Oct 28 '12 at 02:04
  • I know @StephenC but when i turned into an array i could not figure out how to start counting from 2 to twelve. the thing always kept saying 1 occurred 0 times. maybe you can illustrate me. thanks – UnPatoCuacCuac Oct 28 '12 at 02:42

2 Answers2

5

Create an additional function called toStars

public String toStars(int number) {
    StringBuilder temp = new StringBuilder();
    for(int i=0;i<number;i++) {
        temp.append("*");
    }
    return temp.toString();
}

Then, alter your print statements as such:

System.out.println("2 : " + toStars(two));
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • I'd encourage the use of `StringBuilder` over string concatenation, but the idea is correct ;) – MadProgrammer Oct 28 '12 at 02:16
  • @MadProgrammer I was going to use that, but couldn't remember the exact syntax. Working on it now. – SomeKittens Oct 28 '12 at 02:17
  • Not a biggy, in this context it's not going to make a huge difference, but it always nice to encourage good practice, +1 anyway ;) – MadProgrammer Oct 28 '12 at 02:29
  • I tried that but I don't understand what is that (int number) next to the class to stars. as you can see I'm a noob first semester programing. thanks.@SomeKittens – UnPatoCuacCuac Oct 28 '12 at 03:37
  • It's not `class` `toStars`, it's a function. The `int` tells the compiler that `toStars` should take one parameter, which is an integer. – SomeKittens Oct 28 '12 at 04:37
1
int n = 10;

for(int i = 0; i < n; i++)
{
   System.out.print("*");
}

That code will print '*' 10 times.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78