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 6 of 6

Thread: logic error: cpu assigning incorrect values in for loop

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default logic error: cpu assigning incorrect values in for loop

    Hello, I have no idea why this is happening, but the computer is assigning incorrect values in my loop:

    byte ran;
    		for(short y=0;y<yt;y++){
    			for(short x=0;x<xt;x++){
    				//
    				if(y==0&&x==0){
    					ran=(byte)(6*Math.random());
    					System.out.println("ran: "+ran);
    					if(ran==0)ta[0][0]=0;
    					if(ran==1)ta[0][0]=12;
    					if(ran==2)ta[0][0]=51;
    					else ta[0][0]=30;
    				}
    				//
    				if(y==0&&x>0){
    					if(ta[0][x-1]==1||ta[0][x-1]==13||ta[0][x-1]==30||ta[0][x-1]==50){//s
    						ran=(byte)(6*Math.random());
    						System.out.println("ran: "+ran);
    						if(ran==0)ta[0][x]=0;
    						if(ran==1)ta[0][x]=12;
    						if(ran==2)ta[0][x]=51;
    						else ta[0][x]=30;
    						System.out.println("ta["+y+"]["+x+"]="+ta[0][x]);
    					}
    					if(ta[0][x-1]==0||ta[0][x-1]==5||ta[0][x-1]==10){//s-hm
    						ran=(byte)(5*Math.random());
    						System.out.println("ran: "+ran);
    						if(ran==0)ta[0][x]=1;
    						if(ran==1)ta[0][x]=4;
    						else ta[0][x]=10;
    						System.out.println("ta["+y+"]["+x+"]="+ta[0][x]);
    					}
    					if(ta[0][x-1]==7||ta[0][x-1]==11||ta[0][x-1]==44){//hm-s
    						ran=(byte)(5*Math.random());
    						System.out.println("ran: "+ran);
    						if(ran==0)ta[0][x]=6;
    						if(ran==1)ta[0][x]=45;
    						else ta[0][x]=11;
    						System.out.println("ta["+y+"]["+x+"]="+ta[0][x]);
    					}
    					if(ta[0][x-1]==4||ta[0][x-1]==8||ta[0][x-1]==12){//udm
    						ran=(byte)(4*Math.random());
    						System.out.println("ran: "+ran);
    						if(ran==0)ta[0][x]=7;
    						if(ran==1)ta[0][x]=9;
    						else ta[0][x]=34;
    						System.out.println("ta["+y+"]["+x+"]="+ta[0][x]);
    					}
    					if(ta[0][x-1]==6||ta[0][x-1]==9||ta[0][x-1]==35){//ddm
    						ran=(byte)(4*Math.random());
    						System.out.println("ran: "+ran);
    						if(ran==0)ta[0][x]=5;
    						if(ran==1)ta[0][x]=8;
    						else ta[0][x]=13;
    						System.out.println("ta["+y+"]["+x+"]="+ta[0][x]);
    					}
    					if(ta[0][x-1]==33||ta[0][x-1]==34||ta[0][x-1]==45){//hm
    						ran=(byte)(5*Math.random());
    						System.out.println("ran: "+ran);
    						if(ran==0)ta[0][x]=35;
    						if(ran==1)ta[0][x]=44;
    						else ta[0][x]=33;
    						System.out.println("ta["+y+"]["+x+"]="+ta[0][x]);
    					}
    					if(ta[0][x-1]==31||ta[0][x-1]==51){//valley m
    						ran=(byte)(2*Math.random());
    						System.out.println("ran: "+ran);
    						if(ran==0)ta[0][x]=50;
    						else ta[0][x]=31;
    						System.out.println("ta["+y+"]["+x+"]="+ta[0][x]);
    					}
    				}
    }
    The output for this code is:

    x?
    10
    y?
    5
    ran: 4
    ran: 2
    ta[0][1]=51
    ran: 0
    ta[0][2]=50
    ran: 1
    ta[0][3]=30
    ran: 2
    ta[0][4]=51
    ran: 1
    ta[0][5]=31
    ran: 1
    ta[0][6]=31
    ran: 0
    ta[0][7]=50
    ran: 0
    ta[0][8]=30
    ran: 4
    ta[0][9]=30
    Press any key to continue...

    As you can see, there are 2 errors:
    ta[0][3] should = 12
    ta[0][8] should = 0


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: logic error: cpu assigning incorrect values in for loop

    You'll have to give us an idea of what the code is doing on a higher level, not just the expected output, otherwise there's very little chance for us to help you identify the problem.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: logic error: cpu assigning incorrect values in for loop

    As you can see, there are 2 errors:
    ta[0][3] should = 12
    ta[0][8] should = 0
    How can we tell there are errors? Why do you think there are errors?

    Normally the computer does EXACTLY what the programmer tells it to do in his/her program.
    99.999% of the time when the programmer thinks the computer is wrong, it's not.
    Computers do what the programmer tells it to do, not what the programmer wants it to do.
    Last edited by Norm; July 25th, 2010 at 08:48 PM.

  4. #4
    Junior Member
    Join Date
    Jun 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: logic error: cpu assigning incorrect values in for loop

    First of all, ta[0][0]=30. I didn't add a system out println to that one.

    But you can tell there's an error even if you don't know what the code is about. I'll explain:
    ta[0][3] should not = 30, because:
    ta[0][2] = 50 (which is correct, the first 3 indexes (0,1,2) are all correct). And since ta[0][2] = 50 then, the condition for the next x value (3) in ta[0][3] should be determined by ta[0][x-1] (which is ta[0][2], which is 50).
    So therefore, the condition that is chosen is:
    if(ta[0][x-1]==1||ta[0][x-1]==13||ta[0][x-1]==30||[B]ta[0][x-1]==50[/B]){//s
    						ran=(byte)(6*Math.random());
    						System.out.println("ran: "+ran);
    						if(ran==0)ta[0][x]=0;
    						[B]if(ran==1)ta[0][x]=12;[/B]
    						if(ran==2)ta[0][x]=51;
    						else ta[0][x]=30;
    						System.out.println("ta["+y+"]["+x+"]="+ta[0][x]);
    					}
    since that is the condition that is dependent on if ta[0][2]=50. So it makes the random number, and ran = 1, so therefore ta[0][3] = 12. Instead, ta[0][3] = 30, as if ran = 3, 4, or 5.

    The same type of scenario happens again for ta[0][8] relative to ta[0][7]. The pattern I have noticed is that only 51, 50, or 30 end up being chosen as values for the array. I even changed the random chance so they would be much less likely values, and still they get picked.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,696
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: logic error: cpu assigning incorrect values in for loop

    not sure if this is specifically your error, but I found what would cause this. You have messed up your conditional statements here:
    if(y==0&&x>0){
    	if(ta[0][x-1]==1||ta[0][x-1]==13||ta[0][x-1]==30||ta[0][x-1]==50){//s
    		ran=(byte)(6*Math.random());
    		System.out.println("ran: "+ran);
    		[B]if(ran==0)ta[0][x]=0;
    		if(ran==1)ta[0][x]=12;
    		if(ran==2)ta[0][x]=51;
    		else ta[0][x]=30;[/B]
    		System.out.println("ta["+y+"]["+x+"]="+ta[0][x]);
    	}
    ...}

    Ok, so what is happening here is that ran does equal 1 during ta[0][3] should = 12. BUT, you havent done any else if statements, so when it reaches if(ran==2)ta[0][x]=51;, it returns false and instead does the else: else ta[0][x]=30;, overriding the value and setting it to 30.

    What you want in place of those if statements is else if statements. Here is what it should be replaced by:
    if(ran==0)
         ta[0][x]=0;
    else if(ran==1)
         ta[0][x]=12;
    else if(ran==2)
         ta[0][x]=51;
    else 
         ta[0][x]=30;

    This setup will stop the value from being overwritten by else statement in the last check. If you have any questions, I'll try to answer them.
    Last edited by aussiemcgr; July 25th, 2010 at 09:17 PM.

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Perd1t1on (July 25th, 2010)

  7. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: logic error: cpu assigning incorrect values in for loop

    haha, i can't believe I forgot about else if. thanks aussiemcgr

Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 03:09 AM
  2. help with the logic on this letter grade program.
    By etidd in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 09:14 PM
  3. Simple recursion logic
    By chronoz13 in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 24th, 2009, 10:53 PM
  4. Swing incorrect behaviour from JRE5 to JRE6
    By singhkanhaiya in forum AWT / Java Swing
    Replies: 1
    Last Post: August 25th, 2009, 02:23 AM
  5. Assigning variable to an Array
    By sridevi in forum Collections and Generics
    Replies: 3
    Last Post: August 10th, 2009, 11:58 PM