Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MetricConverter_04 {
- public static void main(String[] args) {
- Scanner myScan = new Scanner(System.in);
- // converting m <-> mm <-> cm <-> m
- double number = Double.parseDouble(myScan.nextLine());
- String convertFrom = myScan.nextLine();
- String convertTo = myScan.nextLine();
- double numberInMetres = 0;
- double numberInCentimetres = 0;
- double numberInMillimetres = 0;
- if (convertFrom.equals("m")) {
- numberInCentimetres = number * 100;
- numberInMillimetres = number * 1000;
- } else if (convertFrom.equals("cm")) {
- numberInMetres = number / 100;
- numberInMillimetres = number * 10;
- } else if (convertFrom.equals("mm")) {
- numberInMetres = number / 1000;
- numberInCentimetres = number / 10;
- }
- if (convertTo.equals("m")) {
- System.out.printf("%.3f", numberInMetres);
- } else if (convertTo.equals("cm")) {
- System.out.printf("%.3f", numberInCentimetres);
- } else if (convertTo.equals("mm")) {
- System.out.printf("%.3f", numberInMillimetres);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement