我有返回基於用戶的輸入字符串中的基本方法:突破它後重新點燃條件語句?
public String getString() {
String message = inputGenerator.getMessage(); // Returns user inputted string
String messageStart = message.substring(0, 3); // Get start of message
String concat = ""; // Variable to concatenate messages
if(messageStart.equals("Hi")) {
concat += message; // Append input to concat string.
inputGenerator.getMessage(); // Call for another user prompt
} else {
concat += message; // Append input to concat string.
}
return concat; // Return concatenated string.
}
我想要做什麼:
正如你所希望的工作了,我想要做的如果消息的開頭包含單詞hi
,直到它沒有,並且返回該連接的字符串,例如提示用戶輸入更多消息
>> Enter a string ("hiexample")
>> Enter a string ("hianotherexample")
>> Enter a string ("nothi")
>> returns "hiexamplehianotherexamplenothi"
問題
的問題是,由於inputGenerator.getMessage();
被稱爲後明顯跳出條件的if語句只能使用一次。
如果我嘗試使用while()
語句,它會永遠運行並最終導致程序崩潰。
顯示'while'循環你正在嘗試使用。 –
你可以在使用while循環時顯示你嘗試的邏輯,只是一個fyi:'「hi」.equals(「Hi」)== false' – clcto
@ PM77-1 while while(messageStart.equals(「Hi 「)) – user2381114