1

我在这一行得到一个错误double dollar = Double.parseDouble(input)

java.lang.NumberFormatException:
For input string: "q"(in sun.misc.FloatingDecimal)

代码:

   /**
   Converts money using CurrencyConverter
   */

   import java.util.*;

   public class CurrencyConverterTester {
     public static void main(String[] args) {
       //Normally the scanner is based on System.in,
       //Scanner scanner = new Scanner(System.in) ;
       //but for predictability we set the input to a fixed sequence:
       Scanner scanner = new Scanner("0.79447 100 20 88.88 q");

       System.out.print("Conversion factor (euros per dollar): ");
       //We could use scanner.nextDouble() here, but this is an example
       //of using parseDouble, which you need in the next loop.
       String input = scanner.next();
       double rate = Double.parseDouble(input);
       System.out.println(rate);
       //----------------Start below here. To do: approximate lines of code = 1
       // 1. make the CurrencyConverter object based on the rate
       CurrencyConverter converter = new CurrencyConverter(rate);
       //----------------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

       System.out.print("Dollar value (Q to quit): ");
       input = scanner.next();
       System.out.println(input); //echo the input
       //----------------Start below here. To do: approximate lines of code = 8
       // 1. write a while loop where the condition is that input is not "Q" or "q" ; 2. use parseDouble to get the dollars amount ; 3.  use the converter object to convert the dollars to euros ; 4.  print the dollars and euros in the style shown in the Expected region (Hint: use printf); 5. prompt for the next input ; 6. read the next input ; 7. echo the input (i.e., print it out)

       while (input != "q" && input != "Q") {
         double dollar = Double.parseDouble(input);
         double euros = converter.convert(dollar);
         System.out.printf("%.2f dollars = %.2f euros\n", dollar, euros);
         System.out.print("Dollar value (Q to quit): ");
         input = scanner.next();
         System.out.println(input);
       }
     }
   }

   Conversion factor(euros per dollar): 0.79447
   Dollar value(Q to quit): 100
   100.00 dollars = 79.45 euros
   Dollar value(Q to quit): 20
   20.00 dollars = 15.89 euros
   Dollar value(Q to quit): 88.88
   88.88 dollars = 70.61 euros
   Dollar value(Q to quit): q
4

3 回答 3

2

您正在==对字符串输入使用相等运算符。对于字符串,您必须使用他们的.equals()方法。

所以:

while (input != "q" && input != "Q")

应该:

while (!(input.equals("q") || input.equals("Q")))

或者

while (!"q".equalsIgnoreCase(input)) // While input not q or Q

还要小心使用scanner.next()它会更好,scanner.nextLine()否则你会在你的输入变量中得到一个换行符。因此,当您键入“q”时,输入变量可能是String input = "q\n";

于 2013-11-04T00:41:25.120 回答
0

input是一个字符串,所以你必须使用.equals(),而不是==。相等运算符==确定两个引用是否指向同一个对象。该.equals()方法检查它们是否在逻辑上相等。

你也想用||not &&。更好的是使用String#equalsIgnoreCase()

于 2013-11-04T00:41:19.067 回答
0

不要将 String 值与==or进行比较!=。改为使用String.equals

您所做的==是比较对象地址。这不是您想要的,因为您在内存中的某处有一个 String 对象,其中包含"q",并且您在内存中的其他地方有另一个包含"q"对象。它们具有不同的地址,这导致使用返回 false 进行比较,而内容在文本上是相同的。==

于 2013-11-04T00:41:28.707 回答