【问题标题】:Could someone please tell me what's wrong with my Java code? [duplicate]有人可以告诉我我的 Java 代码有什么问题吗? [复制]
【发布时间】:2015-08-18 04:24:15
【问题描述】:

我正在尝试制作美元 日元转换器。如果你觉得这些数字不合适,相信我——我做了数学计算。另外,我知道间距已经关闭,但这没关系。如果需要,可以在 N++ 中对其进行变形。

import java.util.Scanner;
import java.lang.Math;

public class jpy_usd {
public static void main(String[] args) {
    //Initializing variables and the scanner
    boolean isSourceUSD;
    double usd;
    double usdMult = 0.00803568;
    int jpy;
    double jpyMult = 124.449;
    Scanner scanner = new Scanner(System.in);

    //Selecting an input currency and checking for valid input arguments
    System.out.println("Choose your input currency. Type \'USD\' for U.S. Dollars or \'JPY\' for Japanese Yen.");
    while(isSourceUSD != true && isSourceUSD != false) {
        if(scanner.nextLine == "USD") {
            isSourceUSD = true;
        } else if(scanner.nextLine == "JPY") {
            isSourceUSD = false;
        } else {
            System.out.println("Invalid argument. Please use \'USD\' for U.S. Dollars or \'JPY\' for Japanese Yen.");
        }
    }

    //Asking for the input in the chosen currency and converting
    if(isSourceUSD) {
        System.out.println("Please enter the value to convert");
        usd = scanner.nextDouble;
        System.out.println(usd " in JPY is " (Math.round(usd * jpyMult)));
    } else if(!isSourceUSD) {
        System.out.println("Please enter the value to convert");
        jpy = scanner.nextInt;
        System.out.println(jpy " in USD is " (Math.round(jpy * usdMult)));
        }
    }
}

如果您希望我添加错误消息,我想我可以。没有太多解释,这两行平均分布有 6 个错误:

System.out.println(usd " in JPY is " (Math.round(usd * jpyMult)));
System.out.println(jpy " in USD is " (Math.round(jpy * usdMult)));

每个错误有两个 - 每行一个 - 所以行中的相似之处必须是相同的问题(duh)。那么,有人有线索吗?

P.S.:如果除此之外代码有任何问题,请务必提及。

附: 2:我知道我基本上是通过将每个字符放入该文本块中来公开此代码,但请不要窃取。我知道有人要这样做,但请不要这样做。 ~wundr

【问题讨论】:

  • while(isSourceUSD != true && isSourceUSD != false) 告诉我这个逻辑有什么问题。据我估计,您的代码甚至不应该触及与Scanner 相关的部分。
  • 如果你想连接字符串,你应该使用+System.out.println(usd + " in JPY is " + (Math.round(usd * jpyMult)));

标签: java converter currency


【解决方案1】:

理想情况下,您的代码存在许多问题,从方法名称的命名约定、字符串比较开始。以下是使您的代码工作所需的最小代码更改:-

import java.util.Scanner;
import java.lang.Math;

public class jpy_usd {
public static void main(String[] args) {
    //Initializing variables and the scanner
    String isSourceUSD;
    double usd;
    double usdMult = 0.00803568;
    int jpy;
    double jpyMult = 124.449;
    Scanner scanner = new Scanner(System.in);

    //Selecting an input currency and checking for valid input arguments
    System.out.println("Choose your input currency. Type \'USD\' for U.S. Dollars or \'JPY\' for Japanese Yen.");

        if(scanner.nextLine() .equalsIgnoreCase( "USD")) {
            isSourceUSD = "USD";
        } else if(scanner.nextLine() .equalsIgnoreCase( "JPY")) {
            isSourceUSD ="JPY" ;
        } else {
            isSourceUSD="NA";
            System.out.println("Invalid argument. Please use \'USD\' for U.S. Dollars or \'JPY\' for Japanese Yen.");
        }

    //Asking for the input in the chosen currency and converting
    if(isSourceUSD.equalsIgnoreCase("USD")) {
        System.out.println("Please enter the value to convert");
        usd = scanner.nextDouble();
        System.out.println(usd +" in JPY is "+ (Math.round(usd * jpyMult)));
    } else if(isSourceUSD.equalsIgnoreCase("JPY")) {
        System.out.println("Please enter the value to convert");
        jpy = scanner.nextInt();
        System.out.println(jpy+ " in USD is " +(Math.round(jpy * usdMult)));
        }
    else{
        System.out.println("Do nothing!!!");
    }
    scanner.close();
    }
}

【讨论】:

  • 非常感谢。看来我需要恢复到我的 Java 标准。输入 JPY 作为货币不起作用,但我会检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
  • 2020-10-30
  • 2010-11-08
  • 1970-01-01
  • 2021-11-16
相关资源
最近更新 更多