我在编译过程中收到错误。它期待着一个.class。我不认为这需要一个。我对编码很陌生,所以请原谅我的无知。我也想要一些指导,当用户输入C或F时,如何使大小写无效,这样他们就可以放c或f,而不会得到错误消息。
这是我的密码:
import java.util.Scanner;
import java.util.InputMismatchException;
public class TempCALC
{
public static void main(String[] args)
{
System.out.println("This Program will allow the user to calculate temperature.");
calculateTemp();
}
private static void calculateTemp() {
int F;
int C;
F=0;
C=1;
Scanner input = new Scanner (System.in);
System.out.println("Please enter a F to convert Fahrenheit to Celsius.");
System.out.println("Please enter a C to convert Celsius to Fahrenheit.");
int option = input.nextInt();
if (int=0) {
System.out.println("Please enter a temperature in degrees Fahrenheit.");
ftoc();
} else if (int= 1) {
System.out.println("Please enter a temperature in degrees Celsius.");
ctof();
} else {
System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
}
}
private static void ftoc() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
System.out.println(celsius + "celsius is" + ((celsius * 9 / 5.0) + 32) + "Fahrenheite");
calculatetemp();
}
private static void ctof() {
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
System.out.println(Fahrenheit + "Fahrenheit is" + ((Fahrenheit - 32) * (5 / 9.0)) + "Celsius");
calculatetemp();
}
private static void print(String string); {
System.out.println("\n" + string);
}
}发布于 2016-02-03 16:31:48
if (int=0)这是无效的Java语法。int是一种类型,而不是变量。你的意思是:
if(option == 0)注意== (比较)而不是= (赋值)。这就是您要实现的流程:
if (option == 0) {
System.out.println("Please enter a temperature in degrees Fahrenheit.");
ftoc();
} else if (option == 1) {
System.out.println("Please enter a temperature in degrees Celsius.");
ctof();
} else {
System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
}如果希望用户输入f或c而不提及学位,则需要使用:
String option = input.next();以获得String而不是Integer。
然后:
if (option.equals('F') { ... }
else if(option.equals('C') { ... }
else { ... }如果您希望您的输入不区分大小写,请查看toLowerCase或toUpperCase并将其应用于您的需要(这里有一个答案说明了它的使用)。
发布于 2016-02-03 16:31:49
int是保留的关键字,在java中你不能用它和表达式进行比较,你应该像这样使用他的值:
int option = input.nextInt();
if (option ==0) {不是if (int=0) {和=用于赋值,而是使用==检查等式。
发布于 2016-02-03 16:38:17
您首先必须检查ifs语句中的验证,正如前面的答案所提到的那样。
另外,我认为您的实际转换公式是交换的。
然后,为了接收,无论是'C‘还是'c'
String option = input.next();然后将所有输入转换为小写
if (option.toLowerCase().equals("f"))以下是完整的示例:
public class TempCALC {
public static void main(String[] args) {
System.out.println("This Program will allow the user to calculate temperature.");
calculateTemp();
}
private static void calculateTemp() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a F to convert Fahrenheit to Celsius.");
System.out.println("Please enter a C to convert Celsius to Fahrenheit.");
String option = input.next();
if (option.toLowerCase().equals("f")){
System.out.println("Please enter a temperature in degrees Fahrenheit.");
ftoc();
}else if (option.toLowerCase().equals("c")){
System.out.println("Please enter a temperature in degrees Celsius.");
ctof();
}else{
System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
}
}
private static void ftoc() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
System.out.println(celsius + "celsius is" + ((celsius * 9 / 5.0) + 32) + "Fahrenheite");
calculatetemp();
}
private static void ctof() {
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
System.out.println(Fahrenheit + "Fahrenheit is" + ((Fahrenheit - 32) * (5 / 9.0)) + "Celsius");
calculatetemp();
}
private static void print(String string){
System.out.println("\n" + string);
}
private static void calculatetemp(){
System.out.println("\nInside calculateTemp");
}
}https://stackoverflow.com/questions/35182416
复制相似问题