Question
The last person didn't read my actual question so please read at least the sections I wrote on chegg before answering Code giving out weird
The last person didn't read my actual question so please read at least the sections I wrote on chegg before answering
Code giving out weird answer (Java)
So far I have the following code:
Main file:
MY QUESTION:
I keep getting this formatting problem (personally I think this is an issue in Main file) in the output:
Second to last line (I would have it say 120.0 CAD = ...GBP, but instead it's printing a wrong value in USD), it only happens when I convert from non-usd currency to non-usd currency
How do I fix this (preferably I would prefer a quick fix and not change the whole program), I don't think a quick fix should be too hard because it looks like a formatting issue in the Main file to me. Anything I missed?
*Please ignore comments on the code, that is for my classmates to see (collaborative), the last person who answered it thought my question had to do with Java comments even though I never mentioned it.
public class CurrencyConverter \{ //Here I created the USD_TO_x_Rate variables with a11 the exchange rates for the currencies, using //USD as the centra1 currency. //Note: I got these values a few days ago so they could have fluctuated since. public static final double USD_TO_CAD_RATE =1.34; public static final doub7e USD_TO_GBP_RATE =0.82; public static final double USD_TO_JPY_RATE =127.8 public static fina1 double USD-TO_EUR_RATE =0.93; public static final double USD-TO_RUB_RATE =68.88 public static final double USD_TO_CNY_RATE =6.78; pub7ic static final doub7e USD_TO_AUD_RATE =1.07; public static final doub7e USD_TO_CHF_RATE =0.93; public static final double USD_TO_HKD_RATE =7.81; public static final double USD_TO_SGD_RATE =1.32; public static final double USD_TO_SEK_RATE =10.42; public static final doub7e USD_TO_KRW_RATE =1238.73; //The following two methods, //using a switch statement to sort out a return depending on what the user inputtted. public static double convertFromusd(double usdAmount, String targetcurrency) \{ switch (targetcurrency) \{ case "CAD": return usdAmount * USD_TO_CAD_RATE; case "GBP": case "JPY": case "EUR": return : usdAmount * USD_TO_EUR_RATE; case "RUB": return usdAmount * USD_TO_RUB_RATE; case "CNY": case "AUD": return usdAmount * USD_TO_AUD_RATE; case "CHF": 11 imports are here ort java.uti1. ; 7y class in this file, called Main lic class Mainf //Only method in this file, executes the program. public static void main(String[] args) f //Opens a scanner, and introduce the user and print sone info and things they can input Scanner scanner = new Scanner(System. in); System. out. print in("We lcome to the currency converter!"); System.out. println("Here is the 7ist of currencies we support:"); System. out. println("Currency t "tExchange Rate (USD 1=x Currency)"); System. out. print]n("CAD t,t " t+ CurrencyConverter. USD_TO_CAD_RATE); System. out. println'" "GEP tt\t\t+ CurrencyConverter. USD TO_GBP RATE); System. out. print]n("JPY t\t\t+ CurrencyConverter. USD_TO_JPY_RATE); System. out. print 1n ("ELR \t\\t+ CurrencyConverter. USD TO_ELR_RATE); System. out. print 1n ("RUB \t,t\t+ CurrencyConverter. USD_TO RUB RATE); System. out. print]n("CNY t\t\t+ CurrencyConverter. USD TO_CNY RATE); System. out. print]n("AUD \tt\t+ CurrencyConverter. USD_TO_AUD_RATE); System. out. print 1n ("CHF \\t\t+ CurrencyConverter. USD TO CHF_RATE); System. out. print]n("SGD tt\t\t+ CurrencyConverter. USD TO_HKD_RATE); System. out. print]n("SEK \\t\t+ CurrencyConverter. USD_TO_SEK RATE); System, out. print 1n ("KRW \t\\t+ CurrencyConverter. USD_TO_KRW_RATE); // Loop where program runs while (true) \{ System.out.println("P lease enter the currency you want to convert (ex: USD, CAD, GBP):"); String sourceCurrency = scanner. nextLine O; System. out. println("P lease enter the currency you want to convert to (ex: USD, CAD, GBP):"); String targetCurrency = scanner. nextLineO; System. out. println("Please enter the amount:"); double amount; //Making sure we get a valid input (e.g. not something like " 32332 or "asijfkv") try f 3 amount = Doub le. parseDoub le (scanner. nextLine O); catch (NumberFormatException e) \& System. out. println( "Invalid amount. Please enter a number. "); contirueStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started