KONVERSI SUHU
KONVERSI SUHU MENGGUNAKAN
BAHASA PROGRAM JAVA
1. Buka Aplikasi NetBeans lalu buat project baru. pastikan nama class sama dengan nama file-nya
package com.mycompany.temeratur;
import java.util.Scanner;
public class Temeratur {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Pilih skala suhu yang ingin dimasukkan:");
System.out.println("1. Celsius");
System.out.println("2. Reamur");
System.out.println("3. Kelvin");
System.out.println("4. Fahrenheit");
System.out.print("Masukkan pilihan (1-4): ");
int pilihan = scanner.nextInt();
System.out.print("Masukkan nilai suhu: ");
double nilai = scanner.nextDouble();
if (pilihan == 1) { // Celsius
konversiDariCelsius(nilai);
} else if (pilihan == 2) { // Reamur
konversiDariReamur(nilai);
} else if (pilihan == 3) { // Kelvin
konversiDariKelvin(nilai);
} else if (pilihan == 4) { // Fahrenheit
konversiDariFahrenheit(nilai);
} else {
System.out.println("Pilihan tidak valid.");
}
scanner.close();
}
static void konversiDariCelsius(double celsius) {
double reamur = celsius * 4 / 5;
double kelvin = celsius + 273.15;
double fahrenheit = (celsius * 9 / 5) + 32;
System.out.printf("Reamur: %.2f, Kelvin: %.2f, Fahrenheit: %.2f\n", reamur, kelvin, fahrenheit);
}
static void konversiDariReamur(double reamur) {
double celsius = reamur * 5 / 4;
double kelvin = celsius + 273.15;
double fahrenheit = (celsius * 9 / 5) + 32;
System.out.printf("Celsius: %.2f, Kelvin: %.2f, Fahrenheit: %.2f\n", celsius, kelvin, fahrenheit);
}
static void konversiDariKelvin(double kelvin) {
double celsius = kelvin - 273.15;
double reamur = celsius * 4 / 5;
double fahrenheit = (celsius * 9 / 5) + 32;
System.out.printf("Celsius: %.2f, Reamur: %.2f, Fahrenheit: %.2f\n", celsius, reamur, fahrenheit);
}
static void konversiDariFahrenheit(double fahrenheit) {
double celsius = (fahrenheit - 32) * 5 / 9;
double reamur = celsius * 4 / 5;
double kelvin = celsius + 273.15;
System.out.printf("Celsius: %.2f, Reamur: %.2f, Kelvin: %.2f\n", celsius, reamur, kelvin);
}
}
Comments
Post a Comment