gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:10:59 25 4
gpt4 key购买 nike

为什么我的代码在输入句子后找不到句点?用户应该写一个句子,然后在末尾加一个句号。当进入该时间段时,程序应该结束。帮忙?

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;
}
}
}
}

最佳答案

while(sentence.length()){
{

if(sentence.equals("."))

while 语句中包含无效表达式。并且:如果您只输入了 ,sentence.equals(".") 只会返回 true。

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

关于java - 为什么我的代码在输入句子后找不到句点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23830987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com