2017-02-14 40 views
0

我被困在我的項目中,我們必須在數字行的縮放比例中顯示一行字符串,以便我們可以刪除或向該字符串添加字符。我不確定如何根據字符串的長度在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); 

    } 
+4

歡迎來到Stack Overflow!看起來你可能會問作業幫助。雖然我們本身沒有任何問題,但請觀察這些[應做和不應該](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845),並相應地編輯您的問題。 (即使這不是作業,也請考慮建議。) –

回答

1

我會建議你實現一個方法printScale或類似的東西,這需要Stringint作爲參數,並打印兩行的你。

你傷心,你已經可以這樣,如果你有一個String與價值"This is the ine"刪除字符,你在你的例子顯示,你可以這樣調用方法:

printScale(myNewString.length());

這種方法可能看起來像這樣(不完美,但工程):

public void printLine(int amountOfCharacters) { 
    StringBuilder lineNumber = new StringBuilder(); 
    StringBuilder lineScaleSymbols = new StringBuilder(); 
    for (int i = 0; i < amountOfCharacters; i++) { 
     if (i % 10 == 0) { 
      if (i < 10) { 
       lineNumber.append(i); 
      } else { 
       lineNumber.insert(i -1, i); 
      } 
      lineScaleSymbols.append('|'); 
     } else if (i % 5 == 0) { 
      if (i < 10) { 
       lineNumber.append(i); 
      } else { 
       lineNumber.insert(i -1, i); 
      } 
      lineScaleSymbols.append('+'); 
     } else { 
      lineNumber.append(' '); 
      lineScaleSymbols.append('-'); 
     } 
    } 

    System.out.println(lineNumber.toString()); 
    System.out.println(lineScaleSymbols.toString()); 
} 

希望這會有所幫助。

+0

感謝您的幫助 – bubbles2189

1

您的showNumberLine方法處於正確的軌道上。

讓我們的輪廓,你需要做什麼:

  • 確定字符串的長度
  • 產生相同長度的字符串
    • 每一個字符用0結束的數行會成爲特殊字符|
    • 每個以5結尾的字符將成爲特殊字符+
    • 所有其他角色將是-

你可以讓你的循環是這樣,使用模運算來確定寫一個字:

for(int i = 0; i < line.length(); i++) { 
    if(i % 10 == 0) { 
     // the number is divisible by 10 (ends in zero) 
     System.out.print("|"); 
    } else if(i % 5 == 0 && i % 10 != 0) { 
     // the number is divisible by 5 and not divisible by 10 (ends in 5) 
     System.out.print("+"); 
    } else { 
     System.out.print("-"); 
    } 
    System.out.println(); 
} 

輸出:

|----+----|----+----|----+----|----+----|--- 
The quick brown fox jumped over the lazy dog 

你需要更多的代碼來寫出數字(0, 5號,10號,15號),我會把它留給你。這將是類似的邏輯,但有一些細微的問題需要考慮,因爲數字的長度是1個字符,然後是2個字符,然後是3個字符(0,5,10,15,... 100,105)。在某些時候,你不得不停下來,因爲這些數字不適合這個空間。

+0

感謝您的幫助 – bubbles2189

相關問題