簡體   English   中英

PrintStream 類型中的方法 println(double) 不適用於 arguments (String, double)

[英]The method println(double) in the type PrintStream is not applicable for the arguments (String, double)

這是代碼:

import java.util.Scanner;

public class MoviePrices {
    public static void main(String[] args) {
        Scanner user = new Scanner(System.in);
        double adult = 10.50;
        double child = 7.50;
        System.out.println("How many adult tickets?");
        int fnum = user.nextInt();

        double aprice = fnum * adult;
        System.out.println("The cost of your movie tickets before is ", aprice);

    }
}

我對編碼很陌生,這是我的學校項目。 我正在嘗試在該字符串中打印變量 aprice,但標題中出現錯誤。

代替這個:

System.out.println("The cost of your movie tickets before is ", aprice);

做這個:

System.out.println("The cost of your movie tickets before is " + aprice);

這稱為“串聯”。 閱讀此Java跟蹤以獲取更多信息。

編輯:您也可以通過PrintStream.printf使用格式。 例如:

double aprice = 4.0 / 3.0;
System.out.printf("The cost of your movie tickets before is %f\n", aprice);

印刷品:

之前的電影票費用為1.333333

您甚至可以執行以下操作:

double aprice = 4.0 / 3.0;
System.out.printf("The cost of your movie tickets before is $%.2f\n", aprice);

這將打印:

您之前的電影票費用為$ 1.33

%.2f可以理解為“格式( % )為具有小數點后兩位( .2 )的數字( f )。” %前面的$只是為了顯示,順便說一句,它不是格式字符串的一部分,只說“這里放$”。 您可以在Formatter javadocs中找到格式規范。

你在找

System.out.println("The cost of your movie tickets before is " + aprice);

+連接字符串。 ,分隔方法參數。

試試這個

System.out.println("The cost of your movie tickets before is " + aprice);

您也可以這樣做:

System.out.printf("The cost of your movie tickets before is %f\n", aprice);

這將有助於:

System.out.println("The cost of your movie tickets before is " + aprice);

原因是如果您昏迷,則發送兩個不同的參數。 如果使用上面的行,則將double值添加到字符串上,然后它將參數作為String而不是String和double值發送。

當您使用而不是+,它會發生:

使用這個:

System.out.println ("x value" +x);

代替

System.out.println ("x value", +x);

我不知道你是否找到了答案,但我知道你應該這樣寫:

System.out.println(MessageFormat.format("我的名字是 = {0},我的 FirstLetter 名字是 {1},Myage 是 = {2}",Myname,myfirstl,d));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM