【问题标题】:If and Else statement run on second iteration of while loopIf 和 Else 语句在 while 循环的第二次迭代中运行
【发布时间】:2017-10-20 12:55:54
【问题描述】:

我正在研究这个简单的货币转换器,只是为了强化我学到的一些概念。一切都按计划进行,除了转换器第一次运行后,while 循环的第二次迭代导致第一个答案同时使用 if 和 else。输出如下:


美元兑换成哪种货币? [日元、人民币、比索、英镑、欧元] 磅

您希望兑换多少美元? 5

您已将 5.0 美元兑换成 3.8 英镑

美元兑换成哪种货币? [日元、人民币、比索、英镑、欧元] 请选择一种货币 将美元兑换成哪种货币? [日元、人民币、比索、英镑、欧元]


货币类别:

package currency;

import java.util.HashMap;
import java.util.Map;

public class Currency {

    private Map<String, Double> ct;
    private double newCurrency;
    private String answer;
    private double amount;

    public Currency() {
        this.ct = new HashMap<String, Double>();
    }

    // Getters and Setters
    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public String getAnswer() {
        return this.answer;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public double getAmount() {
        return this.amount;
    }

    // Current available currencies
    public void loadMap() {
        ct.put("Pound", 0.76);
        ct.put("Euro", 0.85);
        ct.put("Yen", 112.23);
        ct.put("Yuan", 6.61);
        ct.put("Peso", 18.87);

        System.out.println(ct.keySet());

    }

    // Multiples amount times currency
    public double multiplyCurrency(String answer, double amount) {

        newCurrency += ct.get(getAnswer()) * getAmount();

        return newCurrency;

    }

    public double getNewCurrency() {
        return this.newCurrency;
    }

    // Checks user if Map contains userinput.
    public boolean checkAnswer(String answer) {
        if (!ct.containsKey(answer)) {
            return false;
        } else {
            return true;
        }
    }


    @Override
    public String toString() {
        return "You have converted $" + getAmount() + " into " + getNewCurrency() + " " + this.answer + "\n";
    }

}

逻辑类:

package logic;

import java.util.Scanner;

import currency.Currency;

public class Logic {

    private Currency currency;
    private Scanner reader;
    private boolean running;
    private String answer;

    public Logic() {
        this.currency = new Currency();
        this.reader = new Scanner(System.in);
        this.running = true;
        this.answer = null;
    }

    public void start() {

        //Runs until quit
        while (running) {
            System.out.println("Convert US dollar into which currency?");
            currency.loadMap();
            answer = reader.nextLine();
            currency.setAnswer(answer);

            // checks to make sure the value is in the map
            if (currency.checkAnswer(currency.getAnswer()) == true) {
                System.out.println("\n" + "How many dollars would you like converted?");
                double amount = reader.nextDouble();
                currency.setAmount(amount);
                currency.multiplyCurrency(currency.getAnswer(), currency.getAmount());
                System.out.println("");
                System.out.println(currency);

            } else {
                System.out.println("Please select a currency");
            }

        }
    }
}

主类:

package logic;

public class Main {

    public static void main(String[] args) {

        Logic l = new Logic();

        l.start();


    }

}

我非常感谢任何反馈,这样我就可以做笔记,并且再也不会发生这种情况了。提前致谢。

【问题讨论】:

  • 基本上你会遇到这个:stackoverflow.com/questions/13102045
  • 我建议你在调试器中单步调试代码,第二次你会看到nextLine返回一个空字符串。
  • @JuanCarlosMendoza:我没有看到任何证据表明这是问题所在 - 你在哪里看到了不正确的比较?
  • 链接错误。已收回。

标签: java if-statement


【解决方案1】:

Scanner.nextDouble 方法不会使用您输入的最后一个换行符,因此在下次调用 Scanner.nextLine 时会使用该换行符。 您基本上遇到了与this post

中提到的相同的问题

一个简单的解决方法是将值作为字符串读取并将其解析为双精度,以便替换

double amount = reader.nextDouble();

double amount = Double.parseDouble(reader.nextLine());

【讨论】:

  • 感谢所有反馈。这个答案很完美。我从来不知道这一点,并将在我未来的所有计划中牢记这一点。快乐编码!
【解决方案2】:

在您的Currency 类中,只需将answer = reader.nextLine(); 更改为answer = reader.next();

这是由于this - 即在nextDouble(); 上造成新行

另外,请注意,如果用户没有输入货币的确切文本,它只会打印您的 else 语句并重新开始。

【讨论】:

    猜你喜欢
    • 2012-06-18
    • 2015-11-08
    • 1970-01-01
    • 2015-04-07
    • 2014-07-01
    • 2016-10-22
    • 2019-05-09
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多