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

==String 入力に等価演算子を使用しています。.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()、 ではなくを使用する必要があり==ます。等価演算子==は、2 つの参照が同じオブジェクトを指しているかどうかを判断します。.equals()メソッドが論理的に等しいかどうかをチェックする場所。

||notも使いたいです&&。さらに良いのは使用することですString#equalsIgnoreCase()

于 2013-11-04T00:41:19.067 に答える
0

==文字列値をまたはと比較しないでください!=String.equals代わりに使用してください。

あなたがしていたこと==は、オブジェクトのアドレスを比較することです。を含むメモリ内のどこかに String オブジェクトがあり、メモリ内の別の場所"q"にを含む別のオブジェクトがあるため、これはあなたが望むものではありません。これらは異なるアドレスを持っているため、内容はテキスト的に同じであるにもかかわらず、false を返すことを使用して比較が行われます。"q"==

于 2013-11-04T00:41:28.707 に答える