2

現在、while ループ内の行は、「10 ドルの手数料が請求されます」と「アカウントを閉鎖しますか?」です。

「10 ドルの手数料が請求されます」のように別の行に表示されます。
「指示はよろしいですか?」

両方のステートメントが 1 行に出力されるようにコードの書式を変更するにはどうすればよいですか。プログラムは同じように動作するはずです。

「10 ドルの手数料が請求されます。アカウントを閉鎖しますか?」という回答が表示されます。

import acm.program.*;

public class BankAccount extends ConsoleProgram {
    public void run() {

        int bankRoll = 50;

        while(bankRoll > 0) {
            println("Currently your balance is " + bankRoll + " . ");
            println("You will be charged a fee of 10 dollars."); 
            String closeAct = readLine("Would you like to close your account?");
            if(closeAct.equals("yes")) {
                break;
            }
            bankroll -= 10;
        } /* end of while loop */

        println("your account is now closed.");

    }
}
4

7 に答える 7

4

println の代わりに print を使用する必要があります。

println は print に似ていますが、文字列の最後に改行文字である "\n" を追加します。

複数の行を作成する場合は、print を使用して、区切りたい場所に「\n」を追加することもできます。

例えば:

print("This is an example with one line")
print("This is still in the same line as the text ahead.")

2行にしたいが、1つの文字列が1つある場合は、次のこともできます

print("This is an example with 2 lines" + "\n" + "but only 1 String")

println は、以前のように行末に「\n」を作成します

println("This is an example with two lines") // Here is an automatically added "\n"
print ("This text is in the next line")
于 2013-07-02T11:58:36.533 に答える
2

あなたの文字列を準備し、最後にあなたができること

result= result.replaceAll("\\\\n","");

そしてprint

于 2013-07-02T11:30:58.517 に答える
1

println() から print().Println() への変更は、毎回新しい行で始まります。

于 2013-07-02T11:30:02.307 に答える
0

印刷物のようなものを使用する場合があります。たとえば、System.out.print はコンソールの同じ行に出力を表示します。System.out.println は、出力を別々の行に表示します。

于 2013-07-02T11:30:04.637 に答える
0

これを試して:

print("You will be charged a fee of 10 dollars. "); 

print("your account is now closed.");
于 2013-07-02T11:30:45.180 に答える