有人可以告诉我我的Java代码有什么问题吗?

时间:2015-08-18 04:24:15

标签: java converter currency

我试图赚取美元< - > JPY转换器。如果这些数字对你不利,请相信我 - 我做了数学计算。另外,我知道间距是关闭的,但这并不重要。如果需要,用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。:如果除此之外的代码有任何错误,请提及。

P.S。 2:我知道我通过将每个字符放入这个文本块中来公开这个代码,但请不要窃取。我知道有人要去做,但请不要。 〜wundr

1 个答案:

答案 0 :(得分:0)

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

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();
    }
}