1

私のコードが原因で PrintStream が新しい行に移動する理由がわかりません。

// displays the total time of the leak in months from the calculateLeakTime() method. 
    String leakTime = LeakCalculator.calculateLeakTime(); 
    System.out.println("The total time of the leak is " + leakTime + " months.");

出力は次のようになります。

「リークの合計時間は12
か月です。」

月が新しい行に印刷されている理由がわかりません。私もこれを試しました:

// displays the total time of the leak in months from the calculateLeakTime() method. 
    String leakTime = LeakCalculator.calculateLeakTime(); 
    System.out.print("The total time of the leak is " + leakTime);
    System.out.println(" months.");

そして、私は同じ出力を得ました。「月」を新しい行に移動する理由を誰かが説明できますか?

ありがとう、

ケビン

編集:これがcalculatLeakTime()メソッドです:

static String calculateLeakTime() throws ParseException{

    long leakEndMili = getLeakEnd(); 
    long leakBeginMili = getLeakBegin(); 

    long timeDays = ((leakEndMili - leakBeginMili) / (1000 * 60 * 60 * 24));

    double timeMonths = (double) (timeDays * 0.0328549); 

    System.out.println(timeMonths);

    String monthsRounded = String.format("%.0f%n", timeMonths); 

    return monthsRounded; 
    }
4

2 に答える 2

0

メソッドが返す文字列の末尾にLeakCalculator.calculateLeakTime()改行を追加するため、これを行っています。"\n"確実に伝えるために、返された文字列を構成する個々の文字を出力してください。

これを解決するLeakCalculator.calculateLeakTime()には、返された文字列に改行を追加しないようにメソッドを修正します。おそらく、文字列ではなく数値を返す必要があります。


あなたの状態を編集
:

これは問題の format("%.0f%n", timeMonths) だと思います

はい、%n改行文字です。

于 2014-02-21T02:31:39.467 に答える