-1

I'm having trouble resolving how to take an input based of two possible options and returning a value for both options.

example:

What temperature system do you use: celsius or Fahrenheit

(after selection is made, in this case celsius, take answer and convert to Fahrenheit)

Output both temperatures

I've tried 'if else' and 'switch' statement but the when I make a selection say Celsius it will convert both

System.out.println("Select which temperature scale you currently use, (c)elsius or (f)ahrenheit: ");
temperatureScale = scannerIn.next();

System.out.println("Temp in C is: " + temperatureInC);
     */

if (temperatureScale.equals("c")) {
System.out.println("Please enter the temperature in Celsius: ");
temperatureInC = scannerIn.nextDouble();
}
else {
System.out.println("Please enter the temperature in Fahrenheit: ");
temperatureInF = scannerIn.nextDouble();
}

tempInCelsiusConvFromFahrenheit = (temperatureInF - 32) * 5/9;
tempInFahrenheitConvFromCelsius = (temperatureInC * 9/5) + 32;


averageQuizScore = ((quiz1 + quiz2 + quiz3)/3);

System.out.println("*** Thank You ***");
System.out.println("Student EMPLID:" + studentID);
System.out.println("Quiz 1 Score: " + quiz1);
System.out.println("Quiz 2 Score: " + quiz2);
System.out.println("Quiz 3 Score: " + quiz3);
System.out.println("Average quiz score: " + df2.format(averageQuizScore));
System.out.println("Age in months: " + age * 12);
System.out.println("Age in years: " + age);
System.out.println("Temperature in Celsius: " +   tempInCelsiusConvFromFahrenheit + '°');
 System.out.println("Temperature is Fahrenheit: " + tempInFahrenheitConvFromCelsius + '°');


}

}

I selected celsius and enter 32. and thought I would get 32 C and 91.4 but I got: Temperature in Celsius: -17.77777777777778° Temperature is Fahrenheit: 91.4°

Carefulle
  • 11
  • 1
  • 2
  • 5

2 Answers2

1

If you trace your program you will understand:

1) You enter a value for Celcius, therefore your program falls into:

if (temperatureScale.equals("c")) {
    System.out.println("Please enter the temperature in Celsius: ");
    temperatureInC = scannerIn.nextDouble();
}

then you try display a value for temperature in Celsius by:

System.out.println("Temperature in Celsius: " +   tempInCelsiusConvFromFahrenheit + '°');

and you try to do it by getting a value for the tempInCelsiusConvFromFahrenheit variable.

However, you try to fill tempInCelsiusConvFromFahrenheit variable with a value in

tempInCelsiusConvFromFahrenheit = (temperatureInF - 32) * 5/9;

and you try to get this value in "Else Block" that you never got into:

else {
     System.out.println("Please enter the temperature in Fahrenheit: ");
     temperatureInF = scannerIn.nextDouble();
}

but you never get into there. So it is probably initialized as 0 when you declare it. And (0-32)*5/9 = -17.78°C. So the output is correct.

But your coding design is wrong.

I hope this helps.

obayral
  • 141
  • 3
  • 15
1

Your calculations are correct. If the user inputs celsius don't convert it. In other words temp in celsius is tempuratureInC and not tempInCelsiusConvFromFahrenheit.

erudite
  • 11
  • 2