确定2D字符数组中行/列的ASCII值

时间:2014-01-20 21:00:29

标签: java for-loop multidimensional-array

试图弄清楚如何计算2D数组中一行中ASCII字符的总值。根据我的理解,我必须创建一个for循环来遍历数组中的每个实例,但我不确定如何去做。给我的另一个解决方案是使用HashSet,但我很担心使用HashSet,因为我没有经验。我尝试过的测试实验经历了数组中的每个字符并将它们全部添加起来,当然不是很有效。

import java.util.Scanner;

class SmartPuzzleProgram{
  public static void main(String[] args){
    char[][] table = new char[5][5];
    table[0][0] = 'S';
    table[0][1] = 'M';
    table[0][2] = 'A';
    table[0][3] = 'R';
    table[0][4] = 'T';
    table[1][1] = 'T';
    table[1][2] = 'S';
    table[1][3] = 'M';
    table[2][2] = 'R';
    table[2][4] = 'S';
    table[3][1] = 'S';
    table[3][2] = 'M';
    table[4][2] = 'T';
    table[4][3] = 'S';

    int rows = 5;
    int columns = 5;    

    for(int k = 0; k>=0; k++){

      System.out.println("   | 1 | 2 | 3 | 4 | 5 |");
      System.out.println("---+---+---+---+---+---+");
      System.out.println(" 1 | "+table[0][0]+" | "+table[0][1]+" | "+table[0][2]+" |   "+table[0][3]+" | "+table[0][4]+" |");
      System.out.println("---+---+---+---+---+---+");
      System.out.println(" 2 | "+table[1][0]+" | "+table[1][1]+" | "+table[1][2]+" | "+table[1][3]+" | "+table[1][4]+" |");
      System.out.println("---+---+---+---+---+---+");
      System.out.println(" 3 | "+table[2][0]+" | "+table[2][1]+" | "+table[2][2]+" | "+table[2][3]+" | "+table[2][4]+" |");
      System.out.println("---+---+---+---+---+---+");
      System.out.println(" 4 | "+table[3][0]+" | "+table[3][1]+" | "+table[3][2]+" | "+table[3][3]+" | "+table[3][4]+" |");
      System.out.println("---+---+---+---+---+---+");
      System.out.println(" 5 | "+table[4][0]+" | "+table[4][1]+" | "+table[4][2]+" | "+table[4][3]+" | "+table[4][4]+" |");
      System.out.println("---+---+---+---+---+---+");

      System.out.println("Enter a row (1-5): ");
      Scanner scRow = new Scanner(System.in);
      int m = scRow.nextInt();

      System.out.println("Enter a column (1-5): ");
      Scanner scCol = new Scanner(System.in);
      int n = scCol.nextInt();

      System.out.println("Enter a letter (S, M, A, R or T): ");
      Scanner scChar = new Scanner(System.in);
      char c = scChar.next().charAt(0);

      if (m < 1 || m > 5){
        System.out.println("Invalid: Enter a valid row. ");
      }
      else if (n < 1 || n > 5){
        System.out.println("Invalid: Enter a valid column. ");
      }
      else if (c != 'S' && c != 'M' && c != 'A' && c != 'R' && c != 'T'){
        System.out.println("Invalid: Enter a valid character. ");
      }
      else{
        table[(m-1)][(n-1)] = c;
      }
     //if ((int)table[0][] == 391 && (int)table[1][] == 391 && (int)table[2][] == 391 && (int)table[3][] == 391 && (int)table[4][] == 391 
     //   && (int)table[][0] == 391 && (int)table[][1] == 391 && (int)table[][2] == 391 && (int)table[][3] == 391 && (int)table[][4] == 391){
     //  System.out.println("Congrats! You win!");
     //}
    }
  }
}

应该发生的是,一旦用户获得了S M A R和T在5个字符的行中覆盖整个5x5阵列,游戏就会宣告用户为胜利者。这可以通过添加ASCII值来计算,这些值最终在任何方向上等于391。如果有人有一个最好是for循环的解决方案,那就太好了。

1 个答案:

答案 0 :(得分:0)

如果您只需要查看他们刚刚输入信件的行,请尝试以下操作:

int total = 0;
for (int column = 0; column < 5; column++)
{
    total += table[m-1][column];
}

if (total == 391)
{
    // you won!
}

如果通过“在任何方向上等于391”,你的意思是这些字母也可以出现在列或对角线中,那么你也需要遍历这些字母。你能说清楚这是不是你的意思吗?

另外,S,M,A,R,T不会是五个字母的唯一组合,最多可以加391,但是你只是在这五个字母上过滤输入,所以你应该没问题。

包含所有行,从而包含预设值:

for (int row = 0; row < 5; row++)
{
    int total = 0;  // reset the total on each row

    // total the cells on the current row
    for (int column = 0; column < 5; column++)
    {
        total += table[row][column];
    }

    if (total == 391)
    {
        // you won!
        break;  // no need to continue calculating the rest of the rows
    }
}

要按列检查,只需交换'for'循环:

for (int column = 0; column < 5; column++)
{
    int total = 0;  // reset the total on each row

    // total the cells on the current column
    for (int row = 0; row < 5; row++)
    {
        total += table[row][column];
    }

    if (total == 391)
    {
        // you won!
        break;  // no need to continue calculating the rest of the rows
    }
}

检查对角线:

int total = 0;

// total the cells on the current row
for (int rowcolumn = 0; rowcolumn < 5; rowcolumn++)
{
    total += table[rowcolumn][rowcolumn];  // notice that the both indices are identical
}

if (total == 391)
{
    // you won!
}