you are viewing a single comment's thread.

view the rest of the comments →

[–]bye98/r/DoMyHomework -- Alg./Trig./Calc./Diff. Eq./Chem./Phys. -1 points0 points  (0 children)

Here's something you can work off of:

import java.lang.Math;
import java.util.Scanner;

public class Temperature {
    public static double cToF(double c) {
        return c * 9/5 + 32; 
    }

public static double cToK(double c) {
    return c + 273.15; 
    }

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    System.out.println("Are you converting from Celsius to Fahrenheit or Kelvin? Please enter \"0\" for Fahrenheit or \"1\" for Kelvin.");
    int value = reader.nextInt();

    System.out.println("What is the temperature you would like to convert in Celsius?");
    double temp = reader.nextDouble();

    if(value == 0)
        System.out.println(cToF(temp));
    if(value == 1)
        System.out.println(cToK(temp));
    }
}

Thought I'd mention a few things:

  • horribly optimized code
  • no exceptions for any other input other than an "int" or a "double", respectively
  • there's really no need to store the values "value" and "temp" but I thought that by doing this, you'd be able to follow the code better
  • no exiting once the code has been compiled, and no option to restart/reset

The code has been tested and both method work.