Last active
October 15, 2020 16:08
-
-
Save maf946/5158175ec7f2c769cdeea7e2f5fdb535 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package currencyconversion; | |
import java.util.Scanner; | |
/** | |
* | |
* @author maf946 | |
*/ | |
public class CurrencyConversion { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
// Currency conversion: Write a snippet that first asks the user to type | |
// today's U.S. dollar price for one Bitcoin. Then use a loop to: | |
// 1. prompt the user to enter a Bitcoin quantity | |
// 2. convert that amount to US dollars | |
// 3. print the amount to the screen, formatted to two decimal places | |
// Use "stop" as a sentinel to stop the loop. | |
double dollarPrice; | |
double bitcoinAmount; | |
double conversionPrice; | |
Scanner scnr = new Scanner(System.in); | |
System.out.print("Enter today's U.S. dollar price for one Bitcoin (ex: 999.99): "); | |
dollarPrice = scnr.nextDouble(); | |
while (true) { | |
System.out.print("Please enter a Bitcoin quantity (ex: 1.23), or \"stop\" to stop: "); | |
if (scnr.hasNextDouble()) { | |
bitcoinAmount = scnr.nextDouble(); | |
conversionPrice = dollarPrice * bitcoinAmount; | |
System.out.print(bitcoinAmount + " Bitcoin equals $"); | |
System.out.printf("%.2f", conversionPrice); | |
System.out.print("\n"); | |
} else { | |
if (scnr.next().equals("stop")) { | |
break; | |
} else { | |
System.out.println("Invalid input!"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment