【发布时间】: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)));