In: Computer Science
Hello i am working on an assignment for my programming course in JAVA. The following is the assignment:
In main, first ask the user for their name, and read the name into a String variable. Then, using their name, ask for a temperature in farenheit, and read that value in. Calculate and print the equivalent celsius, with output something like
Bob, your 32 degrees farenheit would be 0 degrees celsius
Look up the celsius to farenheit conversion if you do not remember it, your answer should not always be a whole number.
☑ Then have your program ask for the temperature in celsius and print the equivalent farenheit, in the same way.
I have correctly been able to do the first step, however i am unable to figure out how to properly code for the second task, where the program asks for the temperature in celsius and prints the farenheit equivalent. Here is what i have done.
import java.util.Scanner;
public class TempConverter {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Scanner scName = new Scanner(System.in);
String name;
System.out.print("Enter your name : ");
name = scName.nextLine();
System.out.print("Hi "+name+" Enter temperature in farenheit :
");
double temp = sc.nextDouble();
double tempInCel = (temp - 32) * 5 / 9;
double tempInFahr = (temp *9/5)+ 32;
System.out.println(name+", your "+temp+" degrees farenheit would be
"+tempInCel+" degrees celsius");
System.out.println(", your "+temp+ " degrees celsius would be"
+tempinFahr+" degrees celsius");
}
}
import java.util.Scanner;
public class TempConverter {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Scanner scName = new Scanner(System.in);
String name;
System.out.print("Enter your name : ");
name = scName.nextLine();
System.out.print("Hi "+name+" Enter temperature in farenheit :
");
double temp = sc.nextDouble();
double tempInCel = (temp - 32) * 5 / 9;
System.out.println(name+", your "+temp+" degrees farenheit would be
"+tempInCel+" degrees celsius");
System.out.print("Hi "+name+" Enter temperature in celsius :
");
sc = new Scanner(System.in);
temp = sc.nextDouble();
double tempInFahr = (temp *9/5)+ 32;
System.out.println(name+", your "+temp+ " degrees celsius would be
" +tempInFahr+" degrees farenheit");
}
}
/* OUTPUT */
Enter your name : Bob
Hi Bob Enter temperature in farenheit : 32
Bob, your 32.0 degrees farenheit would be 0.0 degrees celsius
Hi Bob Enter temperature in celsius : 0
Bob, your 0.0 degrees celsius would be 32.0 degrees
farenheit