如何将少数数组的相同索引值存储到单个数组中

时间:2014-10-19 14:53:03

标签: java arrays

import java.util.Scanner;


public class namefinder {


public static void main(String[] args) {
    System.out.println("                        NAME PREDICTER        ");

    System.out.println(" Row 1  A B C D E F");
    System.out.println(" Row 2  G H I J K L");
    System.out.println(" Row 3  M N O P Q R");
    System.out.println(" Row 4  S T U V W X");
    System.out.println(" Row 5  Y Z . . . .");
    System.out.println("enter the length of your first name in number!!! EX: vino , so length is 4");
    Scanner scanner = new Scanner(System.in);

    int y =  scanner.nextInt();
    int s[] =  new int [20];


    for(int i=1;i<=y;i++)
    {
    System.out.println("Enter whether the "+i+"letter of your name is in which row");
    Scanner scanner1 = new Scanner(System.in);

    s[i] =  scanner1.nextInt();
    }

    char a[] = {'A','B','C','D','E','F'};
    char b[] = {'G','H','I','J','K','L'};
    char c[] = {'M','N','O','P','Q','R'};
    char d[] = {'S','T','U','V','W','X'};
    char e[] = {'Y','Z','.','.','.','.'};


  for(int i=0;i<6;i++){
      for(int j = 0; j <=y; j++) {


          switch(s[j] ) {  // Since counting starts from 0.

          case 1: System.out.print("\t"+a[i]);break;
          case 2: System.out.print("\t"+b[i]); break;
          case 3: System.out.print("\t"+c[i]); break;
          case 4: System.out.print("\t"+d[i]); break;
          case 5: try {

              System.out.print("\t"+e[i]);
          } catch(ArrayIndexOutOfBoundsException ex) {

              System.out.println("\t "); // Print an empty space with a \t character. 
          }

          break; // Prone to ArrayIndexOutOfBoundsException
                    }
  }
  System.out.println(" "+"Row"+ (i+1)); // Print a newline character after printing every line
}  

output:
                    NAME PREDICTER        
Row 1  A B C D E F
Row 2  G H I J K L
Row 3  M N O P Q R
Row 4  S T U V W X
Row 5  Y Z . . . .
enter the length of your first name in number!!! EX: vino , so length is 4
3
Enter whether the 1letter of your name is in which row
4
Enter whether the 2letter of your name is in which row
2
Enter whether the 3letter of your name is in which row
3
S   G   M Row1
T   H   N Row2
U   I   O Row3
V   J   P Row4
W   K   Q Row5
X   L   R Row6

需要帮助:

现在我需要将所有第1行的字母存储到单独的数组中。 即row1 [] = {S,G,M}和row2 [] = {T,H,N}等等...........请给出一些实施建议

1 个答案:

答案 0 :(得分:0)

如果我理解了您的问题,那么一种可能的解决方案是用一维二维替换您的abcde数组阵列。此外,您只需要一个Scanner。您可以使用Arrays.copyOf(char[],int)Arrays.deepToString(Object[])之类的

Scanner scanner = new Scanner(System.in);
int y = scanner.nextInt();
char[][] rows = { { 'A', 'B', 'C', 'D', 'E', 'F' },
            { 'G', 'H', 'I', 'J', 'K', 'L' },
            { 'M', 'N', 'O', 'P', 'Q', 'R' },
            { 'S', 'T', 'U', 'V', 'W', 'X' },
            { 'Y', 'Z', '.', '.', '.', '.' } };
char[][] out = new char[3][y];
for (int i = 0; i < y; i++) {
    System.out.println("Enter the row the " + (1 + i)
            + " letter is on:");
    int row = scanner.nextInt();
    out[i] = Arrays.copyOf(rows[row-1], rows[row-1].length);
}
System.out.println(Arrays.deepToString(out));