繁体   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