Guest User

Untitled

a guest
Jan 13th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Main1 {
  4. public static void main(String[] args) {
  5. //Notre objet Scanner
  6. Scanner sc = new Scanner(System.in);
  7.  
  8. //initialisation des variables
  9. double aConvertir, convertit=0;
  10. char reponse=' ', mode = ' ';
  11.  
  12. System.out.println("CONVERTISSEUR DEGRÉS CELSIUS ET DEGRÉS FAHRENHEIT");
  13. System.out.println("-------------------------------------------------");
  14.  
  15. do{//tant que reponse = O //boucle principale
  16.  
  17. do{//tant que reponse n'est pas O ou N
  18. mode = ' ';
  19. System.out.println("Choisissez le mode de conversion : ");
  20. System.out.println("1 - Convertisseur Celsius - Fahrenheit");
  21. System.out.println("2 - Convertisseur Fahrenheit - Celsius ");
  22. mode = sc.nextLine().charAt(0);
  23.  
  24. if(mode != '1' && mode != '2')
  25. System.out.println("Mode inconnu, veuillez réitérer votre choix.");
  26.  
  27. }while (mode != '1' && mode != '2');
  28.  
  29. //saisie de la température à convertir
  30. System.out.println("Température à convertir :");
  31. aConvertir = sc.nextDouble();
  32. //Pensez à vider la ligne lue
  33. sc.nextLine();
  34.  
  35. //Selon le mode, on calcule différemment et on affiche le résultat
  36. if(mode == '1'){
  37. convertit = ((9.0/5.0) * aConvertir) + 32.0;
  38. System.out.print(aConvertir + " °C correspond à : ");
  39. System.out.println(arrondi(convertit, 2) + " °F.");
  40. }
  41. else{
  42. convertit = ((aConvertir - 32) * 5) / 9;
  43. System.out.print(aConvertir + " °F correspond à : ");
  44. System.out.println(arrondi(convertit, 2) + " °C.");
  45. }
  46.  
  47. //On invite l'utilisateur à recommencer ou à quitter
  48. do{
  49. System.out.println("Souhaitez-vous convertir une autre température ?(O/N)");
  50. reponse = sc.nextLine().charAt(0);
  51.  
  52. }while(reponse != 'O' && reponse != 'N');
  53.  
  54. }while(reponse == 'O');
  55.  
  56. System.out.println("Au revoir !");
  57.  
  58. //Fin de programme
  59. }
  60.  
  61. public static double arrondi(double A, int B) {
  62. return (double) ( (int) (A * Math.pow(10, B) + .5)) / Math.pow(10, B);
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment