Vous êtes sur la page 1sur 2

}

IF-ELSE with UNIT CHOICE


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package sw2aaquienw;
import java.util.Scanner;
/**
*
* @author user4
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input=new Scanner(System.in);
double C, F, K, R, X;
System.out.print("Press 1 if you like to convert to
Farenheit.\n");
System.out.print("Press 2 if you like to convert to
Kelvin.\n");
System.out.print("Press 3 if you like to convert to
Rankine.\n");
System.out.print("\nEnter your choice of unit.");
X=input.nextDouble();
if(X==1)
{
System.out.print("Please enter the Temperature in
Celsius:");
C=input.nextDouble();
F=(9*C/5)+32;
System.out.printf("\nThe temperature in Farenheit
is %8.4f.\n", +F);
}
else
{
if(X==3)
{
System.out.print("Please enter the Temperature in
Celsius:");
C=input.nextDouble();
F=(9*C/5)+32;
R=F+459.67;
System.out.printf("\nThe temperature in Rankine is
%8.4f.\n", +R);
}
else
{
if(X==2)
{
System.out.print("Please enter the Temperature in
Celsius:");
C=input.nextDouble();
K=C+273.15;
System.out.printf("\nThe temperature in Kelvin is
%8.4f.\n", +K);
}
else
{
System.out.printf("\nPlease enter again the right
choice for the conversion.\n");
main(args);

}
}
}
}

SWITCH-CHOICE
package sw2baquienw;
import java.util.Scanner;
/**
*
* @author user4
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input=new Scanner(System.in);
double C, F, K, R;
int choice;
System.out.print("Press 1 if you like to convert to
Farenheit.\n");
System.out.print("Press 2 if you like to convert to
Kelvin.\n");
System.out.print("Press 3 if you like to convert to
Rankine.\n");
System.out.print("\nEnter your choice of unit.");
choice=input.nextInt();
switch(choice)
{
case 1:
System.out.print("Please enter the Temperature in
Celsius:");
C=input.nextDouble();
F=(9*C/5)+32;
System.out.printf("\nThe temperature in Farenheit is
%8.4f.\n", +F);
break;
case 2:
System.out.print("Please enter the Temperature in
Celsius:");
C=input.nextDouble();
K=C+273.15;
System.out.printf("\nThe temperature in Kelvin is
%8.4f.\n", +K);
break;
case 3:
System.out.print("Please enter the Temperature in
Celsius:");
C=input.nextDouble();
F=(9*C/5)+32;
R=F+459.67;
System.out.printf("\nThe temperature in Rankine is
%8.4f.\n", +R);
break;

default:
{
System.out.print("\nRe-enter the right choice for the
conversion.\n\n");
main(args);
}

}
}
}

DO-WHILE
package dowhileaquien;
import java.util.Scanner;
/**
*
* @author user4
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input=new Scanner(System.in);
double S, E, T, psi, mmHg, ftH2O, bar;
System.out.print("Please enter the starting pressure value in psi: ");
S=input.nextDouble();
System.out.print("Please enter the ending pressure value in psi: ");
E=input.nextDouble();
System.out.print("\n
psi=S;

psi\t

mmHg\t

ftH2O\t

bar\n");

do
{
mmHg=psi*(760/14.7);
ftH2O=psi*(33.899/14.7);
bar=psi*(1.01325/14.7);
System.out.printf("\n%11.4f\t %11.4f\t %11.4f\t %11.4f\t", psi,mmHg,ftH2O,bar);
psi=psi+5.0;
}
while (psi!=(E+5.0));
}
}

Vous aimerez peut-être aussi