views:

70

answers:

2

Under the addRolls method I need to populate the nums array with the frequency of the 'total' variable numbers. These method adds the two dice rolls together properly and stores it in the 'total' variable but I cannot seem to properly throw them into an array that does the frequency such as the method above it - indFreq. In case that's hard to follow I think this might make more sense : It's just the addRolls method that needs to be looked at I believe. It needs to return an array with the frequency of the 'total' numbers.

import java.util.Scanner; 
public class Project2 {


public static void main( String[] args )
{
    Scanner inp = new Scanner( System.in );
    int matrix [][]=new int[100][2];

    System.out.print("Press 'q' to exit or 'enter' to continue: \t");
    String response =inp.nextLine();

    while ( !response.toLowerCase().equals("q") ){             
        for (int row=0;row<matrix.length;row++){
            for (int column=0;column<matrix[row].length;column++)
                matrix[row][column]=diceRoll();
        }

        int [] hist1=indFreq(matrix);
        int [] roll=addRolls(matrix);

        for (int i=1;i<hist1.length;i++){
            String star=starPrint(hist1[i]);
            System.out.print(" \n" +hist1[i]+ star);
        }
        System.out.println("");
        //  } 

        System.out.print("\n\nPress 'q' to exit or 'enter' to run again:\t");
        response =inp.nextLine();
    }       
}

public static int diceRoll(){
    int num=(int)(Math.random()*6+1);
    return num;
}

public static String starPrint(int value){
    String star= "";
    for (int i=0;i<value;i++){
        star +="*";
    }
    return star;
}

public static int [] indFreq(int [][] matrix){
    int [] nums =new int[7];

    for (int i=0;i<matrix.length;i++){          
        for (int j=0; j<matrix[i].length;j++)     
            nums[matrix[i][j]] += 1;                 
    }
    return nums;
}

public static int [] addRolls(int [][] matrix){
    int total= 0;
    int[]nums=new int[13];

    //Adds the rows together.
    for (int i=0;i<matrix.length;i++){
        total=0;
        for (int j=0;j<matrix[i].length;j++)   
            total+=matrix[i][j];  
    }
    return nums;    
}

}
+1  A: 
    //Adds the rows together. 
    for (int i=0;i<matrix.length;i++){ 
        total=0; 
        for (int j=0;j<matrix[i].length;j++)    
            total+=matrix[i][j]; 
        nums[total]++;
    } 

have you tried this?

        for (int i=1;i<roll.length;i++){ 
            String star=starPrint(roll[i]); 
            System.out.print("\n" + i+ " : " +roll[i] + " " + star); 
        } 
st0le
YUP! That was it. Thanks a bunch. God... I didn't know I was so close. :((((( I'm retarded. Thanks again st0le.
S.Jaub
A: 

It doesn't look like you're doing anything to int[] nums in addRolls. Perhaps you want an nums[index]++; somewhere?


On using foreach loop

In most of your loops, you don't actually need an explicit index. In these cases, it'd be much more readable to use a "for-each" loop instead. Here's an example:

    int[] nums = { 1, 3, 5 };
    int sum = 0;
    for (int num : nums) {
        sum += num;
    }
    System.out.println(sum); // prints "9"

And for a 2-dimensional array (or rather, an array of arrays):

    int[][] mat = {
        { 1, 2, 3 },
        { 4, 5 },
        { 3, 3, 3 }
    };
    for (int[] row : mat) {
        int prod = 1;
        for (int num : row) {
            prod *= num;
        }
        System.out.println(prod);
    } // prints "6", "20", "27"

References


Minor points

Instead of writing

variable += 1;

You can just write:

variable++;

Also, please pay attention to indentation. Your indentation is highly inconsistent.

Also, instead of using Math.random() and then doing funky formulas, you can also instantiate a java.util.Random once and call its int nextInt(int n) method in your application.

See also

polygenelubricants