2017-01-25 4 views
0
public static void main(String[] args) 
{ 
    char ch; 

    ch = JOptionPane.showInputDialog("").charAt(0); 

    if (Character.isLetter(ch)) 
    { 
     System.out.println(Character.toUpperCase(ch)); 
    } 

    if (Character.isDigit(ch)) 
    { 
     Math.sqrt(ch); 
     System.out.println(ch); 
    } 

    if (Character.isWhitespace(ch)) 
    { 
     int code = ch; 
     System.out.println (code); 
    } 
} 

두 개 이상의 문자를 입력하면 첫 번째 문자 만 인식합니다. 사용자가 char로 입력하는 여러 문자를 프로그램에 표시하려면 어떻게합니까?문자 입력에서 여러 문자 읽기 및 이중으로 변환

+1

'.charAt (0)'첫 번째 문자 만 가져 오라고 말하고 있습니다. – litelite

+0

여러 문자를 가져 오려면 어떻게해야합니까? –

+0

'ch'를 문자열로 선언하고'.charAt (0)'을 제거하십시오. 그러나받은 루프 문자를 반복 할 루프를 추가해야합니다. – litelite

답변

0
에만이 가진 첫번째 character을 복용

: - charAt(0) 당신에게 항상 첫 번째 문자 0의 인덱스와 character을 제공

ch = JOptionPane.showInputDialog("").charAt(0); 

.

당신이 전체 input 및뿐만 아니라 첫 번째 문자 당신을 치료하려면,

String s = JOptionPane.showInputDialog(""); 

은 또한 당신의 Charater.isLetter(ch)은 당신이 할 수없는, 지금은 작동하지 않습니다 다음과 같이 문자열을 사용해보십시오 String으로 해주세요. 그러나 원하는 경우 전체 StringUpperCase으로 변환 할 수 있습니다. 또한 String이 유효한 Double인지 쉽게 확인할 수 없습니다. exception handling이 필요하며 혼동을 줄 수 있습니다. 어쨌든 포함 시키므로 혼란 스러울 때는 언제든지 물어보십시오.

최종 프로그램은 다음과 같다 : - 당신은 아무것도에 붙어있는 경우

String s; 
s = JOptionPane.showInputDialog(""); 
try{ 
    double d = Double.parseDouble(s); 
}catch(Exception e){ 
    //Not a Double as it comes inside catch 
    //Therefore String must be empty or contain Characters 

    //Checking for Empty 

    if(s.trim().length()<1){ 
    //String is empty 
    //Do what you want 
    System.out.println("String is whitespace"); 
    }else { 
     //String must contain Characters. 
     System.out.println(s.toUpperCase()); 
    } 
} 

, 문의하십시오.

+0

도와 줘서 고마워! 매우 감사! @Shashwat –