2017-03-01 23 views
-3
  for (int i = 0, len = input.length(); i < len; i++) { 
       char ch = input.charAt(i); 
       if (i % 2 == 0) { 
        System.out.print(Character.toLowerCase(ch)); 
       } else { 
        System.out.print(Character.toUpperCase(ch)); 

       } 
      } 

我想在不使用字符類的情況下執行此操作。只使用tolowerCase和toUpperCase和基本循環。在不使用字符類的情況下輸出一個交替大寫和小寫的字符串

因此,基本程序會將字符串「Hello World」轉換爲「HeLlo WoRID」而不使用字符類。

我被告知你可以做到,但我無法弄清楚。這真的讓我很沮喪

+0

查看ASCII表 –

+0

您還可以使用子字符串以及 – UnknowableIneffable

+0

子字符串!謝謝!!! – konigsberg7

回答

0

我想通了。這純粹是這樣的:

  for (int i = 0, len = input.length(); i < len; i++) { 

       char ch = input.charAt(i); 
       String str = ch + ""; //<- converts char to a string! 

       if (i % 2 == 0) { 
        System.out.print(str.toLowerCase()); 
       } else { 
        System.out.print(str.toUpperCase()); 

不知道爲什麼,這引起了downvoted這麼多,我花了像谷歌上一個小時的尋找這樣的事情。我想我吮吸谷歌。

相關問題