Class 类、接口或枚举应为java编译错误
我的任务是编写显示温度表的代码和将华氏温度转换为摄氏温度的方法。这是到目前为止我的代码。我似乎不知道出了什么问题Class 类、接口或枚举应为java编译错误,class,methods,interface,call,Class,Methods,Interface,Call,我的任务是编写显示温度表的代码和将华氏温度转换为摄氏温度的方法。这是到目前为止我的代码。我似乎不知道出了什么问题 import java.text.DecimalFormat; import java.util.Scanner; public class temperature { public static void main(String[] args) { double fah; int choice;
import java.text.DecimalFormat;
import java.util.Scanner;
public class temperature
{
public static void main(String[] args)
{
double fah;
int choice;
Scanner keyboard = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#.0");
do
{
tempTable();
System.out.println("Enter degrees in Fahrenheit");
System.out.println("and I will convert it to Celsius");
fah = keyboard.nextDouble();
convert(fah);
System.out.println(formatter.format(fah) + " degrees Fahrenheit is " + formatter.format(cel) + " degrees Celsius");
System.out.println("Press 1 to continue");
System.out.println("Press 2 to exit");
choice = keyboard.nextInt();
}while(choice!=2);
}
}
/**
tempTable displays the fahrenheit to celsius temperature table
*/
public static void tempTable()
{
System.out.println("Celsius Temperature Table\n");
System.out.println("Fahrenheit\tCelsius\n");
System.out.printf("%3.1f\n", 0.0);
System.out.printf("%3.1f\n", 1.0);
System.out.printf("%3.1f\n", 2.0);
System.out.printf("%3.1f\n", 3.0);
System.out.printf("%3.1f\n", 4.0);
System.out.printf("%3.1f\n", 5.0);
System.out.printf("%3.1f\n", 6.0);
System.out.printf("%3.1f\n", 7.0);
System.out.printf("%3.1f\n", 8.0);
System.out.printf("%3.1f\n", 9.0);
System.out.printf("%3.1f\n", 10.0);
System.out.printf("%3.1f\n", 11.0);
System.out.printf("%3.1f\n", 12.0);
System.out.printf("%3.1f\n", 13.0);
System.out.printf("%3.1f\n", 14.0);
System.out.printf("%3.1f\n", 15.0);
System.out.printf("%3.1f\n", 16.0);
System.out.printf("%3.1f\n", 17.0);
System.out.printf("%3.1f\n", 18.0);
System.out.printf("%3.1f\n", 19.0);
System.out.printf("%3.1f\n\n", 20.0);
}
/**
celsius returns degrees in fahrenheit
@param fah degrees in fahrenheit enter by user
@return returns degrees in celsius
*/
public static double convert(double fah)
{
double cel;
cel = (fah-32)5/9;
return cel;
}
当我通过终端编译代码时,我得到了类、接口、枚举的预期错误。
我不知道我做错了什么。这不是编写方法的方法吗?您过早地关闭了类温度,根据Java命名约定,类温度应该是温度。此外,您的转换方法使用整数除法,因此您错过了乘法操作数
public static void main(String[] args) {
double fah;
int choice;
Scanner keyboard = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#.0");
do {
tempTable();
System.out.println("Enter degrees in Fahrenheit");
System.out.println("and I will convert it to Celsius");
fah = keyboard.nextDouble();
double cel = convert(fah); // <-- declare cel.
System.out.println(formatter.format(fah)
+ " degrees Fahrenheit is " + formatter.format(cel)
+ " degrees Celsius");
System.out.println("Press 1 to continue");
System.out.println("Press 2 to exit");
choice = keyboard.nextInt();
} while (choice != 2);
} // <-- only one brace after while, the other methods should be in the class.
/**
* celsius returns degrees in fahrenheit
*
* @param fah
* degrees in fahrenheit enter by user
* @return returns degrees in celsius
*/
public static double convert(double fah) {
double cel = (fah - 32) * ((double) 5 / 9); /* <-- multiply.
not integer math. */
return cel;
}