Home > database >  how to remove repeated initial variable. Celsius to Fahrenheit in table (Java)
how to remove repeated initial variable. Celsius to Fahrenheit in table (Java)

Time:10-13

I'm having trouble removing the initial value I put for Fahrenheit in the output. When it's printed there is 104.0 under Fahrenheit each time. The right value is on the celsius side under the correct celsius number. How do I remove the 104.0 and move the right Fahrenheit number to the Fahrenheit side?

public static void main(String[] args) {
    double celsius =40.0;
    double fahrenheit = ((9.0/5.0 * celsius)   32);
    System.out.println("Celsius\t\tFahrenheit");
    do {
          System.out.println(celsius  "\t"   "\t"  fahrenheit  );
          System.out.println((9.0/5.0 * celsius)   32);
          --celsius;
    }while (celsius >= 31);
         
    
}}

It's supposed to look like this: Under Celsius: 40, 39.0,...32.0,31.0 Under Fahrenheit: 104.0, 102.2,...89.6,87.8

CodePudding user response:

You calculate the value of fahrenheit before the loop, but never modify the value inside of the loop. You do however, print it inside every loop iteration, which means you will print 104 every loop.

All you need to do is move the calculation for fahrenheit inside of the loop and remove the extra print statement, you do not need to make a special case for the first case, it will simply print on the first calculation on the first iteration of the loop.

Here is the full change and test run:

public static void main(String[] args) {
    double celsius = 40.0;
    System.out.println("Celsius\t\tFahrenheit");
    do {
        double fahrenheit = ((9.0 / 5.0 * celsius)   32);
        System.out.println(celsius   "\t"   "\t"   fahrenheit);
        --celsius;
    } while (celsius >= 31);
}

Test Run:

Celsius     Fahrenheit
40.0        104.0
39.0        102.2
38.0        100.4
37.0        98.60000000000001
36.0        96.8
35.0        95.0
34.0        93.2
33.0        91.4
32.0        89.6
31.0        87.80000000000001

CodePudding user response:

If you're looking for an output as follows:

Celsius         Fahrenheit
40.000000       104.000000
39.000000       102.200000
38.000000       100.400000
37.000000       98.600000
36.000000       96.800000
35.000000       95.000000
34.000000       93.200000
33.000000       91.400000
32.000000       89.600000
31.000000       87.800000

Use printf inside the loop. See this for details.

public class MyClass {
    public static void main(String args[]) {
        double celsius = 40.0;
        System.out.println("Celsius\t\t\tFahrenheit");
        do {
          double fahrenheit = 9.0/5.0 * celsius   32;
          System.out.printf("%f\t\t%f\n", celsius, fahrenheit);
          --celsius;
        } while (celsius >= 31);
    }
}

If you want formatted output, like only two decimal points precision, see this.

CodePudding user response:

public static void main(String... args) {
    int celsius = 40;

    System.out.println("Celsius\t\tFahrenheit");

    while (celsius >= 32) {
        System.out.format(Locale.ENGLISH, "%d\t\t\t%.1f\n", celsius, convertToFahrenheit(celsius));
        celsius--;
    }
}

public static double convertToFahrenheit(double celsius) {
    return celsius * 1.8   32;
}

Demo

Celsius     Fahrenheit
40          104.0
39          102.2
38          100.4
37          98.6
36          96.8
35          95.0
34          93.2
33          91.4
32          89.6
  •  Tags:  
  • java
  • Related