Results 1 to 2 of 2
  1. #1
    o0n1 is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default printf error with int and double

    I have the following problem, I'm trying to print some values in the following format(shown on the following code) but printf doesn't work. when i tried to run the program it gave the following message<<Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int, double)>>

    the code :
    System.out.printf("%10d%22.4f", dice, average);
    where dice is of type int and average is of type double.

    The following is the whole program please Help!!


    public class DiceAverage {

    static final int NUMBER_OF_EXPERIMENTS = 10000;

    public static void main(String[] args){
    double average;

    System.out.println("Total On Dice Average Number of Rolls");
    System.out.println("_____________ _______________________");

    for(int dice = 2; dice <= 12; dice++ ){
    average = AverageRollCount(dice);
    System.out.printf("%10d%22.4f", dice, average);


    }

    }
    public static double AverageRollCount( int Roll ){
    int RollCountThisExperiment;
    int TotalRoll;
    double averageRollCount;

    TotalRoll = 0;
    for(int i = 0; i < NUMBER_OF_EXPERIMENTS; i++){
    RollCountThisExperiment = rollFor( Roll );
    TotalRoll += RollCountThisExperiment;
    }
    averageRollCount = ((double)TotalRoll) / NUMBER_OF_EXPERIMENTS;
    return averageRollCount;
    }
    public static int rollFor( int N ){
    int die1, die2;
    int roll;
    int rollCount = 0;
    do {
    die1 = (int)(Math.random()*6) + 1;
    die2 = (int)(Math.random()*6) + 1;
    roll = die1 + die2;
    rollCount = rollCount++;
    }while (roll != N );
    return rollCount;
    }
    }

    I use eclipse version:3.4.0

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Please copy the full text of the error message without editting it.
    Here's what I get when I execute it using: jre1.6.0_02\bin\java.exe :
    Java Code:
    Total On Dice Average Number of Rolls
    _____________ _______________________
             2                0.0000
             3                0.0000
             4                0.0000
             5                0.0000
             6                0.0000
             7                0.0000
             8                0.0000
             9                0.0000
            10                0.0000
            11                0.0000
            12                0.0000
    Last edited by Norm; 09-18-2008 at 09:16 PM.

Similar Threads

  1. Double Value problem
    By sakthivel123 in forum New To Java
    Replies: 2
    Last Post: 07-10-2008, 04:18 PM
  2. Demonstration of printf() method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:37 PM
  3. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:13 PM
  4. printf
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:31 AM
  5. How to round a double?
    By Valeriano in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 03:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •