Java编程、货币计算器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24732018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:33:08  来源:igfitidea点击:

Java programming, currency calculator

java

提问by user3836013

As a beginner, i cannot really understand the issue in this code:

作为初学者,我无法真正理解此代码中的问题:

package Currency;

import java.util.Scanner;

public class Currency {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double value1 = 0.00, value2 = 0.00;
        String currency;

        System.out.print("Enter a value: ");
        value1 = input.nextDouble();
        System.out.print("USD or EUR: ");
        currency = input.nextLine();


        if(currency.equals("USD")){
            value2 = value1 * 0.734878047;
            System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
        } else if (currency.equals("EUR")) {
            value2 = value1 * 1.36077;
            System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
        } else {
            System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
            + "\nConversion rate: 1 EUR = 1.36077 USD");
        }

        input.close();

    }

}

Somehow, it doesnt even read the second input, but just prints last else. Can anyone help me to get this right? :)

不知何故,它甚至不读取第二个输入,而只是打印最后一个。谁能帮我解决这个问题?:)

Thank you in advance.

先感谢您。

回答by paubo147

in the ifclauses in both cases use currency.trim().equals(...to remove any whitespaces at the beginning or end of the String.

在这if两种情况下的子句中都currency.trim().equals(...用于删除字符串开头或结尾的任何空格。

回答by Aristocrates

It seems like input.nextLine();is reading in the newline from when the user hits enter after the first prompt.

input.nextLine();当用户在第一个提示后按回车键时,似乎正在读取换行符。

A quick fix could be adding in another input.nextLine()"dummy" call before you prompt for USDor EUR:

input.nextLine()在您提示USDor之前,快速修复可能是添加另一个“虚拟”调用EUR

package Currency;

import java.util.Scanner;

public class Currency {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double value1 = 0.00, value2 = 0.00;
        String currency;

        System.out.print("Enter a value: ");
        value1 = input.nextDouble();
        input.nextLine();
        System.out.print("USD or EUR: ");
        currency = input.nextLine();

        if(currency.equals("USD")){
            value2 = value1 * 0.734878047;
            System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
        } else if (currency.equals("EUR")) {
            value2 = value1 * 1.36077;
            System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
        } else {
            System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
                    + "\nConversion rate: 1 EUR = 1.36077 USD");
        }

        input.close();
    }
}

回答by npinti

I think that hitting the return key is somewhat affecting the next readline. Doing a minor change as below seems to have fixed the problem:

我认为按下回车键在某种程度上影响了下一个阅读行。做一个如下的小改动似乎已经解决了这个问题:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double value1 = 0.00, value2 = 0.00;
    String currency;

    System.out.print("Enter a value: ");
    String str = input.nextLine();
    value1 = Double.parseDouble(str);
    System.out.print("USD or EUR: ");
    currency = input.nextLine();


    if(currency.equals("USD")){
        value2 = value1 * 0.734878047;
        System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
    } else if (currency.equals("EUR")) {
        value2 = value1 * 1.36077;
        System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
    } else {
        System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
        + "\nConversion rate: 1 EUR = 1.36077 USD");
    }

    input.close();
}

Example:

例子:

Enter a value: 25
USD or EUR: USD
25.0USD = 18.371951175 EUR. (Conversion rate: 1 USD = 0.734878047 EUR)

Alternatively, since in your case you are expecting a string without any white space, you could use the .next()property instead and leave your code as is:

或者,由于在您的情况下,您需要一个没有任何空格的字符串,您可以改用该.next()属性并按原样保留代码:

   public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    double value1 = 0.00, value2 = 0.00;
    String currency;

    System.out.print("Enter a value: ");
    value1 = input.nextDouble();
    System.out.print("USD or EUR: ");
    currency = input.next();


    if(currency.equals("USD")){
        value2 = value1 * 0.734878047;
        System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
    } else if (currency.equals("EUR")) {
        value2 = value1 * 1.36077;
        System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
    } else {
        System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
        + "\nConversion rate: 1 EUR = 1.36077 USD");
    }

    input.close();

}