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 :(得分:0)
不,你做错了。做这些步骤。
1&GT;不要在每种情况下创建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&GT;由于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;