Creating a conversion table from Fahrenheit to Celsius degrees

Asked

Viewed 956 times

2

Write a program that given two temperatures in Fahrenheit degrees (integer values) produces a table with all values in this interval of half degree Fahrenheit. For example, the execution of the program for values 30 and 35 should produce the following output:

Enter spaced interval values (min max):

30

35

Fahrenheit Celsius

30.00 -1.11

30.50 -0.83

31.00 -0.56

31.50 -0.28

32.00 0.00

32.50 0.28

33.00 0.56

33.50 0.83

34.00 1.11

34.50 1.39

35.00 1.67

And I leave my code there, but after I insert the maximum the program gives the error:

java.util.Illegalformatconversionexception: f != java.lang.String

public static void main(String[] args)
{
    System . out . println("Por favor indique um valor minimo:");
    int min = scanner.nextInt();
    System.out.println("Agora indique um valor máximo:");
    int max = scanner.nextInt();
    double celsius = (min-32*(5/9));
    int i = 0;
    min++;


    for (i=0; max + 1 > min;min++)
    {
        System.out.printf("%6.2f\t\t%6.2f\n","Fahrenheit" + min + " Celsius " + celsius);
    }
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

4

There are several errors, some perhaps because part of the code is missing.

If you are going to calculate data with decimal part you need to use at least one float, cannot be a whole.

If the calculation needs to be done in each row of the table the calculation formula needs to be inside the loop to repeat each time.

And should do with the loop variable that increments. Alias who should increment is the loop control variable and not the minimum. The minimum should be used to initialize it. The increment should be 0.5 as the statement shows. The output condition was also wrong, the control variable that should go up to the maximum.

The formatting of the text is also wrong. See documentation how to use.

import java.util.*;

class Ideone {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); 
        System.out.println("Por favor indique um valor minimo:");
        float min = scanner.nextFloat();
        System.out.println("Agora indique um valor máximo:");
        float max = scanner.nextFloat();
        System.out.printf("Fahrenheit Celsius\n");
        for (float i = min; i <= max; i += .5) {
            System.out.printf("%6.2f     %6.2f%n", i, ((i - 32.0) * ( 5.0 / 9.0)));
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.