为什么我的代码在输入句子后找不到句号?

时间:2014-05-23 13:48:14

标签: java string

为什么我的代码在输入句子后找不到句号?用户应该写一个句子然后在句末放一个句号。输入句点后,程序应该结束。帮助

import java.io.*;
class Sentence



{
public static void main(String[] args) throws IOException
{
InputStreamReader inStream = new InputStreamReader (System.in);
BufferedReader mVHS = new BufferedReader (inStream);

String inData; //Store the input data in a String    
int result;//Assign the result to the int data type
String sentence, string2; //Store the names in the String type



//Enter a sentance
System.out.println("Type a sentence but make sure it ends with a period:");
Sting userInput = mVHS.readLine();
sentence = userInput;


while(sentence.length()){
    { 

   if(sentence.equals("."))
        System.out.println("Thank you come again.");
   else
    System.out.println("You must put a period to end the program");
    System.out.println("Type a period:");
    userInput = mVHS.readLine();
    sentence = userInput;
}
}
}
}

1 个答案:

答案 0 :(得分:5)

while(sentence.length()){
    { 

   if(sentence.equals("."))

while语句中包含无效表达式。 和:sentence.equals("。")只会返回true,如果你什么也没输入。

你可以检查:if sentence.endsWith("。");