⭐ Let's code a TEMPERATURE CONVERTER using Java!

6 Views
Published
#java #javatutorial #javacourse

import java.util.Scanner;

public class Main {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

double temp;
double newTemp;
String unit;

System.out.print("Enter the temperature: ");
temp = scanner.nextDouble();

System.out.print("Convert to Celsius or Fahrenheit? (C or F): ");
unit = scanner.next().toUpperCase();

// (condition) ? true : false
newTemp = (unit.equals("C")) ? (temp - 32) * 5 / 9 : (temp * 5 / 9) + 32;

System.out.printf("%.1f°%s", newTemp, unit);

scanner.close();
}
}
Category
Bro Code
Tags
Java tutorial, Java course, Java programming
Be the first to comment