簡體   English   中英

字符類-將字符串中每個單詞的首字母大寫

[英]Character Class- Capitalize the first letter of each word in a string

我在程序中遇到麻煩,在該程序中,我應該接受字符串輸入,將其更改為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);
        }

        }
}

我現在只是嘗試使用for循環將字符串數據更改為char數據,但是該程序即使編譯也不會運行。 (我正在使用BlueJ IDE)

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

復制下面的程序,然后輸入它而不是您的程序。 它會完美地工作。 該程序是:

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 Bhargava)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM