srgrtshnbra
Printable View
srgrtshnbra
this should work all in one class. Let me know if it doesn't, I just threw in an if else statementCode:import java.util.*;
public class FinalProject {
static Scanner cm = new Scanner(System.in); // accessible to all methods
public static void main (String args[]){
// 2.54cm is 1 inch
System.out.println("Enter the CM: "); // Write input
String input = cm.next();
if (input == "b" || input == "B") {
getDegree();
}
else {
//double
double centimeters = cm.nextDouble();
double inches = centimeters/2.54;
System.out.println(inches + " Inch Is " + centimeters + " centimeters");
}
}
public void getDegree() {
double celsius=0, fahrenheit=0;
System.out.println("Enter the amount of Fahrenheit to be converted: ");
fahrenheit = cm.nextDouble();
celsius = (fahrenheit-32)*5/9;
System.out.println("The entered amount of Fahrenheit is equal to " + celsius + " degrees Celsius.");
}
}
I didn't put and if statement for the cm converter I just made it default if they don't type in B or b that it goes to the cm converter.
Thanks for replying but it is showing me this error > Attachment 31501
Try making it public static void getDegree()
no still showing error
it is showing this error
you have to change public void getDegree() to public static void getDegree().
Code:import java.util.*;
public class FinalProject {
static Scanner cm = new Scanner(System.in); // accessible to all methods
public static void main (String args[]){
// 2.54cm is 1 inch
System.out.println("Enter the CM: "); // Write input
String input = cm.next();
if (input == "b" || input == "B") {
getDegree();
}
else {
//double
double centimeters = cm.nextDouble();
double inches = centimeters/2.54;
System.out.println(inches + " Inch Is " + centimeters + " centimeters");
}
}
public static void getDegree() {
double celsius=0, fahrenheit=0;
System.out.println("Enter the amount of Fahrenheit to be converted: ");
fahrenheit = cm.nextDouble();
celsius = (fahrenheit-32)*5/9;
System.out.println("The entered amount of Fahrenheit is equal to " + celsius + " degrees Celsius.");
}
}
When comparing objects (Strings in particular) == tests for identity (ie, if the references compared refer to the same object); to test equality use the equals method. In this case you can also use equalsIgnoreCase:Code:if ("b".equalsIgnoreCase(input)) {