首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java温度计算器

Java温度计算器
EN

Stack Overflow用户
提问于 2016-02-03 16:26:53
回答 3查看 2.1K关注 0票数 1

我在编译过程中收到错误。它期待着一个.class。我不认为这需要一个。我对编码很陌生,所以请原谅我的无知。我也想要一些指导,当用户输入C或F时,如何使大小写无效,这样他们就可以放c或f,而不会得到错误消息。

这是我的密码:

代码语言:javascript
复制
  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);
}
  }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-02-03 16:31:48

代码语言:javascript
复制
if (int=0)

这是无效的Java语法。int是一种类型,而不是变量。你的意思是:

代码语言:javascript
复制
if(option == 0)

注意== (比较)而不是= (赋值)。这就是您要实现的流程:

代码语言:javascript
复制
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!");
  }

如果希望用户输入fc而不提及学位,则需要使用:

代码语言:javascript
复制
String option = input.next();

以获得String而不是Integer

然后:

代码语言:javascript
复制
if (option.equals('F') { ... }
else if(option.equals('C') { ... }
else { ... }

如果您希望您的输入不区分大小写,请查看toLowerCasetoUpperCase并将其应用于您的需要(这里有一个答案说明了它的使用)。

票数 0
EN

Stack Overflow用户

发布于 2016-02-03 16:31:49

int是保留的关键字,在java中你不能用它和表达式进行比较,你应该像这样使用他的值:

代码语言:javascript
复制
    int option = input.nextInt();
      if (option ==0) {

不是if (int=0) {=用于赋值,而是使用==检查等式。

票数 0
EN

Stack Overflow用户

发布于 2016-02-03 16:38:17

您首先必须检查ifs语句中的验证,正如前面的答案所提到的那样。

另外,我认为您的实际转换公式是交换的。

然后,为了接收,无论是'C‘还是'c'

读取字符串,而不是整型

代码语言:javascript
复制
String option = input.next();

然后所有输入转换为小写

代码语言:javascript
复制
if (option.toLowerCase().equals("f"))

以下是完整的示例:

代码语言:javascript
复制
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");
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35182416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档