2014-10-15 2 views
0
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(); 
     } 

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

     switch (s[i]){ 

     case 1: 
      int j; 
      for(j=0;j<6;j++){ 
      char a[] = {'A','B','C','D','E','F'}; 
      System.out.println(""+'\t'+a[j]); 
      } 
      break; 
     case 2: 
      for(j=0;j<6;j++){ 
      char a[] = {'G','H','I','J','K','L'}; 
      System.out.println(""+'\t'+a[j]); 
      } 
      break; 
     case 3: 
      for(j=0;j<6;j++){ 
      char a[] = {'M','N','O','P','Q','R'}; 
      System.out.println(""+'\t'+a[j]); 
      } 
      break; 
     case 4: 
      for(j=0;j<6;j++){ 
      char a[] = {'S','T','U','V','W','X'}; 
      System.out.println(""+'\t'+a[j]); 
      } 
      break; 
     case 5: 
      for(j=0;j<6;j++){ 
      char a[] = {'Y','Z'}; 
      System.out.println(""+'\t'+a[j]); 
      } 
      break; 

     } 
     System.out.println(""); 


     } 



    } 


} 

выход:Как напечатать несколько значений массива паралельно

      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 
T 
U 
V 
W 
X 

G 
H 
I 
J 
K 
L 

M 
N 
O 
P 
Q 
R 

, но мне нужно напечатать, как

S G M 
T H N 
U I O 
V J P 
W K Q 
X L R 

ответ

0

Нет, вы делаете это неправильно. Сделайте эти шаги.

1> Вместо создания char a[] в каждом случае, сделать это за пределами switch блока:

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'}; 

2>

for(int i = 0; i < 6; i++) { // Since 6 characters in an array. 

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

     // Print first characters from each required array, then second characters on a new line, then third and so on. 
     switch(sc[j] + 1) { // 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: System.out.print("\t"+e[i]); break; // Prone to ArrayIndexOutOfBoundsException 
     } 
    } 
    System.out.println(); // Print a newline character after printing every line 
} 

3> Поскольку case 5 склонен к ArrayIndexOutOfBounds, положить его в оператор try-catch. (Необязательно)

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; 
+0

Значения массива печатаются параллельно, как и ожидалось, теперь мне нужно хранить значения каждой строки в отдельном массиве. Можете ли вы помочь мне в этом. Заранее спасибо, – user2322440

Смежные вопросы