PrintStream类型的println(double)方法不适用于参数(String,double) [英] The method println(double) in the type PrintStream is not applicable for the arguments (String, double)

查看:3633
本文介绍了PrintStream类型的println(double)方法不适用于参数(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,但是我在标题中收到错误。

I am very new to coding and this is a project of mine for school. I am trying to print the variable aprice within that string but I am getting the error in the heading.

推荐答案

而不是这样:

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路径

This is called "concatenation". Read this Java trail for more info.

编辑:您还可以通过 PrintStream.printf 格式化。例如:

You could also use formatting via PrintStream.printf. For example:

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

打印:


你的电影票的费用是1.333333

The cost of your movie tickets before is 1.333333

你甚至可以这样做:

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

这将打印:


以前的电影票的费用是$ 1.33

The cost of your movie tickets before is $1.33

%。2f 可以读取为格式()作为一个数字( f ),带有2位小数地方( .2 )。在之前的 $ 只是为了显示,btw,它不是格式字符串的一部分,而不是说放一个$在这里。您可以在 Formatter javadocs

The %.2f can be read as "format (the %) as a number (the f) with 2 decimal places (the .2)." The $ in front of the % is just for show, btw, it's not part of the format string other than saying "put a $ here". You can find the formatting specs in the Formatter javadocs.

这篇关于PrintStream类型的println(double)方法不适用于参数(String,double)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆