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:
Code :
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
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.
Re: logic error: cpu assigning incorrect values in for loop
Quote:
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.
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:
Code :
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.
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:
Code :
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:
Code java:
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.
Re: logic error: cpu assigning incorrect values in for loop
haha, i can't believe I forgot about else if. thanks aussiemcgr