ameliafromafairytale
ameliafromafairytale

Reputation: 43

Floating number not showing up?

I have to make a program for my java class that rolls 2 dice, then compares the sum of their observed percentage of appearance compared to the expected percentage of appearance. The observed percentage is expected to be rounded to do decimal places. However, whenever I run the program the observed percentage always comes out as "%.2fn,[percentage]". Here's my (admittedly somewhat messy) code:

import java.util.*;
public class Problem1 {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    Random roll1 = new Random();
    Random roll2 = new Random();
    int die1;
    int die2;
    int sum;
    double zero = 0;
    double one = 0;
    double two = 0;
    double three = 0;
    double four = 0;
    double five = 0;
    double six = 0;
    double seven = 0;
    double eight = 0;
    double nine = 0;
    double ten = 0;
    double[] occurances = new double [11];
    double[] percentages = new double [11];
    percentages[0] = 2.78;
    percentages[1] = 5.56;
    percentages[2] = 8.33;
    percentages[3] = 11.11;
    percentages[4] = 13.89;
    percentages[5] = 16.67;
    percentages[6] = 13.89;
    percentages[7] = 11.11;
    percentages[8] = 8.33;
    percentages[9] = 5.56;
    percentages[10] = 2.78;
    System.out.print("How many times would you like to roll the dice? ");
    int repeat = keyboard.nextInt();
    for (int i = 0; i < repeat; i++) {
        die1 = roll1.nextInt(6);
        die2 = roll2.nextInt(6);
        sum = die1 + die2;
        if (sum == 0) {
            zero++;
            occurances[0] = zero;
        } else if (sum == 1) {
            one++;
            occurances[1] = one;
        } else if (sum == 2) {
            two++;
            occurances[2] = two;
        } else if (sum == 3) {
            three++;
            occurances[3] = three;
        } else if (sum == 4) {
            four++;
            occurances[4] = four;
        } else if (sum == 5) {
            five++;
            occurances[5] = five;
        } else if (sum == 6) {
            six++;
            occurances[6] = six;
        } else if (sum == 7) {
            seven++;
            occurances[7] = seven;
        } else if (sum == 8) {
            eight++;
            occurances[8] = eight;
        } else if (sum == 9) {
            nine++;
            occurances[9] = nine;
        } else if (sum == 10) {
            ten++;
            occurances[10] = ten;
        }//if

    }//for

    System.out.println("roll      observed      expected");
    for (int i = 0; i < occurances.length; i++) {
        float observed = (float)(occurances[i]/repeat) * 100;
        System.out.println((i+2)+"         %.2fn"+observed+"%     "+percentages[i]+"%");
    }

}

}

How can I get rid of the "%.2fn," before each of the expected percentage and make it only have 2 decimal places? I thought %.2fn was the proper notation for that kind of thing, but it's not working for this.

Upvotes: 1

Views: 562

Answers (1)

rgettman
rgettman

Reputation: 178243

The println method doesn't know anything about formatting patterns such as %.2f; it thinks that's the literal string you want to print.

Look into the printf method instead, which understands formatting patterns.

Upvotes: 3

Related Questions