开发者

Nested While Loops and Duplicate results?

Before you say this is old question, I spent 24 hours trying to solve and read everything. I swear.

The code is supposed to receive one system id and then scan around that system in all directions, with each direction having its own length. It is like scanning a rectangle around that system where 9*9 = 81 systems.

The code works very well; but the problem. I get 8 duplicate results and there is a complete row missing from the results. The one just above the middle row. I think the problem is in the topsystem integer but I don't know what is wrong with it.

here is the code:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" verticalAlign="middle">


<mx:Script>
    <![CDATA[


        private function startscan():void {
            var startsystem:int = int(system.text);
            var resultsys:String = systemsaround.text;
            var leftsystem:int = -4;
            var rightsystem:int = 4;
            var topsystem:int = -400;
            var downsystem:int = 400;
            var mediumsys:int = 0;
            var tempdownsystem:int = 0;
            var temptopsystem:int = 0;


            if(startsystem + downsystem > 10000){
                downsystem = 400 -((startsystem + downsystem) - 10000);
                downsystem = Math.floor(downsystem/100)*100;
                topsystem = topsystem - (400 - downsystem);
            }
            if(startsystem + topsystem < 1){
                topsystem = -400 + (1+(-1*(startsystem + topsystem)));
                topsystem = Math.ceil(topsystem/100)*100;
                downsystem = downsystem + (400 - (topsystem * -1));
            }
            if(startsystem + leftsystem < 1){
                leftsystem = -4 + (1+(-1*(startsystem + leftsystem)));
                rightsystem = rightsystem + (4 - (leftsystem * -1));
            }
            if(startsystem + rightsystem > 10000){
                rightsystem = 4 -((startsystem + rightsystem) - 10000);
                leftsystem = leftsystem - (4 - rightsystem);
            }

            resultsys = systemsaround.text;
            systemsaround.text = resultsys + "down" + downsystem + "top" + topsystem + "left" + leftsystem + "right" + rightsystem + ":" + startsystem + "\r";
            resultsys = systemsaround.text;

            while (rightsystem > 0) {
            resultsys = systemsaround.text;
            mediumsys = startsystem + rightsystem;
            systemsaround.text = resultsys + mediumsys + "\r";
            resultsys = systemsaround.text;
            tempdownsystem = downsystem;
            temptopsystem = topsystem;
            rightsystem--;

                        while (downsystem > 0) {
                            resultsys = systemsaround.text;
                            mediumsys = mediumsys + downsystem;
                            systemsaround.text = resultsys + mediumsys + "\r";
                            resultsys = systemsaround.text;

                            downsystem = downsystem - 100;


                        }
                        while (topsystem < 0) {
                            resultsys = systemsaround.text;
                            mediumsys = mediumsys + topsystem;
                            systemsaround.text = resultsys + mediumsys + "\r";
                            resultsys = systemsaround.开发者_Go百科text;

                            topsystem = topsystem + 100;


                        }
            downsystem = tempdownsystem;        
            topsystem = temptopsystem;


            }





            while (leftsystem < 0) {
            resultsys = systemsaround.text;
            mediumsys = startsystem + leftsystem;
            systemsaround.text = resultsys + mediumsys + "\r";
            resultsys = systemsaround.text;
            tempdownsystem = downsystem;
            temptopsystem = topsystem;
            leftsystem++;
                        while (downsystem > 0) {
                            resultsys = systemsaround.text;
                            mediumsys = mediumsys + downsystem;
                            systemsaround.text = resultsys + mediumsys + "\r";
                            resultsys = systemsaround.text;

                            downsystem = downsystem - 100;


                        }
                        while (topsystem < 0) {
                            resultsys = systemsaround.text;
                            mediumsys = mediumsys + topsystem;
                            systemsaround.text = resultsys + mediumsys + "\r";
                            resultsys = systemsaround.text;

                            topsystem = topsystem + 100;


                        }
            downsystem = tempdownsystem;        
            topsystem = temptopsystem;  



            }

                        while (downsystem > 0) {
                            resultsys = systemsaround.text;
                            mediumsys = startsystem + downsystem;
                            systemsaround.text = resultsys + mediumsys + "\r";
                            resultsys = systemsaround.text;

                            downsystem = downsystem - 100;


                        }
                        while (topsystem < 0) {
                            resultsys = systemsaround.text;
                            mediumsys = startsystem + topsystem;
                            systemsaround.text = resultsys + mediumsys + "\r";
                            resultsys = systemsaround.text;

                            topsystem = topsystem + 100;


                        }






        }
    ]]>
</mx:Script>


    <mx:VBox width="488" height="304" horizontalCenter="0" verticalCenter="-16" horizontalAlign="center" verticalAlign="middle">
        <mx:TextInput id="system" text="3375"/>
        <mx:TextArea width="80%" height="228" fontSize="10" id="systemsaround"/>
        <mx:Button label="start"  click="startscan()" id="start"/>
    </mx:VBox>

</mx:WindowedApplication>

if you supply the starting system as 3375, you get 81 results with those 8 duplicates:

  • 3379 3378 3377 3376 3371 3372 3373 3374

and the missing numbers are (8 is missing):

  • 3279 3278 3277 3276 3274 3273 3272 3271

I hope someone can point me in the right direction. Thanks.

update:

if you watch the part of the code that has if statements. this part handle the edges.specially zero and 10k. about what i want to do. think about it like a 9*9 dots field in rectangular shape. has 9 rows and 9 columns. each dot has an id from 1 to 10k. the input is the id of the dot in the center of the field (column 5, row 5). as now i know this centeral dot id. i want to loop through the whole field to find the other dots ids using topsystem(the distance between the centeral dot to the upper edge).downsystem (the distance between central dot to the lower edge).leftsystem (the distance between central dot and left edge).rightsystem (the distance between central dot and right edge).

the code is working fine but there is 8 missing ids that replaced by 8 duplicated ids. i dont know where they come from.


I am not sure what you are trying to do, so let me explain what I think you are trying to do.

You have 10000 "systems" arranged in a 100x100 grid. Given a system number, you want to output all 81 system numbers around it. Except on the edges of the grid, you don't want to output those.

In that case, delete all of your code and try this instead:

var x = int(system.text)%100;
var y = Math.floor(int(system.text)/100);
for(var dx=-4; dx <= 4; dx++)
for(var dy=-4; dy <= 4; dy++) 
  if(0 <= x+dx && x+dx <= 100 && 0 <= y+dy && y+dy <= 100)
    systemsarround.text += x+dx + (y+dy)*100 + "\r";

This is untested, but you get the idea.

Another attempt:

var x = int(system.text)%100;
var y = Math.floor(int(system.text)/100);
for(var dx=-4; dx <= 4; dx++)
for(var dy=-4; dy <= 4; dy++){
  var sys = x+dx + (y+dy)*100;
  if(1 <= sys && sys <= 10000)
    systemsarround.text += sys + "\r";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜