迭代数组并替换字符

时间:2014-10-25 19:18:37

标签: java arrays grid

我有一个包含破折号和点的数组。我们的目标是开发激光器#34;在用户决定的某个位置进入该数组。在这种情况下,第5行是他们拍摄的地方。我现在需要遍历该行并将所有点更改为破折号( - )或(|),具体取决于它是从东到西还是从北到南。当它击中破折号时,它应该充当镜子并反射并继续向另一个方向移动。任何人都可以帮助循环来做到这一点?我一直把破折号放在错误的地方。任何帮助表示赞赏。

0 . . . . . / . . . \ 
1 . . \ . . . . . . . 
2 . . . . . . . / . . 
3 . . . . . . . . . . 
4 . . . . . . . . \ . 
5>. . . . . . . . . . 
6 . . . . . . . . . . 
7 . . . . . \ . . / . 
8 . . \ . . . . . . / 
9 . . . . . . . . . . 

可能的最终

0 . . . . . / - - - \ 
1 - - \ . . | . . . |
2 . . | . . | . / . | 
3 . . | . . | . . . | 
4>- - - - - - - - \ | 
5 . . | . . | . . | |
6 . . | . . | . . | | 
7 . . | . . \ - - / | 
8 . . \ - - - - - - / 
9 . . . . . . . . . . 

基本上,这可能是激光射入第4行后网格的结束

2 个答案:

答案 0 :(得分:0)

你可以使用一个无限循环来完成它,一旦你的激光因任何原因停止而中断。使用变量作为方向,你的位置和你的下一个位置,你可以编写代码来检查你前进方向的下一个位置,然后沿着那个方向前进,直到你遇到一个/或\,或者即将离开界限。 代码将是这种效果(为了清楚起见,将x和y值分开):

char lasermap[][] = /* your laser grid */
int x_direction = 0; // UP = 1, NONE = 0, DOWN = -1
int y_direction = 0; // RIGHT = 1 NONE = 0, LEFT = -1
int x = 0;
int y = 0;
int nextx = 0;
int nexty = 0;
for (;;) {
    nextx = x + x_direction;
    nexty = y + y_direction;
    if (nextx < 0 || nexty < 0) {
        break;
    } else if (lasermap[nextx][nexty] == '\\') {
        if (x_direction == -1) {
            x_direction = 0;
            y_direction = 1;
        } else if (x_direction == 1) {
            x_direction = 0;
            y_direction = -1;
        } else if (y_direction == -1) {
            x_direction = 1;
            y_direction = 0;
        }
        else if (y_direction == 1) {
            x_direction = -1;
            y_direction = 0;
        }
    } else if (lasermap[nextx][nexty] == '/') {
        if (x_direction == -1) {
            x_direction = 0;
            y_direction = -1;
        } else if (x_direction == 1) {
            x_direction = 0;
            y_direction = 1;
        } else if (y_direction == -1) {
            x_direction = -1;
            y_direction = 0;
        } else if (y_direction == 1) {
            x_direction = 1;
            y_direction = 0;
        }
    }
    x = nextx;
    y = nexty;
}

答案 1 :(得分:0)

import java.util.*;
class Try
{
    public static void main(String args[])
    {
        int n;
        Scanner s = new Scanner(System.in); // input 
        n = s.nextInt();
        String s1[] = new String[n+1]; // input strings
        String s2[] = new String[n+1]; // output strings
        for(int i = 0;i<n + 1;i++)
        {
            s1[i] = new String();
            s1[i] = s.nextLine();
        }

        for(int i = 0;i<n + 1;i++)
        {
            char[] temp = s1[i].toCharArray(); // getting indivdual characters
            for(int j = 0;j<temp.length;j++)  
            {
                if(temp[j] == '>')   // if laser found
                {
                    for(int k = j;k < temp.length && temp[k] != '\\'&& temp[k] != '/';k++)
                    {
                        temp[k] = '-';   // getting dash
                    }
                }

                if(temp[j] == '<')
                {
                    for(int k = j;k >= 0 && temp[k] != '\\'&& temp[k] != '/';k--)
                    {
                        temp[k] = '|'; // getting pipes
                    }
                }
            }
            s2[i] = new String(temp);
        }

        for(int i = 0;i<n+1;i++)
        {
            System.out.println(s2[i]); // printing result
        }
    }
}

这可以帮助你

相关问题