作为初学者,我无法真正理解此代码中的问题:
package Currency;
import java.util.Scanner;
public class Currency {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double value1 = 0.00, value2 = 0.00;
String currency;
System.out.print("Enter a value: ");
value1 = input.nextDouble();
System.out.print("USD or EUR: ");
currency = input.nextLine();
if(currency.equals("USD")){
value2 = value1 * 0.734878047;
System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
} else if (currency.equals("EUR")) {
value2 = value1 * 1.36077;
System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
} else {
System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
+ "\nConversion rate: 1 EUR = 1.36077 USD");
}
input.close();
}
}
不知何故,它甚至没有读取第二个输入,但只是打印最后一个。任何人都可以帮助我做到这一点吗? :)
提前谢谢。
答案 0 :(得分:1)
似乎input.nextLine();
正在用户在第一次提示后点击输入时在换行符中读取。
快速修复可能会在您提示input.nextLine()
或USD
之前添加另一个EUR
“虚拟”来电:
package Currency;
import java.util.Scanner;
public class Currency {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double value1 = 0.00, value2 = 0.00;
String currency;
System.out.print("Enter a value: ");
value1 = input.nextDouble();
input.nextLine();
System.out.print("USD or EUR: ");
currency = input.nextLine();
if(currency.equals("USD")){
value2 = value1 * 0.734878047;
System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
} else if (currency.equals("EUR")) {
value2 = value1 * 1.36077;
System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
} else {
System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
+ "\nConversion rate: 1 EUR = 1.36077 USD");
}
input.close();
}
}
答案 1 :(得分:0)
我认为点击返回键会对下一个读取线产生影响。如下所示做一个小改动似乎解决了这个问题:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double value1 = 0.00, value2 = 0.00;
String currency;
System.out.print("Enter a value: ");
String str = input.nextLine();
value1 = Double.parseDouble(str);
System.out.print("USD or EUR: ");
currency = input.nextLine();
if(currency.equals("USD")){
value2 = value1 * 0.734878047;
System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
} else if (currency.equals("EUR")) {
value2 = value1 * 1.36077;
System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
} else {
System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
+ "\nConversion rate: 1 EUR = 1.36077 USD");
}
input.close();
}
示例:
Enter a value: 25
USD or EUR: USD
25.0USD = 18.371951175 EUR. (Conversion rate: 1 USD = 0.734878047 EUR)
或者,因为在您的情况下,您期望一个没有任何空格的字符串,您可以使用.next()
属性,并保留您的代码:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double value1 = 0.00, value2 = 0.00;
String currency;
System.out.print("Enter a value: ");
value1 = input.nextDouble();
System.out.print("USD or EUR: ");
currency = input.next();
if(currency.equals("USD")){
value2 = value1 * 0.734878047;
System.out.println(value1 + "USD = " + value2 + " EUR. (Conversion rate: 1 USD = 0.734878047 EUR)");
} else if (currency.equals("EUR")) {
value2 = value1 * 1.36077;
System.out.println(value1 + "EUR = " + value2 + " USD. (Conversion rate: 1 EUR = 1.36077 USD)");
} else {
System.out.println("Conversion rate: 1 USD = 0.734878047 EUR"
+ "\nConversion rate: 1 EUR = 1.36077 USD");
}
input.close();
}
答案 2 :(得分:-2)
if
子句中的都使用currency.trim().equals(...
删除字符串开头或结尾的任何空格。