私のプロジェクトでは、番号の行の文字列を表示しなければならないので、あとでその文字列を削除または追加することができます。私は文字列の長さに基づいて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);
}
ようこそスタックオーバーフロー!宿題の助けを求めているようです。それ自体に問題はありませんが、これらのことを守ってください(http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845)、それに応じて質問を編集してください。 (これは宿題でなくても、とにかくアドバイスを検討してください) –