-
Temperature converstion
Assignment:
Write and run a java program that gets an integer temperature form the keyboard and then convert it to degrees C and F, displaying the results a single System.out.println.
Turn in a printed listing of your program and it's output. Run the program 3 times using values 68, 20, and -40.
Sample output:
What is the temperature to convert? 25
25 converts to
-3 deg C
77 deg F
My code:
import java.util.Scanner;
public class tempconv {
public static void main(String[] args) {
double temp; //temperature input
double cel; //Celsius
double fah; //Fahrenheit
Scanner in; new Scanner(System.in);
System.out.println("What is the temperature to covert?");
temp = in.nextDouble();
System.out.println();
cel = (temp - 32) * 5/9;
fah = temp * 9/5 + 32;
System.out.println(temp + "converts to" + cel + "deg C" + fah + "deg F");
}
}
MY ERROR:
tempcov.java:28: variable in might not have been initialized
temp = in.nextDouble();
^
Any Ideas?
-
Also, if I am doing math with doubles, do I need to attach a decimal point and zero at the end of every number?
-
Shouldn't this: Code:
Scanner in; new Scanner(System.in);
be: Code:
Scanner in = new Scanner(System.in);
?
-
Thanks, just a slight overlook I suppose.
Now I have a different problem: When I run the program it just forwarded me back to my original directory.
F\:>javac tempconv.java
F\:>
-
um,... that's not running it. That's compiling it. You need to check out the getting started section of the Java tutorials: Trail: Getting Started (The Java™ Tutorials)
-
I read those but even so, I am unsure as to what needs to be changed. I did not read that error in the tutorial.
-
What error are you talking about?
You haven't read that link send to you by Fubarable. If you have read it, all what you need is there.
After writing your code, have to compile it first. javac command do it for you. According to your application it should be like this.
Code:
javac tempconv.java
It only compile your code, not run. To run the code you have to use java command.