简体   繁体   English

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

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

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,但标题中出现错误。

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 trail for more info. 阅读此Java跟踪以获取更多信息。

Edit: You could also use formatting via PrintStream.printf . 编辑:您也可以通过PrintStream.printf使用格式。 For example: 例如:

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 %.2f can be read as "format (the % ) as a number (the f ) with 2 decimal places (the .2 )." %.2f可以理解为“格式( % )为具有小数点后两位( .2 )的数字( f )。” 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 . 您可以在Formatter javadocs中找到格式规范。

you are looking for 你在找

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

+ concatenates Strings. +连接字符串。 , separates method parameters. ,分隔方法参数。

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

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值发送。

It occurs when you use , instead of + ie: 当您使用而不是+,它会发生:

use this one: 使用这个:

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

instead of 代替

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

I don't know if you have found the answer yet, but I understood that you should write like this:我不知道你是否找到了答案,但我知道你应该这样写:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PrintStream 类型中的方法 println(boolean) 不适用于参数 (void) - The method println(boolean) in the type PrintStream is not applicable for the arguments (void) PrintStream 类型中的方法 println(int) 不适用于参数 (String, int, int, int) - The method println(int) in the type PrintStream is not applicable for the arguments (String, int, int, int) double类型的方法parsedouble字符串不适用于double参数 - the method parsedouble string in the type double is not applicable for the arguments double 类型PrintStream的方法printf(String,Object ...)不适用于参数(String,void) - The method printf(String, Object…) in the type PrintStream is not applicable for the arguments (String, void) PrintStream 类型中的 printf(String, Object[]) 方法不适用于参数 (...) - The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (...) Method(double, double) 不适用于参数 (double[], double[]) - Method(double, double) is not applicable for the arguments (double[], double[]) 如何解决 main 类型中的方法 add(double, double, int, double) 不适用于参数 (double, double, double, double) - how to solve The method add(double, double, int, double) in the type main is not applicable for the arguments (double, double, double, double) main类型的方法(double [])不适用于参数(int []) - The method (double[]) in the type main is not applicable for the arguments (int[]) 错误:找不到适合 println(String,float) 的方法? 方法 PrintStream.println(float) 不适用? - error: no suitable method found for println(String,float)? method PrintStream.println(float) is not applicable? PathIterator类型的currentSegment(float [])方法不适用于参数(Double []) - The method currentSegment(float[]) in the type PathIterator is not applicable for the arguments (Double[])
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM