10921 - Find the Telephone

Problem Link

Cara nak selesaikan soalan ni senang sangat untuk di selesaikan. Kita hanya perlu membuat perbandingan dengan menggunakan "if-else" statement, kemudian display mengikut syarat yang tertera dalam yang table yang telah ditunjukkan. Sebagai contoh jika kita masukkan 'A' maka ianya akan display '2'.

Solution :

import java.util.*;
public class uva10921 {
public static void main(String [] args) {
Scanner scn = new Scanner(System.in);
while(scn.hasNextLine()) {
String s = scn.nextLine();
for(int y=0;y<s.length();y++) {
if(s.charAt(y)=='A' || s.charAt(y)=='B' || s.charAt(y)=='C')
System.out.print("2");
else if(s.charAt(y)=='D' || s.charAt(y)=='E' || s.charAt(y)=='F')
System.out.print("3");
else if(s.charAt(y)=='G' || s.charAt(y)=='H' || s.charAt(y)=='I')
System.out.print("4");
else if(s.charAt(y)=='J' || s.charAt(y)=='K' || s.charAt(y)=='L')
System.out.print("5");
else if(s.charAt(y)=='M' || s.charAt(y)=='N' || s.charAt(y)=='O')
System.out.print("6");
else if(s.charAt(y)=='P' || s.charAt(y)=='Q' || s.charAt(y)=='R' || s.charAt(y)=='S')
System.out.print("7");
else if(s.charAt(y)=='T' || s.charAt(y)=='U' || s.charAt(y)=='V')
System.out.print("8");
else if(s.charAt(y)=='W' || s.charAt(y)=='X' || s.charAt(y)=='Y' || s.charAt(y)=='Z')
System.out.print("9");
else if(s.charAt(y)=='1')
System.out.print("1");
else if(s.charAt(y)=='0')
System.out.print("0");
else if(s.charAt(y)==' ' )
System.out.print(" ");
else if(s.charAt(y)=='-')
System.out.print("-");
}
System.out.println("");
}
}
}

Share this

Related Posts

Previous
Next Post »