I am having some trouble getting my while loop to reiterate when executed. The if statements work as expected but the char value returned from the scanner isn't getting to my while loop. Anyone know what I did wrong or a better solution to my program?
package classWork;
import java.util.Scanner;
public class project_2 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int choice = 0;
double celsius, fahrenheit, inches, centimeters;
char doAgain = 'y';
while (doAgain == 'y' || doAgain == 'Y')
{
System.out.print(" Main Menu \n 1. Celsius to Fahrenheit \n 2. Inches to Centimeters \n"
+ "Please select either 1 or 2 \nThen press enter to continue");
choice = input.nextInt();
if (choice == 1)
{
System.out.println("This program will convert a celsius value into a fahrenheit one. \n");
System.out.println("Please enter the tempature for conversion: ");
celsius = input.nextInt();
fahrenheit = 1.8 * celsius + 32.0;
System.out.println(celsius + " degree(s) in celcius" + " is " + fahrenheit +
" in fahrenheit. \n");
System.out.println("Do you want to continue?\n: " +
"enter a \'y \' for another round\n " +
" enter a\'n\' to end the program\n");
doAgain = input.next().charAt(0);
input.close();
return;
}
if (choice == 2)
{
System.out.println("This program will convert inches to centimeters. \n");
System.out.println("Please enter the length for conversion: ");
inches = input.nextInt();
centimeters = 2.54 * inches;
System.out.println(inches + " inches is " + centimeters + " centimeters. \n");
System.out.println("Do you want to continue?\n: " +
"enter a \'y \' for another round\n " +
" enter a\'n\' to end the program\n");
doAgain = input.next().charAt(0);
input.close();
return;
}
else
{
System.out.println("That is not a valid option. Please restart and try again");
input.close();
}
}
}
}
returnafter asking the user if they want to continue. That will exit the method (and the loop, obviously).returnat the end of yourifstatement blocks