0
這很難解釋,但我正在嘗試創建一個程序,只能將以句號,問號或感嘆號結尾的每個單詞的字母大寫。輸入任何標記後,只有在第二次輸入標記時才能得到結果。換句話說,我必須打兩次才能得到結果,我不知道爲什麼。我仍然在自己的工作,但我堅持這個問題。具有特定要求的句子資本家
import java.util.*;
public class SentenceCapitalizer
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Input a sentence: ");
String line = keyboard.nextLine();
String wrong = keyboard.nextLine();
String[] check = {".!?"};
String upper_case_line="";
Scanner lineScan = new Scanner(line);
for (String sent : check)
{
if (sent.startsWith(wrong))
{
System.out.println("cant use .?!");
}
else
{
/* if (line.startsWith(" "))//if starts with space
System.out.println("good");
else
System.out.println("bad");
*/
//if (int i = 0; i < line.length; i++)
//{char c = line.chartAt(i);
while(lineScan.hasNext())
{
String word = lineScan.next();
upper_case_line += Character.toUpperCase(word.charAt(0)) +
word.substring(1) + " ";
}
System.out.println(upper_case_line.trim());
}
}
}
}
謝謝你的工作 –
@SirNicholas沒問題!樂意效勞! – cunniemm