有人可以告诉我我的 Java 代码有什么问题吗?
Could someone please tell me what's wrong with my Java code?
我正在尝试制作美元 <-> 日元转换器。如果您觉得这些数字不对,请相信我——我算了算。另外,我知道间距不对,但这并不重要。如果需要,可以在 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)));
每个错误有两个 - 每行一个 - 所以行中的相似之处必须是相同的问题(呃)。那么,有人有任何线索吗?
P.S.: 如果代码除此之外还有任何问题,请务必指出。
P.S。 2:我知道我基本上是通过将每个字符放入此文本块来编写此代码 public,但请不要窃取。我知道有人要这样做,但请不要这样做。
~奇迹
理想情况下,您的代码存在很多问题,首先是方法名称的命名约定、字符串比较。以下是使您的代码正常工作所需的最少代码更改:-
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();
}
}
我正在尝试制作美元 <-> 日元转换器。如果您觉得这些数字不对,请相信我——我算了算。另外,我知道间距不对,但这并不重要。如果需要,可以在 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)));
每个错误有两个 - 每行一个 - 所以行中的相似之处必须是相同的问题(呃)。那么,有人有任何线索吗?
P.S.: 如果代码除此之外还有任何问题,请务必指出。
P.S。 2:我知道我基本上是通过将每个字符放入此文本块来编写此代码 public,但请不要窃取。我知道有人要这样做,但请不要这样做。 ~奇迹
理想情况下,您的代码存在很多问题,首先是方法名称的命名约定、字符串比较。以下是使您的代码正常工作所需的最少代码更改:-
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();
}
}