Java PrintStream 类型中的方法 println(double) 不适用于参数 (String, double)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18260337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:48:34  来源:igfitidea点击:

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

javastringvariablesdouble

提问by user2687163

Here is the code:

这是代码:

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);

    }
}

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.

我对编码很陌生,这是我的一个学校项目。我试图在该字符串中打印变量 aprice,但在标题中出现错误。

采纳答案by Brian

Instead of this:

取而代之的是:

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

Do this:

做这个:

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

This is called "concatenation". Read this Java trailfor more info.

这称为“串联”。阅读此 Java 跟踪了解更多信息。

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

编辑:您也可以通过PrintStream.printf. 例如:

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

Prints:

印刷:

The cost of your movie tickets before is 1.333333

你之前的电影票成本是1.333333

You could even do something like this:

你甚至可以做这样的事情:

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

This will print:

这将打印:

The cost of your movie tickets before is $1.33

你之前的电影票成本是 1.33 美元

The %.2fcan 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 Formatterjavadocs.

%.2f可以读作“格式(的%),其为数字(f)用2位小数(的.2)”。在$前面的在%是作秀,顺便说一句,这不是比说其他格式字符串的一部分,“把这里$”。您可以在Formatterjavadocs 中找到格式规范。

回答by jlordo

you are looking for

你正在寻找

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

+concatenates Strings. ,separates method parameters.

+连接字符串。,分隔方法参数。

回答by ThePerson

This will help:

这将有助于:

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

The reason is that if you put in a coma, you are sending two different parameters. If you use the line above, you add the double onto your string, and then it sends the parameters as a String rather than a String and a double.

原因是,如果您进入昏迷状态,则会发送两个不同的参数。如果使用上面的行,则将 double 添加到字符串中,然后它将参数作为 String 而不是 String 和 double 发送。

回答by ThePerson

Try this one

试试这个

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

And you can also do that:

你也可以这样做:

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

回答by YeLiMs NeEvAn

It occurs when you use ,instead of +i.e:

它发生在您使用,而不是+ie 时:

use this one:

使用这个:

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

instead of

代替

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