我被困在我的項目中,我們必須在數字行的縮放比例中顯示一行字符串,以便我們可以刪除或向該字符串添加字符。我不確定如何根據字符串的長度在5秒內打印出刻度。根據給定的字符串顯示一個數字行
例:
0 5 10 15 20
|----+----|----+----|-
This is the first line
然後,用戶將選擇他們想要從位置和位置使用字符串刪除字符。它將顯示用戶從字符串中選擇的位置並刪除。
例:
from position: 12
to position: 18
0 5 10 15 20
|----+----|----+----|-
This is the first line
^^^^^^^ --> // this will be deleted
y/n: y
0 5 10 15
|----+----|----+
This is the ine
我能夠刪除字符,但我不知道如何表達基於字符串的數字線。這是我到目前爲止的代碼:
public void showNumberLine(String line)
{
int lineCount = line.length(); // getting the length of the string being passed in
String numberLine = "";
for(int i = 0; i <= lineCount; i++) //
{
numberLine = "" + i;
System.out.println("|----+----|----+----|-");
}
}
public void deleteSubString()
{
Scanner keyboard = new Scanner(System.in);
showNumberLine(textOfLine); // this will print out then number line and the line
System.out.print("from position: ");
int fromIndex = keyboard.nextInt();
System.out.print("to position: ");
int toIndex = keyboard.nextInt();
if(fromIndex < 0 || fromIndex > numOfChar || toIndex < 0 || toIndex > numOfChar)
{
System.out.println("Cannot delete at the given index: Index Out of Bounds");
}
/*
* Create a new number line where it shows what is going to be deleted
*/
String newLineOfString = textOfLine.substring(fromIndex, toIndex);
textOfLine = textOfLine.replace(newLineOfString, "");
System.out.println(newLineOfString);
}
歡迎來到Stack Overflow!看起來你可能會問作業幫助。雖然我們本身沒有任何問題,但請觀察這些[應做和不應該](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845),並相應地編輯您的問題。 (即使這不是作業,也請考慮建議。) –