Skip to content

Instantly share code, notes, and snippets.

@Rizr09
Created July 21, 2022 09:33
Show Gist options
  • Save Rizr09/89c4cc6896b3cc6b2f12358d244f58e0 to your computer and use it in GitHub Desktop.
Save Rizr09/89c4cc6896b3cc6b2f12358d244f58e0 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Test {
public static float toFahrenheit(int celcius) {
return (celcius * 9 / 5) + 32;
}
public static float toCelcius(int fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
public static void main(String[] args) {
Scanner sc = null;
try {
sc = new Scanner(System.in);
boolean flag = true;
while (!flag) {
System.out.print("Pilih suhu yang dituju: \n1. Fahrenheit\n2. Celcius\n3. Exit\n> ");
int choices = sc.nextInt();
switch (choices) {
case 1:
System.out.print("Masukkan suhu awal (celcius): ");
int celcius = sc.nextInt();
System.out.println(celcius + "\\U+2103" + "= " + toFahrenheit(celcius) + "\\U+2109\nAgain? (y/n)\n> ");
char loop1 = sc.next().charAt(0);
if(loop1 == 'n' || loop1 != 'y'){
flag = false;
}
break;
case 2:
System.out.print("Masukkan suhu awal (fahrenheit): ");
int fahrenheit = sc.nextInt();
System.out.println(fahrenheit + "\\U+2109" + "= " + toCelcius(fahrenheit) + "\\U+2103\nAgain? (y/n)\n> ");
char loop2 = sc.next().charAt(0);
if(loop2 == 'n' || loop2 != 'y'){
flag = false;
}
break;
default:
flag = false;
break;
}
}
} finally {
if (sc != null) {
sc.close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment