Java 程序没有显示输出
Java program not showing output
这是我为一个简单的汽车租赁 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);
}
}
您没有正确使用 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
如果您希望能够在一行中添加多个字符串,请使用:
System.out.print("I am an example String!");
如果您希望字符串独占一行,请使用:
System.out.println("I am an example String on my own line!");
我建议使用:
System.out.println("Insert String Here");
而不是 printf
,因为您没有使用占位符。
printf
是 "print format" 的缩写,它采用格式 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
这是我为一个简单的汽车租赁 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);
}
}
您没有正确使用 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
如果您希望能够在一行中添加多个字符串,请使用:
System.out.print("I am an example String!");
如果您希望字符串独占一行,请使用:
System.out.println("I am an example String on my own line!");
我建议使用:
System.out.println("Insert String Here");
而不是 printf
,因为您没有使用占位符。
printf
是 "print format" 的缩写,它采用格式 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