主线程java.util.illegalformatconversionexception d=java.lang.double中出现异常
Java新手在这里,我一直在做一个程序来计算在乘车时哪个选项更好:单卡、月卡还是日卡。所以我得到了这个错误:主线程java.util.illegalformatconversionexception d=java.lang.double中出现异常,java,Java,Java新手在这里,我一直在做一个程序来计算在乘车时哪个选项更好:单卡、月卡还是日卡。所以我得到了这个错误: exception in thread main java.util.illegalformatconversionexception d = java.lang.double 我猜这与d是double有关。但是,double应该可以 另一个问题是-如果2个或3个选项都一样便宜,我如何让程序从2个随机的更便宜的方式中进行选择 代码: 我感谢你的帮助! 编辑:(我自己解决了,只是忘了把e
exception in thread main java.util.illegalformatconversionexception d = java.lang.double
我猜这与d
是double
有关。但是,double
应该可以
另一个问题是-如果2个或3个选项都一样便宜,我如何让程序从2个随机的更便宜的方式中进行选择
代码:
我感谢你的帮助!
编辑:(我自己解决了,只是忘了把else-if语句放在附加()之间)我发现了另一个缺陷,那就是-if计算2,但没有考虑第三个。例如:如果y*s编辑2:新代码,完全工作:
import java.util.Scanner;
public class kt_3_3 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter days:");
double p = scan.nextDouble();
System.out.println("Enter amount of drives:");
double s = scan.nextDouble();
System.out.println("Enter one time ticket cost:");
double y = scan.nextDouble();
System.out.println("Enter the cost of a day ticket:");
double d = scan.nextDouble();
System.out.println("Enter the price of a month card:");
double k = scan.nextDouble();
if (((y * s) < (p * d)) && (y * s) < k) {
System.out.printf("Cheaper way is one time tickets");
} else if (((p * d) < (y * s)) && (p * d) < k) {
System.out.printf("Cheaper way is %f day tickets", p);
} else if (((k < (y * s))) && k < (p * d)) {
System.out.printf("cheaper way is a month card");
} else if (((p * d) < k) && (p * d) < (y * s)) {
System.out.printf("Cheaper way is %f day tickets", p);
} else if (((y * s) < k) && (y * s) < (p * d)) {
System.out.printf("Cheaper way is one time tickets");
} else if ((k < (p * d)) && k < (y * s)) {
System.out.printf("cheaper way is a month card");
} else if (((k == (p * d))) && k < (y * s)) {
System.out.printf("cheaper way is a month card");
} else if (((k == (y * s))) && k < (p * d)) {
System.out.printf("Cheaper way is a month card");
} else if (((y * s) == (p * d)) && (y * s) < k) {
System.out.printf("Cheaper way is one time tickets");
}
}
}
import java.util.Scanner;
公共类kt_3_3{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入天数:”);
double p=scan.nextDouble();
System.out.println(“输入驱动器数量:”);
双s=scan.nextDouble();
System.out.println(“输入一次性机票成本:”);
双y=scan.nextDouble();
System.out.println(“输入一张日票的成本:”);
double d=scan.nextDouble();
System.out.println(“输入一张月卡的价格:”);
double k=scan.nextDouble();
如果(((y*s)<(p*d))&&(y*s)
k==(p*d)
给出一个布尔值(真或假),然后您尝试将该布尔值与双精度类型的(y*s)
进行比较。如果((k==(p*d)==(y*s))
不是有效的表达式。每个布尔表达式只能进行1次比较,因此这将计算为布尔===y*s双精度。你可以加入他们。即
if(k==(p*d)和&k==(y*s))
也
System.out.printf(“更便宜的选择是单卡”)
是错误的,您只对字符串使用了一组
“
”,并且您在该求值中缺少了尾随的;
,编译器首先检查左比较运算符(k==(p*d))并且发现它为真或假。在任何情况下,结果都是布尔值。您现在想要将其与双精度进行比较,这是不正确的。如果(k==(p*d)和&k==(y*s)),则必须执行
请记住转义序列。您需要键入\“
以获得文字”
。同样,您也可以在字符串中键入\\
以获得文字\
import java.util.Scanner;
public class kt_3_3 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter days:");
double p = scan.nextDouble();
System.out.println("Enter amount of drives:");
double s = scan.nextDouble();
System.out.println("Enter one time ticket cost:");
double y = scan.nextDouble();
System.out.println("Enter the cost of a day ticket:");
double d = scan.nextDouble();
System.out.println("Enter the price of a month card:");
double k = scan.nextDouble();
if (((y * s) < (p * d)) && (y * s) < k) {
System.out.printf("Cheaper way is one time tickets");
} else if (((p * d) < (y * s)) && (p * d) < k) {
System.out.printf("Cheaper way is %f day tickets", p);
} else if (((k < (y * s))) && k < (p * d)) {
System.out.printf("cheaper way is a month card");
} else if (((p * d) < k) && (p * d) < (y * s)) {
System.out.printf("Cheaper way is %f day tickets", p);
} else if (((y * s) < k) && (y * s) < (p * d)) {
System.out.printf("Cheaper way is one time tickets");
} else if ((k < (p * d)) && k < (y * s)) {
System.out.printf("cheaper way is a month card");
} else if (((k == (p * d))) && k < (y * s)) {
System.out.printf("cheaper way is a month card");
} else if (((k == (y * s))) && k < (p * d)) {
System.out.printf("Cheaper way is a month card");
} else if (((y * s) == (p * d)) && (y * s) < k) {
System.out.printf("Cheaper way is one time tickets");
}
}
}