2014-05-02 258 views
0

我遇到了一個程序問題,我應該接受一個字符串輸入,將它改爲char數據,然後大寫每個字的第一個字母使用Character類的「字符串」中的單詞。字符類大寫字符串中每個單詞的第一個字母

的代碼如下:

import java.util.*; 
public class wrapper 
{ 
    public static void main(String[] args) 
    { 
     Scanner input= new Scanner(System.in); 
     String s1; 
     s1=input.nextLine(); 
     s1= s1.trim(); 
     int howLong= s1.length(); 
     int i; 
     int counter; 
     char ch; 
     for(counter=0; counter<= howLong; counter++) 
     { 
      ch=s1.charAt(counter);    
      System.out.print(ch); 
     } 

     } 
} 

我只是試圖改變字符串數據爲char使用在此刻循環的數據,但即使它編譯程序將無法運行。 (我正在使用BlueJ IDE)

+1

'計數器<= howLong'應該是'計數器

+0

你被允許使用'toCharArray()'?不是你需要它,但你可能會喜歡它。此外,_「不會運行」_部分告訴我[此調試指南](http://keysersblog.wordpress.com/2014/04/21/debugging-java-code-a-beginners-guide/)可能對你:) – keyser

+0

我目前正在閱讀指導謝謝,沒有講座還沒有提到這種方法。 – user3385542

回答

0

您必須在屏幕上打印一條語句才能輸入字符串,否則控制檯不會自動打開。

複製下面的程序並輸入它而不是您的程序。它會很好地工作。該計劃是:

import java.util.*; 
public class wrapper 
{ 
    public static void main(String[] args) 
    { 
     System.out.println("Enter a string"); 
     Scanner input= new Scanner(System.in); 
     String s1; 
     s1=input.nextLine(); 
     s1= s1.trim(); 
     int howLong= s1.length(); 
     int i; 
     int counter; 
     char ch; 
     for(counter=0; counter< howLong; counter++) 
     { 
      ch=s1.charAt(counter);    
      System.out.print(ch); 
     } 

     } 
} 

希望這對你有所幫助!

問候,

Rachit巴爾加瓦

相關問題