CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2009
    Location
    USA
    Posts
    49

    Resolved Getting the Percentage of Every Outcome Help Java Program

    I made a dice rolls program where it counts how many of each face value on a die (1-6)

    But I'm having problems getting it to output the outcome of each as a percentage of the number of rolls. I keep getting 0 as my percentage output for each of the face values.

    Also, another question, how do I print all the above info including the data for outcomes for each roll into a table?

    Here's my code:

    import java.util.Scanner;
    import java.util.Random;

    class MyRandomOutcomes{

    public static void main(String[] args) {
    Random random = new Random();
    Scanner input = new Scanner(System.in);
    int x,sum1,sum2,sum3,sum4,sum5,sum6;
    System.out.println("Enter the number of rolls: ");
    x = input.nextInt();
    int oneCntr = 0;
    int twoCntr = 0;
    int threeCntr = 0;
    int fourCntr = 0;
    int fiveCntr = 0;
    int sixCntr = 0;


    for(int i = 1; i <= x; i++){
    int num = random.nextInt(6) + 1;
    if(num==1){
    sum1= oneCntr++;
    }
    if(num==2){
    sum2= twoCntr++;
    }
    if(num==3){
    sum3= threeCntr++;
    }
    if(num==4){
    sum4= fourCntr++;
    }
    if(num==5){
    sum5= fiveCntr++;
    }
    if(num==6){
    sum6= sixCntr++;
    }
    }
    //System.out.println(num);
    System.out.println("The face value 1 occured:" +oneCntr);
    System.out.println("In percent face value 1 occurred:" +oneCntr*((x/10)*100));
    System.out.println("The face value 2 occured:" +twoCntr);
    System.out.println("In percent face value 2 occurred:"+twoCntr*((x/10)*100));
    System.out.println("The face value 3 occurred:" +threeCntr);
    System.out.println("In percent face value 3 occurred:" +threeCntr*((x/10)*100));
    System.out.println("The face value 4 occured:" +fourCntr);
    System.out.println("In percent face value 4 occurred:" +fourCntr*((x/10)*100));
    System.out.println("The face value 5 occured:" +fiveCntr);
    System.out.println("In percent face value 5 occurred:" +fiveCntr*((x/10)*100));
    System.out.println("The face value 6 occured:" +sixCntr);
    System.out.println("In percent face value 6 occurred:" +sixCntr*((x/10)*100));
    }
    }
    Last edited by coder752; July 13th, 2009 at 10:10 PM. Reason: Additional Explanation

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Getting the Percentage of Every Outcome Help Java Program

    When dealing with integer math you need to remember that there are no floating point results anywhere throughout the calculation so if, for example, you divide 5 by 10 the result is 0. Siimilarly 5 divided by 10 and then multplied by 100 is still 0, whereas 5 multiplied by 100 and then divided by 10 is 50.

    So for ((x/10)*100), if x is less than 10 the result is always going to be zero; if x is between 10 and 19 the result will be 100, if x is between 20 and 29 the result will be 200 etc etc. Obviously this isn't how you calculate a percentage so you may want to look at your calculation.

    You may also want to look at all that repeated code, if you use an array to store the results you can get rid of all the if statements (which should have been if-else statements) and you'd only need 1 print statement inside a for loop.

  3. #3
    Join Date
    Jul 2009
    Location
    USA
    Posts
    49

    Talking Re: Getting the Percentage of Every Outcome Help Java Program

    Now how do I implement this for a JTable so that the outcomes and percentages get put into a table?

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Getting the Percentage of Every Outcome Help Java Program


  5. #5
    Join Date
    Jul 2009
    Location
    USA
    Posts
    49

    Re: Getting the Percentage of Every Outcome Help Java Program

    Are there any other examples...I need simpler examples. Should I add this JTable to the program above or separate?

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Getting the Percentage of Every Outcome Help Java Program

    Quote Originally Posted by coder752 View Post
    Are there any other examples...I need simpler examples.
    If the SimpleTableDemo.java given in the tutorial isn't simple enough, I don't see how you are going to be able to use JTable... You assemble your data, put it into the table, put the table into a scroll pane and put the scroll pane into your GUI. That's all, for the simple case.

    Why not copy the code, get it to run, and try to understand it - the tutorial explains it all. If you need more than the simple code there, the tutorial takes you through more flexible and custom ways of using tables, complete with example code. Some effort is required to make progress in anything.

    If you don't understand something, just ask.

    They know enough who know how to learn...
    J. Adams
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured