我是java的新手,我试图编写一个将摄氏温度转换为华氏温度的程序
import java.util.Scanner;
public class Temps
{
public static void main(String[] args)
{
System.out.print("Enter temp(70 f or 20 c): ");
double temp = keyboard.nextDouble();
String units = keyboard.next();
if (units.equal("f"))
newtemp = ftoc(temp);
else if (units.equals("c"))
newtemp = ctof(temp);
else System.err.println("units must be c or f");
}
public static double ftoc (int c)
return (( 5.0 / 9.0) * (c - 32));
}
public static double ctof (int f)
{
return ((9.0/5.0)* f+32);
}
}
有人可以向我解释我做错了什么。
答案 0 :(得分:1)
相当多的问题
查看固定代码中的评论
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter temp(70 f or 20 c): ");
double temp = keyboard.nextDouble();
String units = keyboard.next();
double newtemp = -1; // not declared
if (units.equals("f")) // should be equals
newtemp = ftoc(temp);
else if (units.equals("c"))
newtemp = ctof(temp);
else System.err.println("units must be c or f");
System.out.println("the new temp is " + newtemp); // need to print it out
}
public static double ftoc (double c) { // take a double
return (( 5.0 / 9.0) * (c - 32));
}
public static double ctof (double f) // take a double
{
return ((9.0/5.0)* f+32);
}