Задача: Currency Converter. По-удачен вариант за решение на задачата?
Здравейте, някой може ли да предостави по удачен вариант за решаване нa тази задача? Този, който съм написал нещо не ми допада. Аз съм включил само пет комбинации за вдох и то тези, които изискваше judge. Интересно ми е има ли вариант, в който чрез for обикаляме всички възможни комбинации?
Ето го кода: https://pastebin.com/u0Usdcpq
import java.util.Scanner; public class CurrencyConverter { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double amount = Double.parseDouble(scanner.nextLine()); String inputCurrency = scanner.nextLine(); String outputCurrency = scanner.nextLine(); double USD = 1.79549; double EUR = 1.95583; double GBP = 2.53405; if (inputCurrency.equals("USD") && outputCurrency.equals("BGN")) { amount = amount * USD; System.out.printf("%.2f BGN", amount); } else if (inputCurrency.equals("BGN") && outputCurrency.equals("EUR")) { amount = amount / EUR; System.out.printf("%.2f EUR", amount); } else if (inputCurrency.equals("EUR") && outputCurrency.equals("GBP")) { amount = amount * (EUR / GBP); System.out.printf("%.2f GBP", amount); } else if (inputCurrency.equals("USD") && outputCurrency.equals("EUR")) { amount = amount * (USD / EUR); System.out.printf("%.2f EUR", amount); } else if (inputCurrency.equals("GBP") && outputCurrency.equals("USD")) { amount = amount * (GBP / USD); System.out.printf("%.2f USD", amount); } } }
Здравейте,
Ето още едно решение с малко по-мако if - ове.
import java.util.Scanner;
public class CurrencyConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double value = Double.parseDouble(scanner.nextLine());
String inputCurr = scanner.nextLine();
String outputCurr = scanner.nextLine();
double usd = 1.79549;
double eur = 1.95583;
double gbp = 2.53405;
double suma = 0;
if(inputCurr.equals("BGN")){
switch (outputCurr) {
case "USD": suma = value / usd;
System.out.printf("%.2f USD",suma);
break;
case "EUR": suma = value / eur;
System.out.printf("%.2f EUR",suma);
break;
case "GBP": suma = value / gbp;
System.out.printf("%.2f GBP",suma);
break;
}
}
if(inputCurr.equals("USD")){
switch (outputCurr) {
case "BGN": suma = value / (1 / usd);
System.out.printf("%.2f BGN",suma);
break;
case "EUR": suma = (value / (1 / usd)) / eur;
System.out.printf("%.2f EUR",suma);
break;
case "GBP": suma = (value / (1 / usd)) / gbp;
System.out.printf("%.2f GBP",suma);
break;
}
}
if(inputCurr.equals("EUR")){
switch (outputCurr) {
case "BGN":
suma = value / (1 / eur);
System.out.printf("%.2f BGN", suma);
break;
case "USD":
suma = (value / (1 / eur)) / usd;
System.out.printf("%.2f USD", suma);
break;
case "GBP":
suma = (value / (1 / eur)) / gbp;
System.out.printf("%.2f GBP", suma);
break;
}
}
if(inputCurr.equals("GBP")){
switch (outputCurr) {
case "BGN": suma = value / (1 / gbp);
System.out.printf("%.2f BGN",suma);
break;
case "USD": suma = (value / (1 / gbp)) / usd;
System.out.printf("%.2f USD",suma);
break;
case "EUR": suma = (value / (1 / gbp)) / eur;
System.out.printf("%.2f EUR",suma);
break;
}
}
}
}