2017-03-08 111 views

回答

1

由於第一個字母大寫,我們可以得出結論,在偶數位置字符串的每一個字母都會大寫進行,但由於可能還有特殊字符,我們也必須記住這一點。您可以使用的示例算法如下:

String x = jTextField1.getText(); 
len = x.length(); 
String otherstring; 
int j=0; //to be used as counter to check alternate char 
for (int i = 0;i<len;i++) { 
    j++; 
    char ch = x.charAt(i); 
    if(!isalpha(ch)){ 
     j--; //not to consider non-letters 
     otherString += ch; 
    } 
    if (j % 2 != 0) { 
     Character.toLowerCase(ch)); 
     otherString += ch; 
    } 
    else{ 
     Character.toUpperCase(ch); 
     otherString += ch; 
    } 
} 

將字符附加到另一個字符串,您可以顯示輸出。

+0

您好,它非常有助於找出答案。謝謝 –

0

公共靜態無效的主要(字串[] args){// TODO自動生成方法存根

String s="We are the worLD"; 
    System.out.println(s); 
    int j=0; 
    String otherstring=null; 
    int length=s.length(); 
    for (int i=0;i<length;i++){ 
     j++; 
     char ch=s.charAt(i); 
     if(!Character.isAlphabetic(ch)){ 
      j--; 
      otherstring+=ch; 
     } 
     if(j%2==0){ 
      ch=Character.toLowerCase(ch); 
      otherstring+=ch;     
     }else{ 
      ch=Character.toUpperCase(ch); 
      otherstring+=ch; 
     } 
    } 
    System.out.println(otherstring.substring(4)); 

}

+0

這樣我找到了確切的解決方案。 –

相關問題