java - How to print the few array values parallely -
import java.util.scanner; public class namefinder { public static void main(string[] args) { system.out.println(" name predicter "); system.out.println(" row 1 b c d e f"); system.out.println(" row 2 g h 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 length of first name in number!!! ex: vino , length 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 "+i+"letter of name in 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(""); } } }
output:
name predicter row 1 b c d e f row 2 g h j k l row 3 m n o p q r row 4 s t u v w x row 5 y z come in length of first name in number!!! ex: vino , length 4 3 come in whether 1letter of name in row 4 come in whether 2letter of name in row 2 come in whether 3letter of name in row 3 s t u v w x g h j k l m n o p q r
but need print
s g m t h n u o v j p w k q x l r
no, doing wrong. these steps.
1> instead of creating char a[]
in each case, outside switch
block:
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 = 0; < 6; i++) { // since 6 characters in array. for(int j = 0; j < y; j++) { // print first characters each required array, sec characters on new line, 3rd , on. switch(sc[j] + 1) { // since counting starts 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 arrayindexoutofboundsexception } } system.out.println(); // print newline character after printing every line }
3> since case 5
prone arrayindexoutofbounds, set in try-catch statement. (optional)
case 5: seek { system.out.print("\t"+e[i]); } catch(arrayindexoutofboundsexception ex) { system.out.println("\t "); // print empty space \t character. } break;
java
No comments:
Post a Comment