线程主java.util.illegalformatconversionexception中的异常d = java.lang.double

时间:2016-01-29 19:53:00

标签: java

Java新手在这里,我一直在制作一个程序来计算乘坐公共汽车时哪个选项更好:单卡,月卡或日卡。所以我收到了这个错误:

exception in thread main java.util.illegalformatconversionexception d = java.lang.double

这与我d double猜测double有关。但是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 amount of days:"); double p = scan.nextDouble(); System.out.println("Enter amount of rides:"); double s = scan.nextDouble(); System.out.println("Enter the price of one time ticket:"); double y = scan.nextDouble(); System.out.println("Enter the price 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)) { System.out.printf("Cheaper option is single cards"); } else if ((p * d) > (y * s)) { System.out.printf("Cheaper option is %f day tickets", p); } else if ((k > (y * s))) { System.out.printf("Cheaper option is a month card"); } else if ((p * d) > k) { System.out.printf("Cheaper option is %f day tickets", p); } else if ((y * s) > k) { System.out.printf("Cheaper option is single cards"); } else if (k > (p * d)) { System.out.printf("Enter the price of a month card:"); } else if ((k == (p * d) == (y * s))) { //this doesnt seem to be working System.out.printf(""Cheaper option is single cards"") } } } 应该没问题。

另一个问题是 - 如果2或3个选项都相同便宜,我如何让程序从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");

}

}

}

我很感激帮助! 编辑:(由我自己解决,只是忘了在else()之间放置else if语句)我发现了另一个缺陷,那就是 - 如果计算2但是没有考虑到第3个。例如:如果y * s&lt; p * d但是真的&#34; k&#34;是最便宜的,它会说y * s是最便宜的。我试过用&amp;&amp; amp;在那里格式化第二个声明,但它会抛出我&#34;否则&#34;没有&#34;如果&#34;,&#34; ;预期&#34;并且非法开始表达。

编辑2:新代码,完全正常工作:

[PXDBDate(DisplayMask="f",PreserveTime=true,InputMask="f")]

3 个答案:

答案 0 :(得分:1)

if ((k == (p * d) == (y * s)))

不是一个有效的表达方式。每个布尔表达式只能进行1次比较,因此这将计算为布尔值=== y * s双精度值。你可以加入他们。即

if (k == (p * d) && k == (y * s))

另外

System.out.printf(""Cheaper option is single cards"")

错了,您只使用一组"来表示字符串,而您遗漏了;

答案 1 :(得分:0)

k == (p * d)给出一个布尔值(true或false),然后你试图将该布尔值与(y * s)的类型为double进行比较。

答案 2 :(得分:0)

在此评估中,编译器首先检查左对比运算符(k ==(p * d)),并查找为true或false。在任何情况下,结果都是布尔值。您现在想要将它与double进行比较,这是不正确的。你必须if (k == (p * d) && k == (y * s))

记住转义序列。您需要输入\"才能使用文字"。同样,您可以在字符串中为文字\\键入\