Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java currencyConverter Java.lang.NumberFormatException:_Java - Fatal编程技术网

Java currencyConverter Java.lang.NumberFormatException:

Java currencyConverter Java.lang.NumberFormatException:,java,Java,我在这一行得到一个错误double dollar=double.parseDouble(输入) 代码: /** Converts money using CurrencyConverter */ import java.util.*; public class CurrencyConverterTester { public static void main(String[] args) { //Normally the scanner

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

代码:

   /**
   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

输入是一个字符串,因此必须使用
.equals()
,而不是
=
。相等运算符
==
确定两个引用是否指向同一对象。其中,
.equals()
方法检查它们在逻辑上是否相等


您还希望使用
|
而不是
&&
。更好的方法是对字符串输入使用相等运算符
=
。对于字符串,必须使用其
.equals()
方法

因此:

while (input != "q" && input != "Q")
应该是:

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


使用
scanner.next()
时也要小心,最好使用
scanner.nextLine()
否则会在输入变量中获得换行符。因此,当您键入“q”时,输入变量可能是
String input=“q\n”

不要将字符串值与
=
进行比较=。改用

您使用
==
所做的是比较对象地址。这不是您想要的,因为您在内存中的某个地方有一个字符串对象,其中包含
“q”
,而另一个对象则包含
“q”
,位于内存中的另一个位置。它们有不同的地址,这导致使用
==
进行比较返回false,而内容在文本上是相同的。

!(…)
可能不起作用…
“q”。相等信号转移(输入)
将降低
空点异常的风险。。。但我只是吹毛求疵…
,而(!(cond))
会更好。
while (!(input.equals("q") || input.equals("Q")))
while (!"q".equalsIgnoreCase(input)) // While input not q or Q