use*_*163 1 java string variables 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);
}
}
Run Code Online (Sandbox Code Playgroud)
我对编码很新,这是我的学校项目.我试图在该字符串中打印变量aprice但我收到标题中的错误.
而不是这个:
System.out.println("The cost of your movie tickets before is ", aprice);
Run Code Online (Sandbox Code Playgroud)
做这个:
System.out.println("The cost of your movie tickets before is " + aprice);
Run Code Online (Sandbox Code Playgroud)
这称为"连接".阅读此Java跟踪以获取更多信息.
编辑:你也可以使用格式PrintStream.printf.例如:
double aprice = 4.0 / 3.0;
System.out.printf("The cost of your movie tickets before is %f\n", aprice);
Run Code Online (Sandbox Code Playgroud)
打印:
以前的电影票价为1.333333
你甚至可以这样做:
double aprice = 4.0 / 3.0;
System.out.printf("The cost of your movie tickets before is $%.2f\n", aprice);
Run Code Online (Sandbox Code Playgroud)
这将打印:
以前的电影票价是1.33美元
的%.2f可以读作"格式(的%),其为数字(f)用2位小数(的.2)".在$前面的在%是作秀,顺便说一句,这不是比说:"把这里$"等格式字符串的一部分.您可以在Formatterjavadocs中找到格式规范.
| 归档时间: |
|
| 查看次数: |
14406 次 |
| 最近记录: |