I am trying to make a for loop in Java that will convert Farenheit to Celcius starting from 30 going to 100 and
Here's what I have:
public static void farenheitToCelcius() {
double farenheit = 0;
for(double i = 30; i < 100; i = 10) {
double celcius = ((farenheit - 32) *5) / 9;
System.out.println(celcius);
}
}
I keep getting -17.777
printed 7 times. Would really appreciate some help here.
CodePudding user response:
In the farenheit to celsius conversion, you use farenheit
variable (which remains constant at 0
) instead of i
.
This would be a correct implementation (assuming the conversion equation is correct):
public static void farenheitToCelcius(){
//double farenheit = 0;
for(double i = 30; i < 100; i = 10){
double celcius = ((i - 32) *5) / 9;
System.out.println(celcius);
}
}
If you want to include ten, make sure the condition is i <= 100
and make i
an int
to prevent round-off error.
CodePudding user response:
You are not assigning the incremented value, it is always using double farenheit = 0
hence the loop is constantly outputting 17,77 as it is receiving 0 as its parameter
public static void farenheitToCelcius(){
double farenheit = 0;
for(double i = 30; i < 100; i = 10){
farenheit = i; // the value needs to be set here otherwise it is always calculating 0
double celcius = (farenheit - 32) * 5/9;
System.out.println(celcius);
}
}
CodePudding user response:
The other two Answers are good, but could use improved naming and formatting.
Notice how we made double
literals by appending d
to each number.
And notice how we took the advice of the Comment above to use a descriptive name (fahrenheit
) rather than merely i
. This makes our code more self-documenting. In this case, using i
is both non-descriptive and misleading as the type here is not an integer.
public class Converters
{
public static double fahrenheitToCelsius ( final double fahrenheit )
{
double celsius = ( fahrenheit - 32d ) * ( 5d / 9d );
return celsius;
}
}
And, our loop through some Fahrenheit values.
double start = 30d, end = 100d;
for ( double fahrenheit = start ; fahrenheit <= end ; fahrenheit = 10 )
{
double celsius = Converters.fahrenheitToCelsius( fahrenheit );
System.out.println( "fahrenheit " fahrenheit " ⟹ celsius " celsius );
}
When run.
fahrenheit 30.0 ⟹ celsius -1.1111111111111112
fahrenheit 40.0 ⟹ celsius 4.444444444444445
fahrenheit 50.0 ⟹ celsius 10.0
fahrenheit 60.0 ⟹ celsius 15.555555555555557
fahrenheit 70.0 ⟹ celsius 21.11111111111111
fahrenheit 80.0 ⟹ celsius 26.666666666666668
fahrenheit 90.0 ⟹ celsius 32.22222222222222
fahrenheit 100.0 ⟹ celsius 37.77777777777778
Bonus: You could use streams to write a one-liner. Not that this is an improvement in this case, but FYI.
We use IntStream
to generate a sequence of integer numbers while incrementing by ten, convert those to double
values, and convert to celsius, while sending each result to the console.
IntStream.iterate( 30 , fahrenheit -> fahrenheit 10 ).limit( ( ( 100 - 30 ) / 10 ) 1 ).asDoubleStream().map( Converters :: fahrenheitToCelsius ).forEach( System.out :: println );
Or, reformatted:
IntStream
.iterate( 30 , fahrenheit -> fahrenheit 10 )
.limit( ( ( 100 - 30 ) / 10 ) 1 )
.asDoubleStream()
.map( Converters :: fahrenheitToCelsius )
.forEach( System.out :: println );
See that code run live at IdeOne.com.
Or write the conversion into that one-liner, rather than call another method.
IntStream
.iterate( 30 , fahrenheit -> fahrenheit 10 )
.limit( ( ( 100 - 30 ) / 10 ) 1 )
.asDoubleStream()
.map( fahrenheit -> ( fahrenheit - 32d ) * ( 5d / 9d ) )
.forEach( System.out :: println );