Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Constructor doesn't fill in array properly

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Constructor doesn't fill in array properly

    So, in my constructor (where toWound and toHit are instance variables, declared beforehand):
    public Mathhammer()
        {
            toWound = new double[10][10];
            toHit = new double[10][10];
            for (int S=0;S<10;S++) {
                for (int T=0;T<10;T++) {
                    if (S == T)  toWound[S][T] = 0.5;
                    if (S < (T-3))  toWound[S][T] = 0;
                    if (S > (T+1))  toWound[S][T] = (5/6); 
                    if (S == (T+1))  toWound[S][T] = (2/3); 
                    if (S == (T-1))  toWound[S][T] = (1/3); 
                    if (S == (T-3) || S == (T-2))  toWound[S][T] = (1/6); 
                }
                for (int A=0; A<10;A++) {
                    for (int D=0;D<10;D++) {
                        if (A == D || A == D-1) { toHit[A][D] = 0.5; }
                        if (A > D) { toHit[A][D] = (2/3); }
                        if (A < D-1){ toHit[A][D] = (1/3); }
                    }
                }
            }
        }
    only the values for where S = T are filled in, the rest stay as 0.0

    Can anyone help me?
    Last edited by Whyareall; November 6th, 2010 at 04:13 AM.


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Constructor doesn't fill in array properly

    Never mind... just needed to cast to doubles.

Similar Threads

  1. Object array with constructor
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2010, 12:02 PM
  2. Fill & sort an array
    By yroll in forum Collections and Generics
    Replies: 2
    Last Post: April 1st, 2010, 07:00 AM
  3. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 04:26 PM
  4. Fill array concurrently
    By mamba in forum Collections and Generics
    Replies: 2
    Last Post: October 15th, 2009, 08:08 PM
  5. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 07:08 PM