Java连续输入代码



好的,所以我试图弄清楚如何编码的程序(不是真正修复),我必须使用Java来接受来自用户的连续输入,直到他们进入一个句号。然后,它必须计算用户在句点前输入的字符总数。

import java.io.*;
class ContinuousInput
{
    public static void main(String[] args) throws IOException
    {
    InputStreamReader inStream = new InputStreamReader (System.in);
    BufferedReader userInput = new BufferedReader (inStream);
    String inputValues;
    int numberValue;
    System.out.println("Welcome to the input calculator!");
    System.out.println("Please input anything you wish: ");
    inputValues = userInput.readLine();
    while (inputValues != null && inputValues.indexOf('.')) {
    inputValues = userInput.readLine();
    }
    numberValue = inputValues.length();
    System.out.println("The total number of characters is " + numberValue + ".");
    System.out.println("Thank you for using the input calculator!");
    }
}

请不要建议使用Scanner,我们使用的Java SE平台是SDK 1.4.2_19模型,我们无法更新它。空大括号的解释:我认为如果我放入空大括号,它将允许连续输入,直到输入句号,但显然不是这样的…

编辑:更新的代码当前错误:不会结束时。是输入。

您必须将if/else语句与while语句交换。

示例:

inputValues = userInput.readLine();
while (!".".equals(inputValues) {
   //do your stuff
   //..and after done, read the next line of the user input.
   inputValues = userInput.readLine();
}

注意:不要将String对象的值与==操作符的值进行比较。使用equals()方法

如果你只想测试用户输入的句子是否包含.符号,你只需要从equals()切换到contains()。这是java.lang.String类的内置方法。

示例:

 while (inputValues != null && !inputValues.contains(".")) {
    //do your stuff
 }

最新更新