java pacman - 循环只通过数组迭代一次

时间:2016-10-09 00:03:35

标签: arrays pacman

我的程序输出有问题。我能够生成网格并使pacman重新定向并移动,但它只移出第一个位置,然后停止定向或移动。程序重申的方式肯定有问题,但我不确定是什么。

package javaapplication2;
import java.util.*;

public class JavaApplication2 {
    int index;
    int index2;
    final int ARRAY_SIZE = 10;
    final int ARRAY_SIZE2 = 10;
    String [][] myArray = new String [ARRAY_SIZE][ARRAY_SIZE2];
    String output;

public static void main(String[] args) 
{

     JavaApplication2 pacman = new JavaApplication2();
     pacman.createPacman();
     pacman.generateArray();
     Scanner input = new Scanner( System.in );
     int m = input.nextInt();
     pacman.orientPacman(m);



}
public void generateArray(){


    for (index = 0; index < ARRAY_SIZE; index++)
    {
        for (index2 = 0; index2 < ARRAY_SIZE2; index2++)
        {
            Random rand = new Random(); 
            int value = rand.nextInt(50);
            if (myArray[index][index2] == "<")
                myArray[index][index2] = "<";
            else if (myArray[index][index2] == " ")
                myArray[index][index2] = " ";
            else if (value < 45) 
                myArray[index][index2] = ".";
            else myArray[index][index2] = "0";
            System.out.print("   " + myArray[index][index2]);
                  if (index2 + 1 == ARRAY_SIZE)
                  {
    System.out.println();
                  }



}}
}


    public void printArray(String myArray[][]){
                for (index = 0; index < ARRAY_SIZE; index++)
    {
        for (index2 = 0; index2 < ARRAY_SIZE2; index2++)
        {
            System.out.print("   " + myArray[index][index2]);
                  if (index2 + 1 == ARRAY_SIZE)
                  {
    System.out.println();
                  }
    }}
    Scanner input = new Scanner( System.in );
    int m = input.nextInt();
    orientPacman(m);}

    public void createPacman(){
        myArray[0][0] = "<";



    }

    public void orientPacman(int m){
       for (index = 0; index < ARRAY_SIZE; index++)
    {
        for (index2 = 0; index2 < ARRAY_SIZE2; index2++)
        {
   if ((myArray[index][index2] == "<" && m == 1) | 
   (myArray[index][index2] == ">" && m == 2) |
   (myArray[index][index2] == "^" && m == 3) |   
   (myArray[index][index2] == "v" && m == 4)   )
   {
   movePacman();
   break;
   }
    else if (myArray[index][index2] == ">" | myArray[index][index2] == "^"     | myArray[index][index2] == "v" && m == 1)
   myArray[index][index2] = "<";
    else if (myArray[index][index2] == "<" | myArray[index][index2] == "^" | myArray[index][index2] == "v" && m == 2)
   myArray[index][index2] = ">";
    else if (myArray[index][index2] == "<" | myArray[index][index2] == ">" | myArray[index][index2] == "v" && m == 3)
   myArray[index][index2] = "^";
    else if (myArray[index][index2] == "<" | myArray[index][index2] == ">" | myArray[index][index2] == "^" && m == 4)
   myArray[index][index2] = "v";
  printArray(myArray);

        }   

    }}
    public void movePacman(){
       for (index = 0; index < ARRAY_SIZE; index++)
    {
        for (index2 = 0; index2 < ARRAY_SIZE2; index2++)
        {
   if (myArray[index][index2] == "<")
   {
   myArray[index][index2+1] = "<";
   myArray[index][index2] = " ";
   }
    if (myArray[index][index2] == ">")
   {
   myArray[index][index2-1] = ">";
   myArray[index][index2] = " ";
   }
    if (myArray[index][index2] == "^")
   {
   myArray[index+1][index2] = "^";
   myArray[index][index2] = " ";
  }
        if (myArray[index][index2] == "v")
   {
   myArray[index-1][index2] = "v";
   myArray[index][index2] = " ";
   }
   printArray(myArray);


}}}}

1 个答案:

答案 0 :(得分:0)

检查你的break语句是否在第一次迭代中执行。

如果数组为空,则检查数组的第二个索引和第三个索引值。