mercredi 4 septembre 2019

My random number generator is printing the power out in weird form [duplicate]

This question already has an answer here:

Everything works fine except sometimes (with big numbers i think) the last output for the power of 3 numbers comes out weirdly even with decimal format.

tried switching up int and double and decimal format

public static void main(String[] args) {
    // TODO Auto-generated method stub
     Random rand = new Random();

    int rand_int1 = rand.nextInt( 6)+ 1;
    int rand_int2 = rand.nextInt(6)+ 1;
    int rand_int3 = rand.nextInt(6)+ 1;
    System.out.println("First number " +(rand_int1));
    System.out.println("second number " +(rand_int2));
    System.out.println("third numnber " +(rand_int3));

    System.out.println("addition " + (rand_int1 + rand_int2 + 
rand_int3));
    System.out.println("subtraction " + (rand_int1 - rand_int2 - 
rand_int3));
    System.out.println("multiplication " + (rand_int1 * rand_int2 
* rand_int3));

     double rand_int4 = Math.pow(rand_int1, rand_int2);
     System.out.println("The power of number 1 2 and 3 is " + 
Math.pow(rand_int4, rand_int3));
     DecimalFormat df = new DecimalFormat ("###,###.00");
}

this is the output : First number 4 second number 4 third number 3 addition 11 subtraction -3 multiplication 48 The power of number 1 2 and 3 is1.6777216E7

where the last line should be an actual number not '1.6777216E7"




Aucun commentaire:

Enregistrer un commentaire