2016-10-13 3 views
0

問題がなければ、Javaの割り当てに関する助けが必要です。私たちはまだ始まったばかりですが、私の先生は私たちが自分たちでたくさんの研究をしたいと思っています。私は宿題のやり方を理解することができません。行の文字を制限してディスプレイを次の行に移動する方法

私たちは、彼が私たちに10の異なるスピーチを与えてくれるという課題があります。客観的なコーディングを使って全体を表示する必要があります。私はこれまでのところ、最初のファイルにリンクする変数を設定して画面に表示する方法を考え出しましたが、彼は各行にいくつの文字があるかを制限したいので、横にスクロールして単一の行のスピーチ。これは、私が次の数時間のすべてのスピーチのすべての文に対して新しい変数を作っている位置に私を残して、もっと効率的な方法がなければならないと思います。だから、私は友人(去年のクラスを受講した人)に助言を求めました。そして、forループを使用して一定量の文字の後にスペースをスキャンし、次の行にジャンプして続けることを勧めました。これを行う。私がこれまでに持っていたのは、先生が使用するように言われたベースファイルと、最初の10のスピーチの始まりです。

/** 
* TextWriter is a program that uses objective coding to display 10 political speeches 
* @author() 
* @version (10/12/16) 
*/ 
public class TextWriter { 
    private String textToDisplay;//text to be displayed 
    public TextWriter() { 
     textToDisplay = ""; 
    } 
    public TextWriter(String inputText) { 
    textToDisplay = inputText; 
    } 
    public void clearTextToDisplay() { 
     textToDisplay = ""; 
    } 
    public void setTextToDisplay(String inputText) { 
     textToDisplay = inputText; 
    } 
    public String getTextToDisplay() { 
     return textToDisplay; 
    } 
    public void display() { 
     System.out.println(textToDisplay); 
    } 
} 

2つ目、

/** 
* Displays Washington's Farewell speech using objective oriented coding. 
* @author() 
* @version (10/12/16) 
*/ 
public class WashingtonFarewellDriver { 
    public static void main(String[] args) { 
     TextWriter wf1; 
     wf1 = new TextWriter(); 
     wf1.setTextToDisplay("Friends and Citizens: The period for a new election of a citizen to administer the executive government of the United States being not far distant, and the time actually arrived when your thoughts must be employed in designating the person who is to be clothed with that important trust, it appears to me proper, especially as it may conduce to a more distinct expression of the public voice, that I should now apprise you of the resolution I have formed, to decline being considered among the number of those out of whom a choice is to be made."); 
     wf1.display(); 
     TextWriter wf2; 
     wf2 = new TextWriter("I beg you, at the same time, to do me the justice to be assured that this resolution has not been taken without a strict regard to all the considerations appertaining to the relation which binds a dutiful citizen to his country; and that in withdrawing the tender of service, which silence in my situation might imply, I am influenced by no diminution of zeal for your future interest, no deficiency of grateful respect for your past kindness, but am supported by a full conviction that the step is compatible with both."); 
     wf2.display(); 
     TextWriter wf3; 
     wf3 = new TextWriter("The acceptance of, and continuance hitherto in, the office to which your suffrages have twice called me have been a uniform sacrifice of inclination to the opinion of duty and to a deference for what appeared to be your desire. I constantly hoped that it would have been much earlier in my power, consistently with motives which I was not at liberty to disregard, to return to that retirement from which I had been reluctantly drawn."); 
     wf3.display(); 
    } 
} 

(うまくいけば、それが右のフォーマット済みです) 私はそれはそれは一種の見下ろしているように見えるんので、私は、宿題の助けを求めていることを大丈夫だことを願っています私はかなり混乱しています。うまくいけば、誰かが私の先生よりも何が起こっているのか説明できます。 ありがとうございました!質問があれば、私もそれらに答えることができるかもしれません。

+0

スペースで分割して、いくつのインデックスごとに改行を印刷することができます。 – Li357

答えて

0

これには多くの方法があります。

あなたはこのような何かの行を追加するために、あなたのTextWriterクラスでこの機能を追加することができます。

public void addLines(int maxChars){ 
    int lines = 1; 
    String[] lineStrings; 

    if(maxChars <= textToDisplay.length()){ 
     if(textToDisplay.length() % maxChars > 0) lines = textToDisplay.length()/maxChars + 1; 
     else lines = textToDisplay.length()/maxChars; 

     lineStrings = new String[lines]; 

     for(int i = 0; i < lines; i++){ 
      if(i == (lines - 1)) lineStrings[i] = textToDisplay.substring(i*maxChars, i*maxChars + (textToDisplay.length() % maxChars)) + "\r\n"; 
      else lineStrings[i] = textToDisplay.substring(i*maxChars, i*maxChars + maxChars) + "\r\n"; 
     } 

     textToDisplay = ""; 

     for(int i=0; i < lines; i++){ 
      textToDisplay += lineStrings[i]; 
     } 
    } 
} 

をし、自分のメイン機能で、多分:

public class WashingtonFarewellDriver { 
    public static void main(String[] args) { 
     TextWriter wf1; 
     wf1 = new TextWriter(); 
     wf1.setTextToDisplay("Friends and Citizens: The period for a new election of a citizen to administer the executive government of the United States being not far distant, and the time actually arrived when your thoughts must be employed in designating the person who is to be clothed with that important trust, it appears to me proper, especially as it may conduce to a more distinct expression of the public voice, that I should now apprise you of the resolution I have formed, to decline being considered among the number of those out of whom a choice is to be made."); 
     wf1.addLines(50); 
     wf1.display(); 
     TextWriter wf2; 
     wf2 = new TextWriter("I beg you, at the same time, to do me the justice to be assured that this resolution has not been taken without a strict regard to all the considerations appertaining to the relation which binds a dutiful citizen to his country; and that in withdrawing the tender of service, which silence in my situation might imply, I am influenced by no diminution of zeal for your future interest, no deficiency of grateful respect for your past kindness, but am supported by a full conviction that the step is compatible with both."); 
     wf2.addLines(50); 
     wf2.display(); 
     TextWriter wf3; 
     wf3 = new TextWriter("The acceptance of, and continuance hitherto in, the office to which your suffrages have twice called me have been a uniform sacrifice of inclination to the opinion of duty and to a deference for what appeared to be your desire. I constantly hoped that it would have been much earlier in my power, consistently with motives which I was not at liberty to disregard, to return to that retirement from which I had been reluctantly drawn."); 
     wf3.addLines(50); 
     wf3.display(); 
    } 
} 

これは動作するはずですが、いくつかの単語がしますこれは、行の最大文字で大まかに行を区切るため、切り捨てられます。

0

String.charAt()を使用して、文字を文字列でループします。あなたが出した文字の数を記録しておきます。次回に改行文字を吐き出したスペースが表示されたら、カウンタを0にリセットしてもう一度印字を開始してください。すべてのフィードバックのための

String in = "This is a run on sentence that is too long for a single line and should be broken up into multiple lines because I said so. This is a run on sentence that is too long for a single line and should be broken up into multiple lines because I said so."; 

int counter=0; 

for(int i=0;i<in.length();i++){ 
    Char c=in.charAt(i); 
    counter++; 
    System.out.print(c+""); 
    if((counter>25)&&(c=' ')){ 
    System.out.println(); 
    counter=0; 
    } 
} 
0

おかげで、それは私を助けたが、最終的に私のその友人は彼がダウンロードmain.org.apache.commons.lang3.text.WordUtilsパッケージを使用して、私を歩いて別の非常に簡単な方法がありました!

import java.io.IOException; 
import org.apache.commons.lang3.text.WordUtils; 
public class WashingtonFarewellDriver { 
    public static void main(String[] args) throws IOException { 
     int wwl = 110; 
     TextWriter wf1; 
     wf1 = new TextWriter(WordUtils.wrap("long sentences",wwl)); 
     wf1.display(); 
    } 
} 
関連する問題