[英]Java Continuous Input Code
好的,所以我要弄清楚如何編寫代碼的程序(不是真正解決問題的程序),我必須使用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模型,我們無法對其進行更新。 空括號的解釋:我認為如果插入空括號,它將允許在輸入句號之前連續輸入,但顯然不是這樣的...
編輯:更新的代碼當前錯誤:到不會結束。 輸入。
您必須使用while
切換if/else
語句。
樣品:
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.