[英]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.