Reputation: 329
I'm doing small exercise on java Programming.
Basically the application allows the user to enter a Fahrenheit temperature and displays the Celsius equivalent or enter a Celsius temperature and displays the Fahrenheit equivalent. The application includes a Celsius method which returns the Celsius equivalent of a Fahrenheit temperature. The application also include a Fahrenheit method, which returns the Fahrenheit equivalent of a Celsius temperature.
Here's my full code:
import java.util.Scanner;
public class Temperature{
public static void main(String[] args) { // Main Method//
Scanner input = new Scanner(System.in);
//Declare variables
int choice; // the user's choice in the menu //
int temp;
do {
// print the menu
System.out.println("1.fahrenheit to Celsius");
System.out.println("2.Celsius to Fahrenheit");
System.out.println("3.Exit");
System.out.println("");
System.out.println("Choice:");
choice = input.nextInt();
if ( choice != 3 ) {
System.out.print( "Enter temperature: " ); // Display Enter Temperature
temp = input.nextInt(); // Read Temperature
if (choice == 1) {
System.out.println( temp + " Fahrenheit is " + toCelsius( temp ) + " Celsius ");
System.out.println("");
}
else if (choice == 2 ) {
System.out.println(temp + " Celsius is " + toFahrenheit( temp ) + " Fahrenheit ");
System.out.println("");
}
else {
System.out.println("Program terminated "); // Display "Program terminated" if user entered 3
}
} //end if loop
} // end do loop
while (choice !=3);
} // end main method
// return Celsius equivalent of Fahrenheit temperature
public static int toCelsius(int fahrenheit) {
int celsius;
celsius = (int) (5.0/9.0 * (fahrenheit - 32));
return celsius;
} // end method celsius
// return Fahrenheit equivalent of Celsius temperature
public static int toFahrenheit(int celsius) {
int fahrenheit;
fahrenheit = (int) (9.0/5.0 * celsius + 32);
return fahrenheit;
} // end method fahrenheit
}
Good thing is that the code including the methods are working.The User can either enter choice 1 or 2 and then enter temperature number and display it in Celsius or Fahrenheit. But if the user enter choice 3, the program will display 'Program terminated'. For me, when I entered choice 3, The program stopped working (Did not display 'Program terminated'). I think there's a problem on the 'loop' part or the 'IF' part.
Can anyone help me with this?
Upvotes: 0
Views: 1132
Reputation: 75
You have included everything in if(choice!=3) so even if you enter choice as 3 then also the else including Program terminated won't run. So just re-organize the parenthesis and everything will work fine.
import java.util.Scanner;
public class Temperature{
public static void main(String[] args) { // Main Method//
Scanner input = new Scanner(System.in);
//Declare variables
int choice; // the user's choice in the menu //
int temp;
do {
// print the menu
System.out.println("1.fahrenheit to Celsius");
System.out.println("2.Celsius to Fahrenheit");
System.out.println("3.Exit");
System.out.println("");
System.out.println("Choice:");
choice = input.nextInt();
if ( choice != 3 ) {
System.out.print( "Enter temperature: " ); // Display Enter Temperature
temp = input.nextInt(); // Read Temperature
if (choice == 1) {
System.out.println( temp + " Fahrenheit is " + toCelsius( temp ) +" Celsius ");
System.out.println("");
}
else if (choice == 2 ) {
System.out.println(temp + " Celsius is " + toFahrenheit( temp ) + " Fahrenheit ");
System.out.println("");
}
}
else {
System.out.println("Program terminated "); // Display "Program terminated" if user entered 3
}//end else loop
} // end do loop
while (choice !=3);
} // end main method
// return Celsius equivalent of Fahrenheit temperature
public static int toCelsius(int fahrenheit) {
int celsius;
celsius = (int) (5.0/9.0 * (fahrenheit - 32));
return celsius;
} // end method celsius
// return Fahrenheit equivalent of Celsius temperature
public static int toFahrenheit(int celsius) {
int fahrenheit;
fahrenheit = (int) (9.0/5.0 * celsius + 32);
return fahrenheit;
} // end method fahrenheit
}
Upvotes: 0
Reputation: 12819
Your logic is wrong. You have an outer if
statement that is only entered if the user does not enter three. So if you do, the else
in the inner if
will not be executed, and thus your message will never print:
//If you do enter three, it will skip over all of this:
if ( choice != 3 ) {
System.out.print( "Enter temperature: " ); // Display Enter Temperature
temp = input.nextInt(); // Read Temperature
if (choice == 1) {
System.out.println( temp + " Fahrenheit is " + toCelsius( temp ) + " Celsius ");
System.out.println("");
}
else if (choice == 2 ) {
System.out.println(temp + " Celsius is " + toFahrenheit( temp ) + " Fahrenheit ");
System.out.println("");
}
else {
System.out.println("Program terminated "); // Display "Program terminated" if user entered 3
}
}
You need to remove the outer if
, or alternatively move the else
block to after the outer if
.
Upvotes: 2