这是我为一个简单的租车java程序提出的编码。它编译没有错误,但输出没有显示,所以我该怎么办?请帮助tx。我是一名java初学者。
import java.util.Scanner;
public class carRent
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
String name, address;
double bodo,endodo, kilo;
double days, charge;
System.out.print("Name: ");
name=input.nextLine();
System.out.print("State: ");
address=input.nextLine();
System.out.print("Beginning Odometer : ");
bodo=input.nextDouble();
System.out.print("End Odometer : ");
endodo=input.nextDouble();
System.out.print("Days car used : ");
days=input.nextDouble();
kilo = endodo - bodo;
System.out.printf("\nKilometer car driven : ", + kilo ,"km");
System.out.printf("\nNumber of days rented : ", + days);
charge = days * 150.00 + kilo * 1.00;
System.out.printf("\nTotal charge of rent : ", + charge);
}
}
答案 0 :(得分:4)
您没有正确使用printf
(因为您没有以String
格式指定变量的任何占位符)。我建议你改用println
:
kilo = endodo - bodo;
System.out.println("\nKilometer car driven : " + kilo + "km");
System.out.println("\nNumber of days rented : " + days);
charge = days * 150.00 + kilo * 1.00;
System.out.println("\nTotal charge of rent : " + charge);
示例输入和输出:
Name: Eran
State: IL
Beginning Odometer : 1000
End Odometer : 2000
Days car used : 2
Kilometer car driven : 1000.0km
Number of days rented : 2.0
Total charge of rent : 1300.0
答案 1 :(得分:1)
printf
是“打印格式”的缩写,它采用格式String
和需要格式化的参数...
相反,你应该使用更像......
的东西System.out.printf("%nKilometer car driven : %.2fkm", kilo);
哪个可以输出像......
Name: test
State: Test
Beginning Odometer : 123.456
End Odometer : 789.123
Days car used : 10.25
Kilometer car driven : 665.67km
Number of days rented : 10
Total charge of rent : 2203.17
例如值......
System.out.printf("%nKilometer car driven : %.2fkm", kilo);
System.out.printf("%nNumber of days rented : %.0f", days);
charge = days * 150.00 + kilo * 1.00;
System.out.printf("%nTotal charge of rent : %.2f%n", charge);
有关详细信息,请查看these examples
答案 2 :(得分:0)
如果您希望能够在一行中添加多个字符串,请使用:
System.out.print("I am an example String!");
如果您希望String在其自己的行上使用:
System.out.println("I am an example String on my own line!");
答案 3 :(得分:0)
我建议使用:
System.out.println("Insert String Here");
而不是printf
,因为您没有使用占位符。